import{t as e}from"./http-BdoVLsWz.js";import{i as t}from"./vue-router-Bna4o8ws.js";import{n,t as r}from"./notification-BM55Quw3.js";import{t as i}from"./error-handling-CuGxlkPL.js";import{t as a}from"./company-CN0LeovV.js";import{t as o}from"./global-CQwFVtdD.js";var s={name:null,phone:null,address_street_1:null,address_street_2:null,city:null,state:null,country_id:null,zip:null,type:null};function c(){return{name:``,contact_name:``,email:``,phone:null,password:``,confirm_password:``,currency_id:null,website:null,billing:{...s},shipping:{...s},customFields:[],fields:[],enable_portal:!1}}var l=(l=!1)=>{let u=l?window.pinia.defineStore:n,{global:d}=window.i18n;return u(`customer`,{state:()=>({customers:[],totalCustomers:0,selectAllField:!1,selectedCustomers:[],selectedViewCustomer:{},isFetchingInitialSettings:!1,isFetchingViewData:!1,currentCustomer:{...c()},editCustomer:null}),getters:{isEdit:e=>!!e.currentCustomer.id},actions:{resetCurrentCustomer(){this.currentCustomer={...c()}},copyAddress(){this.currentCustomer.shipping={...this.currentCustomer.billing,type:`shipping`}},fetchCustomerInitialSettings(e){let n=t(),r=o(),s=a();this.isFetchingInitialSettings=!0;let c=[];e?c=[this.fetchCustomer(n.params.id)]:this.currentCustomer.currency_id=s.selectedCompanyCurrency.id,Promise.all([r.fetchCurrencies(),r.fetchCountries(),...c]).then(async([e,t,n])=>{this.isFetchingInitialSettings=!1}).catch(e=>{i(e)})},fetchCustomers(t){return new Promise((n,r)=>{e.get(`/api/v1/customers`,{params:t}).then(e=>{this.customers=e.data.data,this.totalCustomers=e.data.meta.customer_total_count,n(e)}).catch(e=>{i(e),r(e)})})},fetchViewCustomer(t){return new Promise((n,r)=>{this.isFetchingViewData=!0,e.get(`/api/v1/customers/${t.id}/stats`,{params:t}).then(e=>{this.selectedViewCustomer={},Object.assign(this.selectedViewCustomer,e.data.data),this.setAddressStub(e.data.data),this.isFetchingViewData=!1,n(e)}).catch(e=>{this.isFetchingViewData=!1,i(e),r(e)})})},fetchCustomer(t){return new Promise((n,r)=>{e.get(`/api/v1/customers/${t}`).then(e=>{Object.assign(this.currentCustomer,e.data.data),this.setAddressStub(e.data.data),n(e)}).catch(e=>{i(e),r(e)})})},addCustomer(t){return new Promise((n,a)=>{e.post(`/api/v1/customers`,t).then(e=>{this.customers.push(e.data.data),r().showNotification({type:`success`,message:d.t(`customers.created_message`)}),n(e)}).catch(e=>{i(e),a(e)})})},updateCustomer(t){return new Promise((n,a)=>{e.put(`/api/v1/customers/${t.id}`,t).then(e=>{if(e.data){let n=this.customers.findIndex(t=>t.id===e.data.data.id);this.customers[n]=t,r().showNotification({type:`success`,message:d.t(`customers.updated_message`)})}n(e)}).catch(e=>{i(e),a(e)})})},deleteCustomer(t){let n=r();return new Promise((r,a)=>{e.post(`/api/v1/customers/delete`,t).then(e=>{let i=this.customers.findIndex(e=>e.id===t);this.customers.splice(i,1),n.showNotification({type:`success`,message:d.t(`customers.deleted_message`,1)}),r(e)}).catch(e=>{i(e),a(e)})})},deleteMultipleCustomers(){let t=r();return new Promise((n,r)=>{e.post(`/api/v1/customers/delete`,{ids:this.selectedCustomers}).then(e=>{this.selectedCustomers.forEach(e=>{let t=this.customers.findIndex(t=>t.id===e.id);this.customers.splice(t,1)}),t.showNotification({type:`success`,message:d.t(`customers.deleted_message`,2)}),n(e)}).catch(e=>{i(e),r(e)})})},setSelectAllState(e){this.selectAllField=e},selectCustomer(e){this.selectedCustomers=e,this.selectedCustomers.length===this.customers.length?this.selectAllField=!0:this.selectAllField=!1},selectAllCustomers(){this.selectedCustomers.length===this.customers.length?(this.selectedCustomers=[],this.selectAllField=!1):(this.selectedCustomers=this.customers.map(e=>e.id),this.selectAllField=!0)},setAddressStub(e){e.billing||(this.currentCustomer.billing={...s}),e.shipping||(this.currentCustomer.shipping={...s})}}})()};export{l as t};