import{bj as oe,r as d,c as i,aO as se}from"./index-BUdRbiwj.js";const y=new oe;function ce(){const l=d({content:[],totalElements:0,totalPages:0,currentPage:0,pageSize:10}),f=d(null),c=d(!1),o=d(null),u=d({page:0,size:10,direction:"ASC",sortBy:"name",searchValue:"",searchFields:["name","description","address"]}),S=d([]),g=i(()=>l.value.content.length>0),P=i(()=>l.value.totalElements),m=i(()=>l.value.currentPage),z=i(()=>l.value.totalPages),L=async(a={})=>{c.value=!0,o.value=null;try{const n={...u.value,...a},r=await y.searchLocations(n);return l.value=r,r}catch(n){throw o.value=n.message,n}finally{c.value=!1}};return{locations:l,selectedLocation:f,loading:c,error:o,searchCriteria:u,miniLocations:S,hasLocations:g,totalLocations:P,currentPage:m,totalPages:z,searchLocations:L,refreshLocations:async()=>await L(),loadMiniLocations:async()=>{c.value=!0,o.value=null;try{const a=await y.getLocationsMini();return S.value=a,a}catch(a){throw o.value=a.message,a}finally{c.value=!1}},getLocationById:async a=>{c.value=!0,o.value=null;try{const n=await y.getById(a);return f.value=n,n}catch(n){throw o.value=n.message,n}finally{c.value=!1}},createLocation:async a=>{c.value=!0,o.value=null;try{return await y.create(a)}catch(n){throw o.value=n.message,n}finally{c.value=!1}},updateLocation:async(a,n)=>{c.value=!0,o.value=null;try{return await y.update(a,n)}catch(r){throw o.value=r.message,r}finally{c.value=!1}},deleteLocation:async a=>{c.value=!0,o.value=null;try{return await y.delete(a)}catch(n){throw o.value=n.message,n}finally{c.value=!1}},getChildLocations:async a=>{c.value=!0,o.value=null;try{return await y.getChildren(a)}catch(n){throw o.value=n.message,n}finally{c.value=!1}},selectLocation:a=>{f.value=a},clearSelectedLocation:()=>{f.value=null},updateSearchTerm:async a=>(u.value.searchValue=a,u.value.page=0,await L()),changePage:async a=>(u.value.page=a,await L()),changePageSize:async a=>(u.value.size=a,u.value.page=0,await L()),changeSorting:async(a,n)=>(u.value.sortBy=a,u.value.direction=n,u.value.page=0,await L()),validateLocation:a=>(o.value=null,!a.name||a.name.trim().length===0?(o.value="Location name is required",!1):!0),formatLocationName:a=>{if(!a)return"";const n=a.name||"",r=a.address&&a.address.trim()?a.address:"";return r?`${n} (${r})`:n},getLocationDisplayName:a=>{if(!a)return"";const n=[];let r=a;for(;r;)n.unshift(r.name),r=r.parentLocation;return n.join(" > ")},clearError:()=>{o.value=null}}}const re=se("locations",()=>{const{locations:l,selectedLocation:f,loading:c,error:o,searchCriteria:u,miniLocations:S,searchLocations:g,loadMiniLocations:P,refreshLocations:m,updateSearchTerm:z,changePage:L,changePageSize:I,changeSorting:j,getLocationById:E,createLocation:B,updateLocation:F,deleteLocation:N,getChildLocations:R,selectLocation:C,clearSelectedLocation:w,validateLocation:H,formatLocationName:V,getLocationDisplayName:D,clearError:x}=ce(),h=d([]),v=d({type:"",status:"all",searchTerm:""}),p=d(!1),M=d("create"),a=d({}),n=i(()=>l.value.content.filter(e=>e.status!=="inactive")),r=i(()=>l.value.content.filter(e=>e.status==="inactive")),b=i(()=>{const e={};return l.value.content.forEach(t=>{const s=t.type||"General";e[s]||(e[s]=[]),e[s].push(t)}),e}),W=i(()=>{let e=l.value.content;if(v.value.type&&(e=e.filter(t=>t.type===v.value.type)),v.value.status==="active"?e=e.filter(t=>t.status!=="inactive"):v.value.status==="inactive"&&(e=e.filter(t=>t.status==="inactive")),v.value.searchTerm){const t=v.value.searchTerm.toLowerCase();e=e.filter(s=>{var T,G,K;return((T=s.name)==null?void 0:T.toLowerCase().includes(t))||((G=s.description)==null?void 0:G.toLowerCase().includes(t))||((K=s.address)==null?void 0:K.toLowerCase().includes(t))})}return e}),J=i(()=>({total:l.value.totalElements,active:n.value.length,inactive:r.value.length,byType:Object.keys(b.value).reduce((e,t)=>(e[t]=b.value[t].length,e),{})})),O=async()=>{try{await Promise.all([g(),P()])}catch(e){console.error("Failed to initialize locations:",e)}},$=async(e={})=>{const t={...e,searchValue:v.value.searchTerm};return await g(t)},Q=async e=>(v.value={...v.value,...e},await $()),U=async e=>{const t=await B(e);return await m(),t},X=async(e,t)=>{const s=await F(e,t);return await A(s),s},Y=async e=>{const t=await N(e);return await m(),t},Z=(e=null,t="create")=>{e?(C(e),M.value=t):(w(),M.value="create"),p.value=!0},_=()=>{p.value=!1,w()},A=e=>{if(!e)return;const t=h.value.findIndex(s=>s.id===e.id);t!==-1&&h.value.splice(t,1),h.value.unshift(e),h.value.length>10&&(h.value=h.value.slice(0,10))},ee=async e=>{const t=l.value.content.find(T=>T.id===e);if(t)return C(t),t;const s=await E(e);return await A(s),s},k=async e=>{const t=await R(e);a.value[e]=t;for(const s of t)s.hasChildren&&await k(s.id);return t},q=()=>{v.value={type:"",status:"all",searchTerm:""}},ae=()=>{q(),w(),x(),p.value=!1,h.value=[],a.value={}},te=async()=>await O(),ne=i(()=>l.value.totalElements||0);return{locations:l,selectedLocation:f,loading:c,error:o,searchCriteria:u,miniLocations:S,recentLocations:i(()=>h.value),locationFilters:i(()=>v.value),showLocationModal:i(()=>p.value),locationModalMode:i(()=>M.value),locationHierarchy:i(()=>a.value),activeLocations:n,inactiveLocations:r,locationsByType:b,filteredLocations:W,locationStats:J,initializeLocations:O,searchWithFilters:$,updateFilters:Q,createLocationAndRefresh:U,updateLocationAndRefresh:X,deleteLocationAndRefresh:Y,openLocationModal:Z,closeLocationModal:_,addToRecentLocations:A,getLocation:ee,buildLocationHierarchy:k,clearFilters:q,resetStore:ae,searchLocations:g,loadMiniLocations:P,refreshLocations:m,updateSearchTerm:z,changePage:L,changePageSize:I,changeSorting:j,getLocationById:E,createLocation:B,updateLocation:F,deleteLocation:N,getChildLocations:R,selectLocation:C,clearSelectedLocation:w,validateLocation:H,formatLocationName:V,getLocationDisplayName:D,clearError:x,loadLocations:te,locationCount:ne}}),ie=re;export{re as a,ie as u};