import{a as b}from"./chunk-SFW4HL5C.js";import{c as L,f as R,j as c,k as N,s,t as y,w as x,x as A}from"./chunk-V67SIXYX.js";import{c as w,d as E}from"./chunk-YADAVUVK.js";import{b as d,i as F}from"./chunk-YKFX4MVT.js";import{$ as p,A as i,G as u,T as m,ka as g,la as v,od as D,qa as S,u as h,v as f,va as l}from"./chunk-HEIW4SFT.js";F();v();E();y();N();D();A();R();var q=(()=>{class a extends b{constructor(e,t){super(),this.api=e,this.translateService=t,this.modelSource=new f(1),this.model$=this.modelSource.asObservable(),this.isLoadingSource=new h(!1),this.isLoading$=this.isLoadingSource.asObservable(),this.isEditMode=!1}fetch(e){if(!w.isSet(e)){this.modelSource.next(null);return}this.isLoadingSource.next(!0),this.api.get(s.humanResources.locationGeofencingDetail(Number(e))).subscribe({next:t=>{this.isEditMode=!0,this.modelSource.next(t),this.isLoadingSource.next(!1)},error:t=>{console.error("Error fetching location",t),this.isLoadingSource.next(!1),this.modelSource.next(null)}})}fetchDefaultData(){this.isEditMode=!1,this.isLoadingSource.next(!0);let e={anQid:0,acLocation:"",acName:"",acAddress:"",acWorkFromHome:"",adDateFrom:new Date,adDateTo:null,acNote:null,geography:null,workFromHome:!1,isActive:!0};this.modelSource.next(e),this.isLoadingSource.next(!1)}buildLocationGeofencingLookupParams(e){let t=new d,r=e?.trim();return r&&(t=t.set("Keywords",r)),t.set("Length","-1").set("SortColumn","acName")}fetchList(e){let t=new d;if(e.keywords&&(t=t.set("Keywords",e.keywords)),e.dateFrom){let r=c.formatDateForApi(e.dateFrom);r&&(t=t.set("DateFrom",r))}if(e.dateTo){let r=c.formatDateForApi(e.dateTo);r&&(t=t.set("DateTo",r))}return e.workObligation&&(t=t.set("WorkObligation",e.workObligation)),t=t.set("Length","-1"),e.SortColumn?t=t.set("SortColumn",e.SortColumn):t=t.set("SortColumn","acName"),e.SortOrder?t=t.set("SortOrder",e.SortOrder):t=t.set("SortOrder","asc"),this.api.get(s.humanResources.locationGeofencing,t).pipe(u(r=>({items:r,totalItems:r.length})))}fetchNeighour(e,t){return this.api.get(s.humanResources.locationGeofencing).pipe(u(r=>{let o=r.findIndex(k=>k.anQid.toString()===e);if(o===-1)throw new Error("Location not found");let n;return t?n=o>0?o-1:r.length-1:n=o<r.length-1?o+1:0,r[n]}))}saveLocation(e){this.isLoadingSource.next(!0);let t=this.mapLocationToSaveApiPayload(e);return(this.isNewLocationRecord(e)?this.api.post(s.humanResources.locationGeofencing,t):this.api.put(s.humanResources.locationGeofencingDetail(e.anQid),t)).pipe(g({next:o=>{o&&this.modelSource.next(o)},error:o=>{this.isNewLocationRecord(e)?console.error("Error creating location:",o):console.error("Error updating location:",o)}}),p(()=>this.isLoadingSource.next(!1)))}delete(e){return this.api.delete(s.humanResources.locationGeofencingDetail(Number(e))).pipe(u(()=>({success:!0,message:this.translateService.instant("LocationDeleteSuccess")})),m(t=>(console.error("Error deleting location:",t),this.locationDeleteErrorResult(e,t))))}mapLocationToSaveApiPayload(e){return{anQid:e.anQid,acLocation:e.acLocation,acName:e.acName,acAddress:e.acAddress,acWorkFromHome:e.acWorkFromHome??"",adDateFrom:c.formatDateForApi(e.adDateFrom),adDateTo:c.formatDateForApi(e.adDateTo),acNote:e.acNote,geography:e.geography,workFromHome:e.workFromHome??!1}}isNewLocationRecord(e){return!e.anQid||e.anQid===0}locationDeleteErrorResult(e,t){switch(t.status){case 404:return i({success:!1,message:this.translateService.instant("LocationDeleteNotFound",{0:e})});case 409:return i({success:!1,message:this.translateService.instant("LocationDeleteInUse")});case 500:default:return i({success:!1,message:t.error||this.translateService.instant("LocationDeleteError")})}}bulkDeleteLocations(e){let t={LocationIds:e};return this.api.deleteWithBody(s.humanResources.locationGeofencingBulkDelete,t).pipe(m(r=>{console.error("Error bulk deleting locations:",r);let o={totalRequested:e.length,successfulDeletes:0,errors:e.map(n=>({locationId:n,errorType:0,errorMessage:r.error?.message||r.message||"Unknown error occurred"}))};return i(o)}))}validateCode(e,t){return!e||e.trim()===""?i(!1):this.api.get(s.humanResources.checkCodeExists(e,t)).pipe(m(r=>(console.error("Error checking location code:",r),i(!1))))}static{this.\u0275fac=function(t){return new(t||a)(l(x),l(L))}}static{this.\u0275prov=S({token:a,factory:a.\u0275fac,providedIn:"root"})}}return a})();export{q as a};
