"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const F=require("@aplus-frontend/antdv/es/theme/internal"),c=require("lodash-unified"),s=require("vue");require("../../config-provider/index.js");const q=require("../../utils/index.js"),M=require("../components/setting/utils.js"),J=require("../context.js"),P=require("../utils.js"),U=require("../../config-provider/hooks/use-locale.js"),W=require("../../config-provider/hooks/use-namespace.js");function C(r,e,n,a){return a?a.fixed:e?e.fixed:n&&"fixed"in n?n.fixed:c.isBoolean(r.fixed)?r.fixed?"left":void 0:r.fixed}const b=Number.MAX_SAFE_INTEGER,y=q.randomStr();function G(r,e){const n=c.isNumber(r.order)?r.order:b;let a=c.isNumber(e.order)?e.order:b;return r.fixed==="right"&&e.fixed!=="right"&&(a=n-1),e.fixed==="left"&&r.fixed!=="left"&&(a=n-1),n-a}function R(r,e){e&&r.removeItem(e)}function k(r,e){if(!e)return[];const n=r.getItem(e);if(!n)return[];const a=JSON.parse(n);return Array.isArray(a)?a:(R(r,e),[])}function A(r,e){if(c.isObject(r)&&c.isObject(e))return c.mergeWith({},r,e,A)}const X=(r,e,n)=>{const{columnsBackup:a,size:m}=J.useInjectApTable(),{t:N}=U.useLocale(),{be:I}=W.useNamespace("ap-table"),[$,z]=F.useToken(),v=s.ref(),u=s.ref([]);let T;T=s.watch(()=>a?.value,(t,i)=>{t?.length&&!i?.length&&(u.value=p(k(localStorage,s.unref(e))),T?.())},{immediate:!0}),s.watch([a,m],([t,i])=>{t&&(v.value={label:"basic_template",columnState:f(t),key:y,size:i})},{immediate:!0,deep:!0}),s.watch(()=>e.value,t=>{const i=p(k(localStorage,t));u.value=i,v.value={label:"basic_template",columnState:f(a?.value||[]),key:y,size:m?.value},n(i??[])});function p(t){return t.length?t.map(i=>({...i,columnState:f(a.value,i.columnState,!1)})):[{label:N("ap.apTable.setting.defaultTemplate"),key:r.getActiveKey?.("DEFAULT_TEMPLATE")??q.randomStr(),size:m?.value,columnState:f(a?.value||[])}]}function f(t,i={},l=!0){const g=c.mergeWith({},l?r.defaultValue||{}:{},i,A);function S(w,L,j){return w.map((o,B)=>{const x=L?.[o.key]||{},d={key:o.key,label:c.isString(o.title)?P.getTableTitle(o,I("table-header","title"),void 0,z.value.colorTextTertiary):o.title,show:!0,fixed:C(o,i[o.key],l?r.defaultValue?.[o.key]:void 0,j),disabled:!1,...c.omit(x,["children","fixed"])};return d.children=o.children?.length?S(o.children,x.children,d):void 0,d.order=d.order??B,d}).sort(G)}return S(t||[],g)}function h(t){if(s.unref(e)){const i=t.map(l=>({...l,columnState:M.getColumnStateString(l.columnState)}));localStorage.setItem(s.unref(e),JSON.stringify(i))}}function E(t){u.value.push(t)}function O(t){const i=u.value.findIndex(l=>l.key===t);u.value.splice(i,1)}function _(t,i){const l=u.value.findIndex(g=>g.key===t);return u.value[l]={...u.value[l],...i},u.value[l]}return s.watch(()=>u.value,t=>{t.length&&h(t)},{deep:!0,immediate:!0}),{basicTemplate:v,customizeTemplateList:u,syncTemplateListToStorage:h,addTemplate:E,deleteTemplate:O,updateTemplate:_}};exports.useTableTemplateStorage=X;
