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