import { jsxs as R, jsx as n } from "react/jsx-runtime";
import pt, { useReducer as mt, useRef as E, useCallback as pe, useEffect as F } from "react";
import { noop as k, size as y, isEmpty as u, filter as H, cond as q, stubTrue as Z, map as Y, flow as ht, find as ft, without as gt, omit as St } from "es-toolkit/compat";
import { isNil as It, isEqual as W } from "es-toolkit/predicate";
import me from "../../utils/classNames.js";
import Tt from "./TreeSearch.js";
import yt from "./TreeSelectAll.js";
import Gt from "./TreeSummary.js";
import w from "./TreeNodeContainer.js";
import he from "./TreeNode.js";
import J from "./TreeLeafList.js";
import fe from "./TreeNothingFound.js";
import Ct from "./TreeOptions.js";
import At from "./TreeRoot.js";
import xt from "./TypeCounter.js";
import { notEqual as K, getTypeCounts as ge, debounceFn as Nt, filterOutByItemId as Q, filterAssetByType as Et, notEmpty as S, containsItemById as Ft, addOrRemoveFromList as vt, getMappedItemsToGroups as bt, sortGroupsByName as Lt, sortGroupItemsByName as Ot, filterEmptyGroups as Vt, getFlatItems as Rt, excludeFromList as kt, getListIds as Se } from "./treeUtils.js";
import { getSubTypeCounts as xs } from "./treeUtils.js";
import { treeReducer as Ht, assetCounted as Ie, allCheckedChanged as v, visibleTypeCountersChanged as wt, searchValueChanged as zt, typeFilterChanged as $t, emptyGroupsChanged as Pt, groupedItemsChanged as Bt, flatItemsChanged as jt } from "./treeReducer.js";
import { useTreeVirtualization as Dt } from "./useTreeVirtualization.js";
import { useTreeExpansion as Mt } from "./useTreeExpansion.js";
import { useTreeHeight as Ut } from "./useTreeHeight.js";
import { useTreeScrollPosition as _t } from "./useTreeScrollPosition.js";
import qt from "../smoothScrollbars/SmoothScrollbars.js";
const Te = (I) => St(I, ["expandedGroups", "onExpandGroupsChange", "onSearchChange", "onSelectionChange", "treeOptions"]), Zt = (I, c) => W(Te(I), Te(c)), Yt = 50, Jt = 5, Gs = pt.memo((I) => {
  const {
    groups: c = [],
    items: m = [],
    selectedGroups: i = [],
    selectedItems: f = [],
    onSelectionChange: ye = k,
    hasMultiselect: G = !0,
    showRadioButtons: z = !1,
    hideSearch: Ge = !1,
    hideTreeHead: Ce,
    treeHeaderContent: Ae,
    summary: xe,
    hideSummary: Ne = !1,
    search: X,
    searchPlaceholder: Ee = "Type here to filter by name",
    onSearchChange: Fe = k,
    className: ve,
    scrollHeight: ee,
    expandedGroups: Kt,
    onExpandGroupsChange: Qt = k,
    showEmptyGroups: b = !0,
    treeOptions: be = [],
    treeOptionsTooltip: Le,
    disableAnimation: x = !1,
    onTypeFilterChange: Oe = k,
    virtualizeThreshold: te = Yt,
    overscan: Ve = Jt,
    ...Re
  } = I, [o, d] = mt(Ht, {
    groupedItems: {},
    flatItems: [],
    allChecked: !1,
    searchValue: "",
    assetCounts: {},
    typeFilter: [],
    visibleTypeCounters: [],
    emptyGroups: []
  }), se = E(null), oe = E(), re = E(), ne = E(""), { internalExpandedGroups: le, handleToggleNode: $ } = Mt(
    I.expandedGroups,
    I.onExpandGroupsChange
  ), C = () => c && S(c), T = () => S(o.searchValue), P = () => T() && C(), B = () => !T() && C(), j = E(null), { virtualizedItems: D, virtualizer: g } = Dt(
    o.groupedItems,
    o.flatItems,
    T(),
    C(),
    le,
    j,
    Ve
  ), { scrollToTop: ke } = _t(g, j), He = pe(
    (e) => {
      $(e);
    },
    [$]
  ), we = pe(
    (e) => {
      if (g && e?.target) {
        const t = e.target;
        g.scrollToOffset(t.scrollTop, { align: "start" });
      }
    },
    [g]
  );
  F(() => {
    if (K(oe.current, m) || K(re.current, c)) {
      oe.current = m, re.current = c;
      const e = ge(m);
      d(Ie(e));
      const t = $e({ items: m, groups: c, selectedItems: f, selectedGroups: i }, o.flatItems);
      d(v(t)), d(wt(Object.keys(e))), L(c, m);
    }
  }, [m, c]);
  const ze = Nt(
    (e, t) => L(e, t)
  );
  F(() => {
    K(ne.current, o.searchValue) && (ze(c, m), ne.current = o.searchValue);
  }, [o.searchValue]), F(() => L(c, m), [o.typeFilter]), F(() => L(c, m), [b]), F(() => {
    const e = y(c), t = y(i);
    t !== e && o.allChecked ? d(v(!1)) : t !== 0 && t === e && !o.allChecked && d(v(!0));
  }, [i]);
  const $e = (e, t) => {
    const {
      items: s,
      groups: r,
      selectedItems: l,
      selectedGroups: a
    } = e;
    if (!C() && u(l) || B() && u(a) || P() && u(l))
      return !1;
    if (B()) {
      const p = H(r, Q(a));
      return u(p);
    }
    if (P()) {
      const p = H(t, Q(l));
      return u(p);
    }
    const h = s.filter(Q(l));
    return u(h);
  }, Pe = (e) => ce(e), Be = (e, t) => {
    const s = e && !t;
    d(v(s)), q([
      [B, () => je(s)],
      [P, () => Pe(s)],
      [Z, () => ce(s)]
    ])();
  }, je = (e) => A([], e ? Se(c) : []), ce = (e) => A(e ? Se(o.flatItems) : [], []), A = (e, t) => {
    ye({
      items: e,
      groups: t
    });
  }, ae = (e, t) => {
    const s = e.id, a = !i.includes(s) && !t ? [...i, s] : kt(i, s), h = ft(o.groupedItems, (_) => _.id === s), p = Y(h?.items, (_) => _.id), N = gt(f, ...p), O = c.length, M = b ? 0 : o.emptyGroups.length, V = O - M === a.length;
    d(v(V)), A(N, a);
  }, De = (e) => {
    Fe(e), d(zt(e)), g && setTimeout(() => {
      ke();
    }, 10);
  }, Me = (e, t) => {
    const s = Rt(e, t);
    d(jt(s));
  }, Ue = (e, t, s) => {
    const r = bt(e, t), l = ht(Lt, Ot)(r), a = s ? l : Vt(l), h = H(l, (p) => u(p.items));
    d(Pt(h)), d(Ie(ge(m))), d(Bt(a));
  }, L = (e, t) => {
    const s = o.searchValue, r = o.typeFilter, l = e, a = t, h = (V) => V && S(V), p = () => u(s) && h(l), N = () => S(s) && h(l), O = u(r) ? a : Et(a, r), M = () => Ue(l, O, b), U = () => Me(O, s);
    q([
      [p, M],
      [N, U],
      [Z, U]
    ])();
  }, _e = Ut(se, ee), ie = () => {
    const e = g.getVirtualItems(), t = C() && !T();
    return /* @__PURE__ */ n(
      qt,
      {
        ref: j,
        className: "tree-virtual-scrollbar",
        onScroll: we,
        autoHeight: !1,
        slideIn: !0,
        style: {
          height: `${_e}px`
        },
        children: /* @__PURE__ */ n(
          "div",
          {
            className: t ? "grouped-list" : "flat-list",
            style: {
              height: `${g.getTotalSize()}px`,
              position: "relative"
            },
            children: e.map((s) => {
              const r = D[s.index];
              if (!r)
                return null;
              const l = i.includes(r.id) || r.type === "leaf" && i.includes(r.groupId);
              return /* @__PURE__ */ n(
                "div",
                {
                  "data-index": s.index,
                  ref: g.measureElement,
                  className: `virtualized-tree-item ${r.type === "group" ? "group-item" : "leaf-item"} ${l ? "checked" : ""}`,
                  style: {
                    position: "absolute",
                    top: 0,
                    left: 0,
                    width: "100%",
                    height: `${s.size}px`,
                    transform: `translateY(${s.start}px)`
                  },
                  children: r.type === "group" ? /* @__PURE__ */ n(
                    w,
                    {
                      groupId: r.id,
                      isOpen: r.isExpanded,
                      disableAnimation: x,
                      children: /* @__PURE__ */ n(
                        he,
                        {
                          node: r.data,
                          hasMultiselect: G,
                          onToggleNode: He,
                          onSelect: ae,
                          isSelected: l,
                          isOpen: r.isExpanded,
                          isIndeterminate: !l && r.data.items.some((a) => f.includes(a.id))
                        }
                      )
                    },
                    r.id
                  ) : (
                    // Render individual leaf item with proper styling
                    /* @__PURE__ */ n(w, { isOpen: !0, disableAnimation: x, children: /* @__PURE__ */ n(
                      J,
                      {
                        leafList: [r.data],
                        hasMultiselect: G,
                        showRadioButtons: z,
                        selectedItems: f,
                        selectedGroups: i,
                        onSelectionChange: A
                      }
                    ) }, r.id)
                  )
                },
                `${r.type}-${r.id}-${s.index}`
              );
            })
          }
        )
      }
    );
  }, qe = () => {
    const { groupedItems: e } = o;
    return u(e) ? /* @__PURE__ */ n(fe, {}) : D.length > te ? ie() : Y(e, (s) => {
      const r = s.id, l = s.items, a = le?.includes(r) ?? !1, h = H(l, Ft(f)).length, p = i.includes(r), N = !p && h > 0;
      return /* @__PURE__ */ R(w, { groupId: r, isOpen: a, disableAnimation: x, children: [
        /* @__PURE__ */ n(
          he,
          {
            node: s,
            hasMultiselect: G,
            onToggleNode: $,
            onSelect: ae,
            isSelected: p,
            isIndeterminate: N,
            isOpen: a
          }
        ),
        a && /* @__PURE__ */ n(
          J,
          {
            leafList: l,
            hasMultiselect: G,
            showRadioButtons: z,
            selectedItems: f,
            selectedGroups: i,
            onSelectionChange: A
          }
        )
      ] }, r);
    });
  }, Ze = () => {
    const { flatItems: e } = o, t = u(e);
    return D.length > te ? ie() : /* @__PURE__ */ n(w, { disableAnimation: x, isOpen: !0, children: t ? /* @__PURE__ */ n(fe, {}) : /* @__PURE__ */ n(
      J,
      {
        leafList: e,
        hasMultiselect: G,
        showRadioButtons: z,
        selectedItems: f,
        selectedGroups: i,
        onSelectionChange: A
      }
    ) });
  }, de = S(c), Ye = () => W(y(f), y(o.flatItems)), Je = () => S(f) && !Ye(), Ke = () => {
    const e = b ? 0 : o.emptyGroups.length;
    return W(y(i), y(c) - e);
  }, Qe = () => de && S(i) && !Ke(), We = T() && u(o.flatItems), Xe = T() && u(o.groupedItems) && de, et = We || Xe, tt = Qe() || Je(), st = me("Tree", ve), ot = me("TreeHead", "display-flex gap-5", "padding-15"), rt = q([
    [() => C() && !T(), () => qe()],
    [Z, () => Ze()]
  ])(), nt = (e) => {
    const t = vt(o.typeFilter, e);
    d($t(t)), Oe(t);
  }, lt = y(o.visibleTypeCounters) !== 1, ct = S(o.typeFilter), at = !Ce, it = !et, dt = !Ge, ut = !Ne, ue = !It(X);
  return /* @__PURE__ */ R("div", { ...Re, className: st, ref: se, children: [
    /* @__PURE__ */ R("div", { className: "TreeHeader", children: [
      Ae,
      dt && !ue && /* @__PURE__ */ n(
        Tt,
        {
          value: o.searchValue,
          onChange: De,
          placeholder: Ee
        }
      ),
      ue && X,
      at && /* @__PURE__ */ R("div", { className: ot, children: [
        it && /* @__PURE__ */ n("div", { className: "border border-right-only hidden-empty padding-right-10 margin-right-2", children: /* @__PURE__ */ n(
          yt,
          {
            isChecked: o.allChecked,
            isEnabled: G,
            isIndeterminate: tt,
            onSelect: Be
          }
        ) }),
        /* @__PURE__ */ n("div", { className: "display-flex justify-content-between align-items-start width-100pct", children: ut ? xe || /* @__PURE__ */ n(Gt, { children: Y(o.visibleTypeCounters, (e) => /* @__PURE__ */ n(
          xt,
          {
            type: e,
            icon: `${e}`,
            value: o.assetCounts[e],
            onClick: nt,
            isActive: o.typeFilter.includes(e),
            hasFilter: ct,
            enableActivity: lt
          },
          e
        )) }) : null }),
        /* @__PURE__ */ n(Ct, { treeOptions: be, treeOptionsTooltip: Le })
      ] })
    ] }),
    /* @__PURE__ */ n(At, { maxHeight: ee, disableAnimation: x, children: rt })
  ] });
}, Zt);
export {
  Gs as default,
  xs as getSubTypeCounts,
  ge as getTypeCounts
};
//# sourceMappingURL=Tree.js.map
