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