import{a4 as f,s as o,a1 as s,u as r}from"./main-B9dpPbRR.js";const l=(d=!1)=>{const h=d?window.pinia.defineStore:f,{global:n}=window.i18n;return h({id:"category",state:()=>({categories:[],currentCategory:{id:null,name:"",description:""},editCategory:null}),getters:{isEdit:t=>!!t.currentCategory.id},actions:{fetchCategories(t){return new Promise((a,i)=>{o.get("/api/v1/categories",{params:t}).then(e=>{this.categories=e.data.data,a(e)}).catch(e=>{s(e),i(e)})})},fetchCategory(t){return new Promise((a,i)=>{o.get(`/api/v1/categories/${t}`).then(e=>{this.currentCategory=e.data.data,a(e)}).catch(e=>{s(e),i(e)})})},addCategory(t){return new Promise((a,i)=>{o.post("/api/v1/categories",t).then(e=>{this.categories.push(e.data.data),r().showNotification({type:"success",message:n.t("settings.expense_category.created_message")}),a(e)}).catch(e=>{s(e),i(e)})})},updateCategory(t){return new Promise((a,i)=>{o.put(`/api/v1/categories/${t.id}`,t).then(e=>{if(e.data){let c=this.categories.findIndex(u=>u.id===e.data.data.id);this.categories[c]=t.categories,r().showNotification({type:"success",message:n.t("settings.expense_category.updated_message")})}a(e)}).catch(e=>{s(e),i(e)})})},deleteCategory(t){return new Promise(a=>{o.delete(`/api/v1/categories/${t}`).then(i=>{let e=this.categories.findIndex(g=>g.id===t);this.categories.splice(e,1),r().showNotification({type:"success",message:n.t("settings.expense_category.deleted_message")}),a(i)}).catch(i=>{s(i),console.error(i)})})}}})()};export{l as u};
