import{t as e}from"./http-BrugN6o-.js";import{n as t,t as n}from"./notification-BM55Quw3.js";import{t as r}from"./error-handling-CRDNZ9kr.js";var i=(i=!1)=>{let a=i?window.pinia.defineStore:t,{global:o}=window.i18n;return a(`item`,{state:()=>({items:[],totalItems:0,selectAllField:!1,selectedItems:[],itemUnits:[],currentItemUnit:{id:null,name:``},currentItem:{name:``,description:``,price:0,unit_id:``,unit:null,taxes:[],tax_per_item:!1}}),getters:{isItemUnitEdit:e=>!!e.currentItemUnit.id},actions:{resetCurrentItem(){this.currentItem={name:``,description:``,price:0,unit_id:``,unit:null,taxes:[]}},fetchItems(t){return new Promise((n,i)=>{e.get(`/api/v1/items`,{params:t}).then(e=>{this.items=e.data.data,this.totalItems=e.data.meta.item_total_count,n(e)}).catch(e=>{r(e),i(e)})})},fetchItem(t){return new Promise((n,i)=>{e.get(`/api/v1/items/${t}`).then(e=>{e.data&&Object.assign(this.currentItem,e.data.data),n(e)}).catch(e=>{r(e),i(e)})})},addItem(t){return new Promise((i,a)=>{e.post(`/api/v1/items`,t).then(e=>{let t=n();this.items.push(e.data.data),t.showNotification({type:`success`,message:o.t(`items.created_message`)}),i(e)}).catch(e=>{r(e),a(e)})})},updateItem(t){return new Promise((i,a)=>{e.put(`/api/v1/items/${t.id}`,t).then(e=>{if(e.data){let r=n(),i=this.items.findIndex(t=>t.id===e.data.data.id);this.items[i]=t.item,r.showNotification({type:`success`,message:o.t(`items.updated_message`)})}i(e)}).catch(e=>{r(e),a(e)})})},deleteItem(t){let i=n();return new Promise((n,a)=>{e.post(`/api/v1/items/delete`,t).then(e=>{let r=this.items.findIndex(e=>e.id===t);this.items.splice(r,1),i.showNotification({type:`success`,message:o.t(`items.deleted_message`,1)}),n(e)}).catch(e=>{r(e),a(e)})})},deleteMultipleItems(){let t=n();return new Promise((n,i)=>{e.post(`/api/v1/items/delete`,{ids:this.selectedItems}).then(e=>{this.selectedItems.forEach(e=>{let t=this.items.findIndex(t=>t.id===e.id);this.items.splice(t,1)}),t.showNotification({type:`success`,message:o.t(`items.deleted_message`,2)}),n(e)}).catch(e=>{r(e),i(e)})})},selectItem(e){this.selectedItems=e,this.selectedItems.length===this.items.length?this.selectAllField=!0:this.selectAllField=!1},selectAllItems(e){this.selectedItems.length===this.items.length?(this.selectedItems=[],this.selectAllField=!1):(this.selectedItems=this.items.map(e=>e.id),this.selectAllField=!0)},addItemUnit(t){let i=n();return new Promise((n,a)=>{e.post(`/api/v1/units`,t).then(e=>{this.itemUnits.push(e.data.data),e.data.data&&i.showNotification({type:`success`,message:o.t(`settings.customization.items.item_unit_added`)}),e.data.errors&&i.showNotification({type:`error`,message:err.response.data.errors[0]}),n(e)}).catch(e=>{r(e),a(e)})})},updateItemUnit(t){let i=n();return new Promise((n,a)=>{e.put(`/api/v1/units/${t.id}`,t).then(e=>{let r=this.itemUnits.findIndex(t=>t.id===e.data.data.id);this.itemUnits[r]=t,e.data.data&&i.showNotification({type:`success`,message:o.t(`settings.customization.items.item_unit_updated`)}),e.data.errors&&i.showNotification({type:`error`,message:err.response.data.errors[0]}),n(e)}).catch(e=>{r(e),a(e)})})},fetchItemUnits(t){return new Promise((n,i)=>{e.get(`/api/v1/units`,{params:t}).then(e=>{this.itemUnits=e.data.data,n(e)}).catch(e=>{r(e),i(e)})})},fetchItemUnit(t){return new Promise((n,i)=>{e.get(`/api/v1/units/${t}`).then(e=>{this.currentItemUnit=e.data.data,n(e)}).catch(e=>{r(e),i(e)})})},deleteItemUnit(t){let i=n();return new Promise((n,a)=>{e.delete(`/api/v1/units/${t}`).then(e=>{if(!e.data.error){let e=this.itemUnits.findIndex(e=>e.id===t);this.itemUnits.splice(e,1)}e.data.success&&i.showNotification({type:`success`,message:o.t(`settings.customization.items.deleted_message`)}),n(e)}).catch(e=>{r(e),a(e)})})}}})()};export{i as t};