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(`users`,{state:()=>({roles:[],users:[],totalUsers:0,currentUser:null,selectAllField:!1,selectedUsers:[],customerList:[],userList:[],userData:{name:``,email:``,password:null,phone:null,companies:[]}}),actions:{resetUserData(){this.userData={name:``,email:``,password:null,phone:null,role:null,companies:[]}},fetchUsers(t){return new Promise((n,i)=>{e.get(`/api/v1/users`,{params:t}).then(e=>{this.users=e.data.data,this.totalUsers=e.data.meta.total,n(e)}).catch(e=>{r(e),i(e)})})},fetchUser(t){return new Promise((n,i)=>{e.get(`/api/v1/users/${t}`).then(e=>{this.userData=e.data.data,this.userData?.companies?.length&&this.userData.companies.forEach((e,t)=>{this.userData.roles.forEach(n=>{n.scope===e.id&&(this.userData.companies[t].role=n.name)})}),n(e)}).catch(e=>{console.log(e),r(e),i(e)})})},fetchRoles(t){return new Promise((t,n)=>{e.get(`/api/v1/roles`).then(e=>{this.roles=e.data.data,t(e)}).catch(e=>{r(e),n(e)})})},addUser(t){return new Promise((i,a)=>{e.post(`/api/v1/users`,t).then(e=>{this.users.push(e.data),n().showNotification({type:`success`,message:o.t(`users.created_message`)}),i(e)}).catch(e=>{r(e),a(e)})})},updateUser(t){return new Promise((i,a)=>{e.put(`/api/v1/users/${t.id}`,t).then(e=>{if(e){let t=this.users.findIndex(t=>t.id===e.data.data.id);this.users[t]=e.data.data}n().showNotification({type:`success`,message:o.t(`users.updated_message`)}),i(e)}).catch(e=>{r(e),a(e)})})},deleteUser(t){let i=n();return new Promise((n,a)=>{e.post(`/api/v1/users/delete`,{users:t.ids}).then(e=>{let r=this.users.findIndex(e=>e.id===t);this.users.splice(r,1),i.showNotification({type:`success`,message:o.t(`users.deleted_message`,1)}),n(e)}).catch(e=>{r(e),a(e)})})},deleteMultipleUsers(){return new Promise((t,i)=>{e.post(`/api/v1/users/delete`,{users:this.selectedUsers}).then(e=>{this.selectedUsers.forEach(e=>{let t=this.users.findIndex(t=>t.id===e.id);this.users.splice(t,1)}),n().showNotification({type:`success`,message:o.t(`users.deleted_message`,2)}),t(e)}).catch(e=>{r(e),i(e)})})},searchUsers(t){return new Promise((n,i)=>{e.get(`/api/v1/search`,{params:t}).then(e=>{this.userList=e.data.users.data,this.customerList=e.data.customers.data,n(e)}).catch(e=>{r(e),i(e)})})},setSelectAllState(e){this.selectAllField=e},selectUser(e){this.selectedUsers=e,this.selectedUsers.length===this.users.length?this.selectAllField=!0:this.selectAllField=!1},selectAllUsers(){this.selectedUsers.length===this.users.length?(this.selectedUsers=[],this.selectAllField=!1):(this.selectedUsers=this.users.map(e=>e.id),this.selectAllField=!0)}}})()};export{i as t};