import { jsxs as j, Fragment as Te, jsx as l } from "react/jsx-runtime";
import { useState as c, useRef as B, useEffect as z } from "react";
import { isEqual as H } from "es-toolkit/predicate";
import { isEmpty as V, mapValues as He } from "es-toolkit/compat";
import { noop as S } from "es-toolkit/function";
import { arrayMove as Le } from "@dnd-kit/sortable";
import Fe from "../../../utils/classNames.js";
import Ie from "../../dialog/Dialog.js";
import Ae from "../../clearableInput/ClearableInput.js";
import { TableSettingsDialogFooter as Me } from "./TableSettingsDialogFooter.js";
import { TableSettingsListContainer as Ee } from "./TableSettingsListContainer.js";
import { filterColumns as Je } from "./TableSettingsListItem.js";
const O = 0, _e = 1e3, $e = (L) => {
  const {
    show: i = !1,
    title: D,
    subtitle: m,
    className: a,
    defaultColumnOrder: h = [],
    defaultHiddenColumns: d = [],
    defaultColumnDetails: F = {},
    columnOrder: x = [],
    hiddenColumns: v = [],
    columnLabels: P = {},
    disabledColumns: G = [],
    columnsDetails: y = {},
    autoLabel: Q = "",
    applyButtonText: Y,
    cancelButtonText: Z,
    closeButtonText: $,
    resetButtonText: ee,
    onColumnChange: b = S,
    onColumnDetailsChange: U = S,
    onDiscard: te = S,
    onApply: ne = S,
    onHide: W,
    columnSearchValue: I = "",
    onSearchChange: w = S,
    searchPlaceholder: se,
    notFoundMessage: oe = "",
    immediateChange: g = !1
  } = L, [A, N] = c(I), [C, R] = c(x), [f, M] = c(v || d), [u, T] = c(y), [k, le] = c({}), [q, re] = c({}), [ie, p] = c(!1), [ae, E] = c(!1), [Ve, J] = c(!1), _ = B(F), K = B(i);
  z(() => {
    (i && !K.current || !i) && N(I), R(x), M(v), T(y);
    const s = x ?? h, n = v ?? d, o = y ?? {}, r = !H(s, h) || !H(n, d) || de(o);
    p(r), K.current = i;
  }, [
    I,
    JSON.stringify(x),
    JSON.stringify(v),
    JSON.stringify(y),
    JSON.stringify(h),
    JSON.stringify(d),
    i
  ]);
  const de = (e) => {
    const t = (n) => {
      if (!n)
        return O;
      const { width: o, defaultWidth: r } = n;
      return Number.isFinite(o) ? o : Number.isFinite(r) ? r : O;
    }, s = (n) => Number.isFinite(n?.defaultWidth) ? n?.defaultWidth : O;
    return V(_.current) ? Object.values(e).some(
      (n) => t(n) !== s(n)
    ) : Object.keys(_.current).some((n) => {
      const o = _.current[n], r = e[n];
      return t(r) !== t(o);
    });
  }, ue = () => {
    J(!1);
  }, ce = (e, t, s) => {
    const n = C.filter((o) => o !== e);
    n.splice(t, 0, e), R(n), J(s ? !!e : !1), p(!0), g && b(n, f), setTimeout(ue, 500);
  }, me = () => E(!0), he = () => E(!1), Ce = (e) => V(F) ? He(e, (t) => {
    if (Number.isFinite(t.defaultWidth)) {
      const r = t.defaultWidth;
      return { ...t, width: r };
    }
    const { width: s, defaultWidth: n, ...o } = t;
    return o;
  }) : F, fe = () => {
    const e = Ce(u);
    R(h), M(d), N(""), p(!1), E(!1), V(u) || T(e), g && (w(""), b(h, d, e));
  }, ge = () => {
    w(""), te(), W();
  }, pe = () => {
    N(""), w(""), b(C, f, u), ne(C, f, u), W();
  }, De = (e) => {
    const s = f.includes(e) ? f.filter((n) => n !== e) : [...f, e];
    M(s), p(!0), g && b(C, s);
  }, be = (e) => {
    const t = e.toLowerCase();
    N(() => (w(t), t));
  }, Se = (e, t) => {
    const s = u[e], n = s ? { ...s, width: t } : {
      width: t,
      defaultWidth: O,
      maxWidth: _e
    }, o = { ...u, [e]: n };
    T(o), p(!0), g && U(e, n);
  }, Oe = (e) => {
    const t = u[e];
    if (!t)
      return;
    const s = {
      ...t,
      width: t.defaultWidth ?? O
    }, n = { ...u, [e]: s };
    T(n), g && U(e, s);
  }, xe = (e) => {
    const t = { ...k };
    t[e] ? delete t[e] : t[e] = e, le(t);
  }, ve = (e, t) => {
    const { active: s, over: n } = e, o = s.id, r = n?.id;
    if (o === r)
      return;
    const Ne = t.indexOf(String(o)), Re = t.indexOf(String(r)), X = Le(t, Ne, Re);
    R(X), J(!0), p(!0), g && b(X, f);
  }, ye = () => {
    const e = {
      columnLabels: P,
      autoLabel: Q,
      disabledColumns: G,
      columnOrder: C,
      hiddenColumns: f,
      columnSearchValue: A,
      columnsDetails: u,
      columnLabelStrings: q,
      openColumnsDetails: k,
      onMoveColumn: ce,
      onOpenDetails: xe,
      onColumnWidthChange: Se,
      onResetColumnWidth: Oe,
      onToggleHideColumn: De
    }, t = C.filter(
      (n) => Je(A, n, q)
    ), s = !H(t, C);
    return /* @__PURE__ */ j("div", { children: [
      /* @__PURE__ */ l("div", { className: "table-settings-search", children: /* @__PURE__ */ j("div", { className: "input-group width-100pct", children: [
        /* @__PURE__ */ l("span", { className: "input-group-addon", children: /* @__PURE__ */ l("span", { className: "rioglyph rioglyph-search" }) }),
        /* @__PURE__ */ l(
          Ae,
          {
            value: A,
            onChange: be,
            placeholder: se
          }
        )
      ] }) }),
      /* @__PURE__ */ l("div", { className: "table-settings-body", children: s ? /* @__PURE__ */ l(
        Ee,
        {
          items: C,
          onSortEnd: ve,
          itemProps: { ...e }
        }
      ) : /* @__PURE__ */ l("div", { className: "text-center text-color-gray", children: oe }) })
    ] });
  }, We = () => /* @__PURE__ */ l(
    Me,
    {
      hasChanged: ie,
      isResetAll: ae,
      immediateChange: g,
      resetButtonText: ee,
      closeButtonText: $,
      cancelButtonText: Z,
      applyButtonText: Y,
      onHide: W,
      onResetColumnChanges: me,
      onDiscardChanges: ge,
      onApplyChanges: pe,
      onConfirmResetColumnChanges: fe,
      onCancelResetColumnChanges: he
    }
  );
  if (!i)
    return null;
  const we = Fe("TableSettingsDialog", a);
  return /* @__PURE__ */ j(Te, { children: [
    i && /* @__PURE__ */ l(je, { columnLabels: P, setRenderedLabels: re }),
    /* @__PURE__ */ l(
      Ie,
      {
        show: i,
        title: D,
        subtitle: m,
        onClose: W,
        body: ye(),
        footer: We(),
        className: we
      }
    )
  ] });
}, je = ({
  columnLabels: L,
  setRenderedLabels: i
}) => {
  const D = B(null);
  return z(() => {
    if (!D.current)
      return;
    const m = {};
    [...D.current.children].forEach((a) => {
      const h = a.getAttribute("data-key");
      if (h) {
        const d = a.textContent?.replace(/\r?\n|\r/g, "").toLowerCase();
        d && (m[h] = d);
      }
    }), i((a) => H(a, m) ? a : m);
  }), /* @__PURE__ */ l("div", { hidden: !0, ref: D, children: Object.entries(L).map(([m, a]) => /* @__PURE__ */ l("div", { "data-key": m, children: a }, m)) });
};
export {
  $e as default
};
//# sourceMappingURL=TableSettingsDialog.js.map
