import{a as u,b as p}from"./chunk-FBYATKKO.js";import{a,c as v,e as D,f as F,g as T,h as Y}from"./chunk-LJRETJNY.js";import{a as b}from"./chunk-SQTC4ZEW.js";import{Ea as U,ya as A}from"./chunk-TV2MZFBF.js";import{b as x,d as w}from"./chunk-R5GBW5HQ.js";import{$ as y,Nc as S,S as h,W as E,l,na as c,pd as I,qa as C,va as L,ya as f,za as m}from"./chunk-3NIPUOIR.js";var j=(()=>{class t{constructor(r,o){this.httpClient=r,this.commonHttpErrorService=o}delete(r){let o=`category/${r}`;return this.httpClient.delete(o)}update(r){let o=`category/${r.id}`;return this.httpClient.put(o,r)}add(r){return this.httpClient.post("category",r).pipe(h(this.commonHttpErrorService.handleError))}getAllCategoriesForDropDown(){return this.httpClient.get("category/dropdown")}static{this.\u0275fac=function(o){return new(o||t)(f(I),f(x))}}static{this.\u0275prov=L({token:t,factory:t.\u0275fac,providedIn:"root"})}}return t})();var V={categories:[],childCategories:[],isAddUpdate:!1,loadList:!0,commonError:null,currentCategoryId:null},W=v({providedIn:"root"},Y(V),D(t=>({rootCategories:S(()=>t.categories().filter(d=>d.parentId===null))})),T((t,d=m(j),r=m(A),o=m(U))=>({loadByCategory:p(l(E(300),c(()=>d.getAllCategoriesForDropDown().pipe(u({next:i=>{let e=[];g(e,[...i]),a(t,{categories:[...e],commonError:null,loadList:!1})},error:i=>{a(t,{commonError:i})}}))))),loadbyChildCategory:p(l(C(i=>{a(t,{childCategories:t.categories().filter(e=>e.parentId===i)})}))),addCategory:p(l(C(()=>a(t,{loadList:!1,currentCategoryId:null})),c(i=>d.add(i).pipe(u({next:e=>{r.success(o.getValue("CATEGORY_SAVE_SUCCESSFULLY")),a(t,{isAddUpdate:!0,loadList:!0,currentCategoryId:e.id})},error:e=>{a(t,{commonError:e})}}))))),updateCategory:p(l(c(i=>d.update(i).pipe(u({next:e=>{let n=e;if(r.success(o.getValue("CATEGORY_SAVE_SUCCESSFULLY")),a(t,{isAddUpdate:!0,categories:t.categories().map(s=>n.id===s.id?n:s)}),n.parentId){let s=[...t.categories()];g([],s),a(t,{childCategories:s.filter(H=>H.parentId===n.parentId)})}},error:e=>{a(t,{commonError:e})}}))))),deleteCategoryById:p(l(y(),C(()=>a(t,{loadList:!1})),c(i=>d.delete(i).pipe(u({next:()=>{r.success(o.getValue("CATEGORY_DELETED_SUCCESSFULLY")),a(t,{loadList:!0})},error:e=>{a(t,{commonError:e})}}))))),resetFlag(){a(t,{isAddUpdate:!1})}})),F({onInit(t,d=m(w)){b(t.loadList).subscribe(r=>{r&&t.loadByCategory()}),d.isLogin()&&t.loadByCategory()}}));function g(t,d,r,o){let i=d.filter(e=>e.parentId==o);return i.length>0&&i.map((e,n)=>{e.deafLevel=r?r.deafLevel+1:0,e.index=(r?r.index:0)+n*Math.pow(.1,e.deafLevel),t.push(e),g(t,d,e,e.id)}),r}export{j as a,W as b};
