import{a as C}from"./chunk-THKS2M3R.js";import{g as T}from"./chunk-RQCMKSAN.js";import{h as S}from"./chunk-NMMBNW5X.js";import{m as a,n as w}from"./chunk-GJIGZB63.js";import{b as g}from"./chunk-5VDVFGVW.js";import{B as d,g as A,ka as D,m as h,pa as m,r as f}from"./chunk-J476KISY.js";var L=(()=>{class l extends C{get model$(){return this._model$}get isEditMode(){return!this._isCreateMode&&g.isSet(this.model?.delivery)}setCreateMode(e){this._isCreateMode=e}constructor(e,t){super(),this.apiService=e,this.dialogService=t,this.model=null,this.modelS=new f(1),this._model$=this.modelS.asObservable(),this.selectedDeliveryMethods=[],this.currentView="",this.currentViewComponent=null,this._isCreateMode=!1}setCurrentView(e,t){this.currentView=e,this.currentViewComponent=t}saveCurrentView(){return this.currentView==="delivery-methods-data"&&this.currentViewComponent?this.currentViewComponent.saveDeliveryMethod():null}unset(){this.formGroup=void 0}unsetModel(){this.model=null,this.modelS.next(null)}fetch(e){this.apiService.get(a.deliveryMethods.getById(e)).subscribe({next:t=>{this.formGroup=new S([]);let i={delivery:t.acDelivery||t.AcDelivery,name:t.acName||t.AcName,transport:t.acTransport||t.AcTransport,active:t.acActive==="T"||t.AcActive==="T",anQid:t.anQid||t.AnQid};this.model=i,this.modelS.next(i)}})}fetchDefaultData(){let e={delivery:"",name:"",transport:"",active:!0};this.model=e,this.modelS.next(e)}saveDeliveryMethod(e){return A(this,null,function*(){if(this._isCreateMode)try{let t={AcDelivery:e.delivery,AcName:e.name,AcTransport:e.transport||"",AcActive:e.active?"T":"F"},i=yield d(this.apiService.post(a.deliveryMethods.create,t)),r=yield d(this.apiService.get(a.deliveryMethods.getById(i))),n={delivery:r.acDelivery||r.AcDelivery,name:r.acName||r.AcName,transport:r.acTransport||r.AcTransport,active:r.acActive==="T"||r.AcActive==="T",anQid:r.anQid||r.AnQid};return this.model=n,this.modelS.next(n),this._isCreateMode=!1,n}catch(t){throw t}else{let t={AcDelivery:e.delivery,AcName:e.name,AcTransport:e.transport||"",AcActive:e.active?"T":"F"},i=yield d(this.apiService.post(a.deliveryMethods.update,t)),r={delivery:i.acDelivery||i.AcDelivery,name:i.acName||i.AcName,transport:i.acTransport||i.AcTransport,active:i.acActive==="T"||i.AcActive==="T",anQid:i.anQid||i.AnQid};return this.model=r,this.modelS.next(r),r}})}fetchNeighour(e,t){return new h(i=>{this.fetchList({}).subscribe({next:r=>{let n=r.items||[],c=n.findIndex(s=>s.delivery===e);if(c===-1){i.next(null),i.complete();return}let o=t?c-1:c+1;if(o<0||o>=n.length){i.next(null),i.complete();return}i.next(n[o]),i.complete()},error:r=>{i.error(r)}})})}fetchList(e){return new h(t=>{this.apiService.get(a.deliveryMethods.list).subscribe({next:i=>{let r=i.map(s=>({delivery:s.acDelivery||s.AcDelivery,name:s.acName||s.AcName,transport:s.acTransport||s.AcTransport,active:s.acActive==="T"||s.AcActive==="T",anQid:s.anQid||s.AnQid}));if(e.search){let s=e.search.toLowerCase();r=r.filter(u=>u.delivery?.toLowerCase().includes(s)||u.name?.toLowerCase().includes(s)||u.transport?.toLowerCase().includes(s))}e.status&&e.status!=="all"&&(r=r.filter(s=>e.status==="active"?s.active===!0:e.status==="inactive"?s.active===!1:!0)),e.sortColumn&&e.sortOrder&&r.sort((s,u)=>{let v=s[e.sortColumn],p=u[e.sortColumn],y=e.sortOrder==="asc"?1:-1;return v<p?-1*y:v>p?1*y:0});let n=e.start||0,c=e.length&&e.length>=r.length?e.length:r.length,o=r.slice(n,n+c);t.next({items:o,totalItems:r.length}),t.complete()},error:i=>t.error(i)})})}deleteDeliveryMethod(e){return this.apiService.post(a.deliveryMethods.remove,{Id:e})}deleteMultipleDeliveryMethods(e){let t={totalRequested:e.length,successfulDeletes:0,errors:[]};return new h(i=>{let r=n=>{if(n>=e.length){i.next(t),i.complete();return}let c=e[n];this.deleteDeliveryMethod(c).subscribe({next:o=>{o&&(o.isDeleted===!0||o.IsDeleted===!0||o.success===!0)?t.successfulDeletes++:t.errors.push({deliveryMethodId:c,errorType:0,errorMessage:o?.error||o?.Error||o?.message||"UnknownError"}),r(n+1)},error:o=>{t.errors.push({deliveryMethodId:c,errorType:1,errorMessage:o?.message||"NetworkError"}),r(n+1)}})};r(0)})}showErrorDialog(e){this.dialogService.open({title:"Error",text:e||"GeneralError",contentStyleClass:"paw-offset"})}static{this.\u0275fac=function(t){return new(t||l)(m(w),m(T))}}static{this.\u0275prov=D({token:l,factory:l.\u0275fac,providedIn:"root"})}}return l})();export{L as a};
