import{bc as oe,bd as p,aO as le,r as f,c as u}from"./index-BUdRbiwj.js";class ie extends oe{constructor(){super("/customers")}async getAll(){try{return(await p.get(this.endpoint)).data}catch(t){throw this.handleError(t)}}async getById(t){try{return(await p.get(`${this.endpoint}/${t}`)).data}catch(a){throw this.handleError(a)}}async create(t){try{return(await p.post(this.endpoint,{name:t.name,code:t.code||"",type:t.type,status:t.status||"active",priority:t.priority||"normal",contactPerson:t.contactPerson||"",email:t.email||"",phone:t.phone||"",alternatePhone:t.alternatePhone||"",location:t.location||"",building:t.building||"",floor:t.floor||"",room:t.room||"",address:t.address||{},department:t.department||"",costCenter:t.costCenter||"",billingCode:t.billingCode||"",slaLevel:t.slaLevel||"standard",responseTime:t.responseTime||24,allowSelfService:t.allowSelfService!==!1,emailNotifications:t.emailNotifications!==!1,smsNotifications:t.smsNotifications||!1,notes:t.notes||"",tags:t.tags||[]})).data}catch(a){throw this.handleError(a)}}async update(t,a){try{return(await p.patch(`${this.endpoint}/${t}`,{name:a.name,code:a.code,type:a.type,status:a.status,priority:a.priority,contactPerson:a.contactPerson,email:a.email,phone:a.phone,alternatePhone:a.alternatePhone,location:a.location,building:a.building,floor:a.floor,room:a.room,address:a.address,department:a.department,costCenter:a.costCenter,billingCode:a.billingCode,slaLevel:a.slaLevel,responseTime:a.responseTime,allowSelfService:a.allowSelfService,emailNotifications:a.emailNotifications,smsNotifications:a.smsNotifications,notes:a.notes,tags:a.tags})).data}catch(o){throw this.handleError(o)}}async delete(t){try{await p.delete(`${this.endpoint}/${t}`)}catch(a){throw this.handleError(a)}}async search(t={}){try{const a=new URLSearchParams;t.search&&a.append("search",t.search),t.type&&a.append("type",t.type),t.status&&a.append("status",t.status),t.priority&&a.append("priority",t.priority),t.department&&a.append("department",t.department),t.location&&a.append("location",t.location),t.slaLevel&&a.append("slaLevel",t.slaLevel),t.sortBy&&a.append("sortBy",t.sortBy),t.sortOrder&&a.append("sortOrder",t.sortOrder),t.page!==void 0&&a.append("page",t.page),t.size!==void 0&&a.append("size",t.size);const o=a.toString()?`${this.endpoint}/search?${a.toString()}`:`${this.endpoint}/search`;return(await p.get(o)).data}catch(a){throw this.handleError(a)}}async getByType(t){try{return(await p.get(`${this.endpoint}/type/${t}`)).data}catch(a){throw this.handleError(a)}}async getByDepartment(t){try{return(await p.get(`${this.endpoint}/department/${encodeURIComponent(t)}`)).data}catch(a){throw this.handleError(a)}}async getByLocation(t){try{return(await p.get(`${this.endpoint}/location/${encodeURIComponent(t)}`)).data}catch(a){throw this.handleError(a)}}async getHighPriority(){try{return(await p.get(`${this.endpoint}/high-priority`)).data}catch(t){throw this.handleError(t)}}async updateStatus(t,a){try{return(await p.patch(`${this.endpoint}/${t}/status`,{status:a})).data}catch(o){throw this.handleError(o)}}async updatePriority(t,a){try{return(await p.patch(`${this.endpoint}/${t}/priority`,{priority:a})).data}catch(o){throw this.handleError(o)}}async updateNotificationSettings(t,a){try{return(await p.patch(`${this.endpoint}/${t}/notifications`,a)).data}catch(o){throw this.handleError(o)}}async bulkUpdate(t){try{return(await p.patch(`${this.endpoint}/bulk`,{updates:t})).data}catch(a){throw this.handleError(a)}}async getStatistics(){try{return(await p.get(`${this.endpoint}/statistics`)).data}catch(t){throw this.handleError(t)}}async validateCode(t,a=null){try{const o=new URLSearchParams;return o.append("code",t),a&&o.append("excludeId",a),(await p.get(`${this.endpoint}/validate-code?${o.toString()}`)).data}catch(o){throw this.handleError(o)}}async getDepartments(){try{return(await p.get(`${this.endpoint}/departments`)).data}catch(t){throw this.handleError(t)}}async getLocations(){try{return(await p.get(`${this.endpoint}/locations`)).data}catch(t){throw this.handleError(t)}}}const h=new ie,T={INTERNAL:"internal",EXTERNAL:"external",TENANT:"tenant",DEPARTMENT:"department",CONTRACTOR:"contractor"},v={ACTIVE:"active",INACTIVE:"inactive",PENDING:"pending",SUSPENDED:"suspended"},C={LOW:"low",NORMAL:"normal",HIGH:"high",CRITICAL:"critical"},ce=le("customers",()=>{const s=f([]),t=f(null),a=f(!1),o=f(null),c=f({}),b=f({page:0,size:20,totalElements:0,totalPages:0}),g=f(null),$=u(()=>s.value.reduce((e,r)=>(e[r.id]=r,e),{})),O=u(()=>s.value.filter(e=>e&&e.status===v.ACTIVE)),H=u(()=>s.value.filter(e=>e&&(e.priority===C.HIGH||e.priority===C.CRITICAL)&&e.status===v.ACTIVE)),R=u(()=>Object.values(T).reduce((e,r)=>(e[r]=s.value.filter(n=>n&&n.type===r),e),{})),B=u(()=>s.value.reduce((e,r)=>(r&&r.department&&(e[r.department]||(e[r.department]=[]),e[r.department].push(r)),e),{})),M=u(()=>s.value.reduce((e,r)=>(r&&r.location&&(e[r.location]||(e[r.location]=[]),e[r.location].push(r)),e),{})),F=u(()=>{const e=s.value.length,r=s.value.filter(i=>i&&i.status===v.ACTIVE).length,n=s.value.filter(i=>i&&i.status===v.INACTIVE).length,l=s.value.filter(i=>i&&i.status===v.PENDING).length,d=s.value.filter(i=>i&&i.status===v.SUSPENDED).length,w=Object.values(T).reduce((i,y)=>(i[y]=s.value.filter(E=>E&&E.type===y).length,i),{}),m=Object.values(C).reduce((i,y)=>(i[y]=s.value.filter(E=>E&&E.priority===y).length,i),{}),S=s.value.reduce((i,y)=>(y&&y.slaLevel&&(i[y.slaLevel]=(i[y.slaLevel]||0)+1),i),{}),I=s.value.filter(i=>i&&i.emailNotifications).length,L=s.value.filter(i=>i&&i.smsNotifications).length,A=s.value.filter(i=>i&&i.allowSelfService).length,N=[...new Set(s.value.map(i=>i&&i.department).filter(Boolean))],P=[...new Set(s.value.map(i=>i&&i.location).filter(Boolean))];return{total:e,active:r,inactive:n,pending:l,suspended:d,byType:w,byPriority:m,slaLevels:S,emailNotifications:I,smsNotifications:L,selfServiceEnabled:A,departments:N.length,locations:P.length}}),x=u(()=>e=>$.value[e]||null),z=u(()=>e=>R.value[e]||[]),V=u(()=>e=>B.value[e]||[]),k=u(()=>e=>M.value[e]||[]),j=u(()=>!c.value||Object.keys(c.value).length===0?s.value:s.value.filter(e=>{var r,n,l,d,w;if(c.value.search){const m=c.value.search.toLowerCase(),S=e.name.toLowerCase().includes(m),I=(r=e.code)==null?void 0:r.toLowerCase().includes(m),L=(n=e.contactPerson)==null?void 0:n.toLowerCase().includes(m),A=(l=e.email)==null?void 0:l.toLowerCase().includes(m),N=(d=e.department)==null?void 0:d.toLowerCase().includes(m),P=(w=e.location)==null?void 0:w.toLowerCase().includes(m);if(!S&&!I&&!L&&!A&&!N&&!P)return!1}return!(c.value.type&&e.type!==c.value.type||c.value.status&&e.status!==c.value.status||c.value.priority&&e.priority!==c.value.priority||c.value.department&&e.department!==c.value.department||c.value.location&&e.location!==c.value.location||c.value.slaLevel&&e.slaLevel!==c.value.slaLevel)})),U=async(e=!1)=>{const n=Date.now();if(!(!e&&g.value&&n-g.value<3e5)){a.value=!0,o.value=null;try{const l=await h.getAll();s.value=l,g.value=n}catch(l){throw o.value=l.message,console.error("Failed to initialize customers:",l),l}finally{a.value=!1}}},Z=async(e={})=>{a.value=!0,o.value=null,c.value={...e};try{const r={...e,page:e.page||0,size:e.size||20},n=await h.search(r);n.content?(s.value=n.content,b.value={page:n.number||0,size:n.size||20,totalElements:n.totalElements||0,totalPages:n.totalPages||0}):s.value=n}catch(r){throw o.value=r.message,console.error("Failed to fetch customers:",r),r}finally{a.value=!1}},G=async e=>{a.value=!0,o.value=null;try{const r=await h.create(e);return s.value.push(r),r}catch(r){throw o.value=r.message,console.error("Failed to create customer:",r),r}finally{a.value=!1}},W=async(e,r)=>{var n;a.value=!0,o.value=null;try{const l=await h.update(e,r),d=s.value.findIndex(w=>w.id===e);return d!==-1&&(s.value[d]=l),((n=t.value)==null?void 0:n.id)===e&&(t.value=l),l}catch(l){throw o.value=l.message,console.error("Failed to update customer:",l),l}finally{a.value=!1}},_=async e=>{var r;a.value=!0,o.value=null;try{await h.delete(e),s.value=s.value.filter(n=>n.id!==e),((r=t.value)==null?void 0:r.id)===e&&(t.value=null)}catch(n){throw o.value=n.message,console.error("Failed to delete customer:",n),n}finally{a.value=!1}},q=async e=>{if(!e){t.value=null;return}const r=x.value(e);if(r)return t.value=r,r;a.value=!0,o.value=null;try{const n=await h.getById(e);return t.value=n,s.value.find(l=>l.id===e)||s.value.push(n),n}catch(n){throw o.value=n.message,console.error("Failed to fetch customer:",n),n}finally{a.value=!1}},J=async(e,r)=>{a.value=!0,o.value=null;try{const n=await h.updateStatus(e,r),l=s.value.findIndex(d=>d.id===e);return l!==-1&&(s.value[l]={...s.value[l],status:r}),n}catch(n){throw o.value=n.message,console.error("Failed to update customer status:",n),n}finally{a.value=!1}},X=async(e,r)=>{a.value=!0,o.value=null;try{const n=await h.updatePriority(e,r),l=s.value.findIndex(d=>d.id===e);return l!==-1&&(s.value[l]={...s.value[l],priority:r}),n}catch(n){throw o.value=n.message,console.error("Failed to update customer priority:",n),n}finally{a.value=!1}},Y=async(e,r)=>{a.value=!0,o.value=null;try{const n=await h.updateNotificationSettings(e,r),l=s.value.findIndex(d=>d.id===e);return l!==-1&&(s.value[l]={...s.value[l],...r}),n}catch(n){throw o.value=n.message,console.error("Failed to update notification settings:",n),n}finally{a.value=!1}},K=async e=>{a.value=!0,o.value=null;try{const r=await h.bulkUpdate(e);return r.forEach(n=>{const l=s.value.findIndex(d=>d.id===n.id);l!==-1&&(s.value[l]=n)}),r}catch(r){throw o.value=r.message,console.error("Failed to bulk update customers:",r),r}finally{a.value=!1}},Q=async(e,r=null)=>{try{return(await h.validateCode(e,r)).isValid}catch(n){return console.error("Failed to validate customer code:",n),!1}},D=async()=>{try{return await h.getDepartments()}catch(e){return console.error("Failed to fetch departments:",e),[]}},ee=async()=>{try{return await h.getLocations()}catch(e){return console.error("Failed to fetch locations:",e),[]}},te=async()=>{g.value=null,await U(!0)},ae=()=>{o.value=null},re=()=>{t.value=null},ne=()=>{s.value=[],t.value=null,g.value=null,c.value={},b.value={page:0,size:20,totalElements:0,totalPages:0}},se=()=>{s.value=[{id:1,customerCode:"CUST-001",name:"ABC Corporation",email:"contact@abc-corp.com",phone:"+1 (555) 123-4567",type:T.EXTERNAL,priority:C.HIGH,status:v.ACTIVE,description:"Large manufacturing company requiring 24/7 support",serviceHours:"24x7",sla:{responseTime:"2h",resolutionTime:"8h",availability:99.9},notifications:{email:!0,sms:!0,phone:!1,portal:!0},primaryContact:{name:"John Smith",title:"Facilities Manager",email:"j.smith@abc-corp.com",phone:"+1 (555) 123-4568"},address:{street:"123 Industrial Blvd",city:"Manufacturing City",state:"CA",zipCode:"90210"},lastContactDate:"2024-01-15T10:30:00Z",lastContactMethod:"Email",createdAt:"2023-06-15T09:00:00Z",updatedAt:"2024-01-15T10:30:00Z"},{id:2,customerCode:"CUST-002",name:"City Health Department",email:"facilities@cityhealth.gov",phone:"+1 (555) 987-6543",type:T.GOVERNMENT,priority:C.MEDIUM,status:v.ACTIVE,description:"Municipal health department with multiple facilities",serviceHours:"business",sla:{responseTime:"4h",resolutionTime:"24h",availability:99.5},notifications:{email:!0,sms:!1,phone:!0,portal:!1},primaryContact:{name:"Sarah Johnson",title:"Operations Director",email:"s.johnson@cityhealth.gov",phone:"+1 (555) 987-6544"},address:{street:"456 Government Plaza",city:"Capital City",state:"CA",zipCode:"90211"},lastContactDate:"2024-01-10T14:15:00Z",lastContactMethod:"Phone",createdAt:"2023-08-20T11:30:00Z",updatedAt:"2024-01-10T14:15:00Z"},{id:3,customerCode:"CUST-003",name:"Tech Innovations LLC",email:"admin@techinnovations.com",phone:"+1 (555) 456-7890",type:T.PARTNER,priority:C.LOW,status:v.INACTIVE,description:"Technology partner requiring occasional maintenance support",serviceHours:"extended",sla:{responseTime:"8h",resolutionTime:"48h",availability:95},notifications:{email:!0,sms:!1,phone:!1,portal:!0},primaryContact:{name:"Mike Chen",title:"IT Manager",email:"m.chen@techinnovations.com",phone:"+1 (555) 456-7891"},address:{street:"789 Tech Park Dr",city:"Silicon Valley",state:"CA",zipCode:"90212"},lastContactDate:"2023-12-05T16:45:00Z",lastContactMethod:"Portal",createdAt:"2023-04-10T13:20:00Z",updatedAt:"2023-12-05T16:45:00Z"}],g.value=Date.now()};return{customers:u(()=>s.value),currentCustomer:u(()=>t.value),loading:u(()=>a.value),error:u(()=>o.value),searchCriteria:u(()=>c.value),pagination:u(()=>b.value),customersById:$,activeCustomers:O,highPriorityCustomers:H,customersByType:R,customersByDepartment:B,customersByLocation:M,customerStats:F,getCustomerById:x,getCustomersByType:z,getCustomersByDepartment:V,getCustomersByLocation:k,filteredCustomers:j,customerTypeOptions:u(()=>Object.values(T).map(e=>({label:e.charAt(0).toUpperCase()+e.slice(1),value:e}))),priorityOptions:u(()=>Object.values(C).map(e=>({label:e.charAt(0).toUpperCase()+e.slice(1),value:e}))),statusOptions:u(()=>Object.values(v).map(e=>({label:e.charAt(0).toUpperCase()+e.slice(1),value:e}))),responseTimeOptions:u(()=>[{label:"1 Hour",value:"1h"},{label:"2 Hours",value:"2h"},{label:"4 Hours",value:"4h"},{label:"8 Hours",value:"8h"},{label:"24 Hours",value:"24h"},{label:"48 Hours",value:"48h"}]),resolutionTimeOptions:u(()=>[{label:"4 Hours",value:"4h"},{label:"8 Hours",value:"8h"},{label:"24 Hours",value:"24h"},{label:"48 Hours",value:"48h"},{label:"72 Hours",value:"72h"},{label:"1 Week",value:"1w"}]),serviceHoursOptions:u(()=>[{label:"24x7",value:"24x7"},{label:"Business Hours (9AM-5PM)",value:"business"},{label:"Extended Hours (7AM-7PM)",value:"extended"},{label:"Custom",value:"custom"}]),totalCustomers:u(()=>s.value.length),activeCustomersCount:u(()=>O.value.length),inactiveCustomersCount:u(()=>s.value.filter(e=>e&&e.status===v.INACTIVE).length),highPriorityCustomersCount:u(()=>H.value.length),averageResponseTime:u(()=>{const e=s.value.map(n=>{var l;return n&&((l=n.sla)==null?void 0:l.responseTime)}).filter(Boolean).map(n=>parseInt(n));if(e.length===0)return null;const r=e.reduce((n,l)=>n+l,0)/e.length;return`${Math.round(r)}h`}),initializeCustomers:U,fetchCustomers:Z,createCustomer:G,updateCustomer:W,deleteCustomer:_,setCurrentCustomer:q,updateCustomerStatus:J,updateCustomerPriority:X,updateNotificationSettings:Y,bulkUpdateCustomers:K,validateCustomerCode:Q,getDepartments:D,getLocations:ee,refreshCustomers:te,clearError:ae,clearCurrentCustomer:re,clearCustomers:ne,loadMockData:se}});export{ce as u};