var r=(e,t,n)=>new Promise((o,s)=>{var c=i=>{try{f(n.next(i))}catch(u){s(u)}},d=i=>{try{f(n.throw(i))}catch(u){s(u)}},f=i=>i.done?o(i.value):Promise.resolve(i.value).then(c,d);f((n=n.apply(e,t)).next())});import{cb as h,bf as l,b6 as v}from"./bootstrap-Bqxdt4iG.js";import{c as q,a as y,s as b,v as w}from"./tms-vendors-CoNy1oWa.js";const C=h("tms-vendors",{state:()=>({version:0}),actions:{getStatusColor(e){var t,n;return(n=(t=w.find(o=>o.value===e))==null?void 0:t.color)!=null?n:"default"},getStatusLabel(e){var t,n;return(n=(t=w.find(o=>o.value===e))==null?void 0:t.label)!=null?n:e},getServiceLabel(e){var t,n;return(n=(t=b.find(o=>o.value===e))==null?void 0:t.label)!=null?n:e},getCoverageLabel(e){var t,n;return(n=(t=y.find(o=>o.value===e))==null?void 0:t.label)!=null?n:e},getCountryLabel(e){var t,n;return(n=(t=q.find(o=>o.value===e))==null?void 0:t.label)!=null?n:e},getList(e){return r(this,null,function*(){var u,a;let t=l.from("vendors").select("*",{count:"exact"}).eq("isDelete",!1);if(e!=null&&e.name&&(t=t.ilike("name",`%${e.name}%`)),e!=null&&e.status&&(t=t.eq("status",e.status)),e!=null&&e.service&&(t=t.contains("services",[e.service])),(e==null?void 0:e.isInternal)!==void 0&&(e==null?void 0:e.isInternal)!==null){const g=typeof e.isInternal=="string"?e.isInternal==="true":e.isInternal;t=t.eq("isInternal",g)}t=e!=null&&e.sortBy?t.order(e.sortBy,{ascending:e.sortOrder!=="desc"}):t.order("createdAt",{ascending:!1});const n=(u=e==null?void 0:e.page)!=null?u:1,o=(a=e==null?void 0:e.pageSize)!=null?a:10,s=(n-1)*o,c=s+o-1;t=t.range(s,c);const{data:d,error:f,count:i}=yield t;if(f)throw f;return{items:d!=null?d:[],total:i!=null?i:0,page:n,pageSize:o}})},getDetail(e){return r(this,null,function*(){const{data:t,error:n}=yield l.from("vendors").select("*").eq("id",e).eq("isDelete",!1).single();if(n)throw n;return t})},isCodeTaken(e,t){return r(this,null,function*(){let n=l.from("vendors").select("id",{count:"exact",head:!0}).eq("code",e).eq("isDelete",!1);t&&(n=n.neq("id",t));const{count:o}=yield n;return(o!=null?o:0)>0})},create(e){return r(this,null,function*(){if(e.code&&(yield this.isCodeTaken(e.code)))throw new Error(v("page.tms.vendors.form.codeDuplicate"));const{data:t,error:n}=yield l.from("vendors").insert(e).select().single();if(n||!t)throw n!=null?n:new Error("Create failed");return this.version++,t})},update(e,t){return r(this,null,function*(){if(t.code&&(yield this.isCodeTaken(t.code,e)))throw new Error(v("page.tms.vendors.form.codeDuplicate"));const{data:n,error:o}=yield l.from("vendors").update(t).eq("id",e).select().single();if(o)throw o;return this.version++,n})},remove(e){return r(this,null,function*(){const{error:t}=yield l.from("vendors").update({isDelete:!0}).eq("id",e);if(t)throw t;return this.version++,!0})},restore(e){return r(this,null,function*(){const{error:t}=yield l.from("vendors").update({isDelete:!1}).eq("id",e);if(t)throw t;return this.version++,!0})},hardDelete(e){return r(this,null,function*(){const{error:t}=yield l.from("vendors").delete().eq("id",e);if(t)throw t;return this.version++,!0})},fetchOptions(e){return r(this,null,function*(){let t=l.from("vendors").select("id, name, isInternal").eq("isDelete",!1).eq("status","ACTIVE").order("name",{ascending:!0});e&&(t=t.contains("services",[e]));const{data:n,error:o}=yield t;if(o)throw o;return(n!=null?n:[]).map(s=>{var c;return{label:s.name,value:s.id,isInternal:(c=s.isInternal)!=null?c:!1}})})},fetchAllOptions(){return r(this,null,function*(){const e=l.from("vendors").select("id, name, status, contacts").eq("isDelete",!1).order("name",{ascending:!0}),{data:t,error:n}=yield e;if(n)throw n;return(t!=null?t:[]).map(o=>({label:o.name,value:o.id,vendor:o}))})},invalidate(){this.version++}}});export{C as u};
