import{g as d,d as h,r as g,t as u,u as p,a as y,b as f,c as m,e as P,f as A,h as w}from"./pagesApi-bmNhyO8R.js";import{a8 as E,ai as o,ab as s,ak as b}from"./main-CJbVOHh0.js";import{c as v}from"./cacheManager-C9fWw59Z.js";const i=v("pages",10*60*1e3),U=E("pages",{state:()=>({pages:[],templates:[],page:null,loading:!1,saving:!1,deleting:!1,translating:!1,isLoadingUpdatePreview:!1,error:null,total:0}),actions:{async addPage(r){this.loading=!0,this.error=null;try{const e=await w(r);return i.clearAll(),e.data.status===!0&&s("success","Success",e.data.message||"Page created successfully"),e.data}catch(e){throw this.error=o(e),s("error","Error",this.error),e}finally{this.loading=!1}},async fetchPages({page:r=1,rows:e=10,sortField:a="",sortOrder:t="",search:c="",...n}={}){this.loading=!0,this.error=null;try{const l=await A({page:r,per_page:e,sort_by:a,sort_order:t===1?"asc":"desc",search:c,...n});l.data.data&&Array.isArray(l.data.data.data)?(this.pages=l.data.data.data,this.total=l.data.data.total):l.data.data&&Array.isArray(l.data.data)?(this.pages=l.data.data,this.total=l.data.total||l.data.data.length):Array.isArray(l.data)?(this.pages=l.data,this.total=l.data.length):(console.warn("Unexpected response structure:",l.data),this.pages=[],this.total=0),i.clearExpired()}catch(l){this.error=o(l),s("error","Error",this.error)}finally{this.loading=!1}},async fetchPageById(r){var e,a;this.loading=!0,this.error=null;try{const t=await P(r);this.page=t.data.data,i.clearExpired()}catch(t){this.error=((a=(e=t.response)==null?void 0:e.data)==null?void 0:a.message)||t.message}finally{this.loading=!1}},async fetchPageUpdatePreview(r,e="en"){this.isLoadingUpdatePreview=!0,this.error=null;try{return(await m(r,e)).data}catch(a){throw this.error=o(a),a}finally{this.isLoadingUpdatePreview=!1}},async acceptGlobalPageUpdate(r,e){var a;this.loading=!0,this.error=null;try{const t=await f(r,{language_code:e,action_type:"accept_global_update"});return i.clearAll(),((a=t.data)==null?void 0:a.status)===!0&&s("success","Success",t.data.message||"Latest global update applied."),t.data}catch(t){throw this.error=o(t),s("error","Error",this.error),t}finally{this.loading=!1}},async resetFranchisePageToGlobal(r,e){var a;this.loading=!0,this.error=null;try{const t=await y(r,{language_code:e,action_type:"reset_to_global"});return i.clearAll(),((a=t.data)==null?void 0:a.status)===!0&&s("success","Success",t.data.message||"Page reset to global successfully."),t.data}catch(t){throw this.error=o(t),s("error","Error",this.error),t}finally{this.loading=!1}},async updatePage(r,e){this.loading=!0,this.error=null;try{const a=await p(e,r);return i.clearAll(),a.data.status===!0&&s("success","Success",a.data.message||"Page updated successfully"),a.data}catch(a){throw this.error=o(a),s("error","Error",this.error),a}finally{this.loading=!1}},async translatePage(r){this.translating=!0,this.error=null;try{const e=await u(r);return i.clearAll(),e.data.status===!0&&s("success","Success",e.data.message||"Page translated successfully"),e.data}catch(e){throw this.error=o(e),s("error","Error",this.error),e}finally{this.translating=!1}},async removePageTranslation(r,e){this.loading=!0,this.error=null;try{const a=await g(r,e);return i.clearAll(),s("success","Success",a.data.message||"Translation removed successfully"),a.data}catch(a){throw this.error=o(a),s("error","Error",this.error),a}finally{this.loading=!1}},async deletePageById(r){var e;this.deleting=!0,this.error=null;try{const a=await h(r);return this.pages=b(this.pages,r),this.total--,((e=this.page)==null?void 0:e.id)==r&&(this.page=null),i.clearAll(),s("success","Deleted","Page deleted successfully"),{status:!0,message:"Page deleted successfully",data:a.data}}catch(a){return this.error=o(a),s("error","Error",this.error),{status:!1,message:this.error}}finally{this.deleting=!1}},async fetchTemplates(){const r=i.generateKey({type:"templates"}),e=i.load(r);if(e)return this.templates=e,{success:!0,fromCache:!0};this.loading=!0,this.error=null;try{const a=await d();this.templates=a.data.data.map(t=>({id:t.id,slug:t.slug,name:t.name})),i.save(r,this.templates),i.clearExpired()}catch(a){this.error=o(a),s("error","Error",this.error)}finally{this.loading=!1}}}});export{U as u};
