import{n as e}from"./pinia-C4FTeoHo.js";import{t}from"./notification.store-EczSZ_VB.js";import{t as n}from"./payment.service-CJ5dn5gC.js";function r(){return{id:null,payment_number:``,payment_date:``,customer_id:null,customer:null,selectedCustomer:null,invoice_id:null,amount:0,payment_method_id:null,notes:``,currency:null,currency_id:null,exchange_rate:null,maxPayableAmount:2**53-1,selectedNote:null,customFields:[],fields:[]}}var i=e(`payment`,{state:()=>({payments:[],paymentTotalCount:0,selectAllField:!1,selectedPayments:[],selectedNote:null,showExchangeRate:!1,paymentModes:[],currentPaymentMode:{id:``,name:null},currentPayment:r(),isFetchingInitialData:!1}),getters:{getPayment:e=>t=>e.payments.find(e=>e.id===t)},actions:{resetCurrentPayment(){this.currentPayment=r()},async fetchPayments(e){let t=await n.list(e);return this.payments=t.data,this.paymentTotalCount=t.meta.payment_total_count,{data:t}},async fetchPayment(e){let t=await n.get(e);return Object.assign(this.currentPayment,t.data),{data:t}},async addPayment(e){let r=await n.create(e);return this.payments.push(r.data),t().showNotification({type:`success`,message:`payments.created_message`}),{data:r}},async updatePayment(e){let r=await n.update(e.id,e),i=this.payments.findIndex(e=>e.id===r.data.id);return i!==-1&&(this.payments[i]=r.data),t().showNotification({type:`success`,message:`payments.updated_message`}),{data:r}},async deletePayment(e){let t=await n.delete(e),r=e.ids[0],i=this.payments.findIndex(e=>e.id===r);return i!==-1&&this.payments.splice(i,1),{data:t}},async deleteMultiplePayments(){let e=await n.delete({ids:this.selectedPayments});return this.selectedPayments.forEach(e=>{let t=this.payments.findIndex(t=>t.id===e);t!==-1&&this.payments.splice(t,1)}),this.selectedPayments=[],{data:e}},async sendEmail(e){return n.send(e)},async previewPayment(e){return n.sendPreview(e.id,e)},async getNextNumber(e,t=!1){let r=await n.getNextNumber(e);return t&&(this.currentPayment.payment_number=r.nextNumber),{data:r}},async fetchPaymentModes(e){let t=await n.listMethods(e);return this.paymentModes=t.data,{data:t}},async fetchPaymentMode(e){let t=await n.getMethod(e);return this.currentPaymentMode=t.data,{data:t}},async addPaymentMode(e){let t=await n.createMethod(e);return this.paymentModes.push(t.data),{data:t}},async updatePaymentMode(e){let t=await n.updateMethod(e.id,e),r=this.paymentModes.findIndex(e=>e.id===t.data.id);return r!==-1&&(this.paymentModes[r]=t.data),{data:t}},async deletePaymentMode(e){let t=await n.deleteMethod(e),r=this.paymentModes.findIndex(t=>t.id===e);return r!==-1&&this.paymentModes.splice(r,1),{data:t}},selectPayment(e){this.selectedPayments=e,this.selectAllField=this.selectedPayments.length===this.payments.length},selectAllPayments(){this.selectedPayments.length===this.payments.length?(this.selectedPayments=[],this.selectAllField=!1):(this.selectedPayments=this.payments.map(e=>e.id),this.selectAllField=!0)},setSelectAllState(e){this.selectAllField=e},selectNote(e){this.selectedNote=null,this.selectedNote=e},resetSelectedNote(){this.selectedNote=null},async fetchPaymentInitialData(e,t,n){this.isFetchingInitialData=!0;let r=[];e&&t?.id&&r.push(this.fetchPayment(Number(t.id)));try{let[,t,i]=await Promise.all([this.fetchPaymentModes({limit:`all`}),this.getNextNumber(),...r]);if(e){let e=i;e?.data?.data?.invoice&&(this.currentPayment.maxPayableAmount=parseInt(String(e.data.data.invoice.due_amount)))}else if(!e&&t){let e=new Date;this.currentPayment.payment_date=a(e),this.currentPayment.payment_number=t.data.nextNumber,n&&(this.currentPayment.currency=n)}}catch{}finally{this.isFetchingInitialData=!1}}}});function a(e){return`${e.getFullYear()}-${String(e.getMonth()+1).padStart(2,`0`)}-${String(e.getDate()).padStart(2,`0`)}`}export{i as t};