import { d as w, b as A, C as i } from "./css-BkBrZtKZ.js";
import { c as G, L as z } from "./Text-XeD58Fq8.js";
import { j as p } from "./jsx-runtime-BbD0H88J.js";
import { A as K } from "./Animated-BbeJdao0.js";
import { L as U, F as Y } from "./Tags-V7sNuGZv.js";
import "./Button-DLtxKGH5.js";
import "./Box-DLO7l9hM.js";
import { a as J } from "./Items-CX11yLMf.js";
import "./Animated-C4xrEVd-.js";
import { useMemo as Q, useCallback as g } from "react";
import { d as V, c as R } from "./Editable-DjQORuR1.js";
import { b as W } from "./types-B5-Tni3G.js";
import "./Provider-BkxUzTWS.js";
import "react-dom";
import "./Haul-DBy-2ds_.js";
import "./provider-D0K9E52u.js";
import { b as X } from "./hooks-BeSuo_di.js";
const T = (e, n) => n.includes(e.key), I = ({
  nodes: e,
  expanded: n,
  sort: t,
  depth: o = 0
}) => {
  const r = { keys: [], nodes: [] };
  return t != null && e.sort(t), e.forEach((s) => {
    const l = T(s, n);
    if (r.keys.push(s.key), r.nodes.push({
      depth: o,
      expanded: l,
      hasChildren: s.children != null
    }), l && s.children != null) {
      const { keys: d, nodes: f } = I({
        nodes: s.children,
        expanded: n,
        sort: t,
        depth: o + 1
      });
      r.keys.push(...d), r.nodes.push(...f);
    }
  }), r;
}, Z = ({
  tree: e,
  destination: n,
  keys: t
}) => (t = w.toArray(t), n == null ? [...P({ tree: e, keys: t }), ...e.filter((r) => !t.includes(r.key))] : (t.forEach((o) => {
  const r = a({ tree: e, key: o });
  r != null && (L({ tree: e, keys: o }), M({ tree: e, destination: n, additions: r }));
}), e)), L = ({
  tree: e,
  keys: n,
  parent: t
}) => {
  if (n = w.toArray(n), t !== void 0) {
    if (t === null) return e.filter((r) => !n.includes(r.key));
    const o = a({ tree: e, key: t });
    return o == null || (o.children = o.children?.filter(
      (r) => !n.includes(r.key)
    )), e;
  }
  return n.forEach((o) => {
    const r = e.findIndex((s) => s.key === o);
    if (r !== -1) e.splice(r, 1);
    else {
      const s = C({ tree: e, key: o });
      s != null && (s.children = s.children?.filter((l) => l.key !== o));
    }
  }), e;
}, M = ({
  tree: e,
  destination: n,
  additions: t,
  throwOnMissing: o = !0
}) => {
  t = w.toArray(t);
  const r = A.by(t, (d) => d.key, !1), s = r.map((d) => d.key);
  if (n == null)
    return [
      ...r,
      ...e.filter((d) => !s.includes(d.key))
    ];
  const l = a({ tree: e, key: n });
  if (l == null) {
    if (o) throw new Error(`Could not find node with key ${n}`);
    return e;
  }
  return l.children ??= [], l.children = [
    ...r,
    ...l.children.filter((d) => !s.includes(d.key))
  ], e;
}, ee = ({
  tree: e,
  key: n,
  updater: t,
  throwOnMissing: o = !0
}) => {
  const r = a({ tree: e, key: n });
  if (r == null) {
    if (o) throw new Error(`Could not find node with key ${n}`);
    return e;
  }
  const s = C({ tree: e, key: n });
  if (s != null) {
    const l = s.children?.findIndex((d) => d.key === n);
    l != null && l !== -1 && s.children?.splice(l, 1, t(r));
  } else
    e.splice(
      e.findIndex((l) => l.key === n),
      1,
      t(r)
    );
  return e;
}, ne = ({
  tree: e,
  parent: n,
  updater: t,
  throwOnMissing: o = !0
}) => {
  const r = a({ tree: e, key: n });
  if (r == null) {
    if (o) throw new Error(`Could not find node with key ${n}`);
    return e;
  }
  return r.children = t(r.children ?? []), e;
}, a = ({
  tree: e,
  key: n,
  depth: t = 0
}) => {
  for (let o = 0; o < e.length; o++) {
    const r = e[o];
    if (r.key === n) return r;
    if (r.children != null) {
      const s = a({ tree: r.children, key: n, depth: t + 1 });
      if (s != null) return s;
    }
  }
  return null;
}, P = ({
  tree: e,
  keys: n
}) => {
  const t = [];
  for (const o of n) {
    const r = a({ tree: e, key: o });
    r != null && t.push(r);
  }
  return t;
}, C = ({
  tree: e,
  key: n
}) => {
  for (const t of e)
    if (t.children != null) {
      if (t.children.some((r) => r.key === n)) return t;
      const o = C({ tree: t.children, key: n });
      if (o != null) return o;
    }
  return null;
}, D = (e) => e.map((n) => ({
  ...n,
  children: n.children != null ? D(n.children) : void 0
})), H = (...e) => {
  const n = [];
  return e.forEach((t) => {
    n.push(t), t.children != null && n.push(...H(...t.children));
  }), n;
}, te = (e, n) => {
  const t = { keys: [], nodes: [] };
  return e.keys.forEach((o, r) => {
    n(o, e.nodes[r].depth) && (t.keys.push(o), t.nodes.push(e.nodes[r]));
  }), t;
}, re = (e) => {
  const n = Math.min(...e.nodes.map((t) => t.depth));
  return e.keys.filter((t, o) => e.nodes[o].depth === n);
}, oe = (e, n) => {
  const t = n.keys.findIndex((o) => o === e);
  return n.nodes[t].depth;
}, O = (e, n) => {
  const t = e.keys.findIndex((o) => o === n);
  return e.nodes[t];
}, [se, $] = G({
  displayName: "Tree.Context",
  providerName: "Tree.Provider"
}), le = ({
  children: e,
  style: n,
  className: t,
  loading: o,
  useMargin: r = !1,
  offsetMultiplier: s = 2.5,
  ...l
}) => {
  const { index: d } = l, { expanded: f, depth: m, hasChildren: x } = $("Tree.Item")[d];
  return /* @__PURE__ */ p.jsxs(
    U,
    {
      className: i(
        i.BE("tree", "item"),
        m !== 0 && i.M("show-rules"),
        r && i.M("margin"),
        t
      ),
      style: {
        [i.var("tree-item-offset")]: `${m * s + 1.5}rem`,
        ...n
      },
      gap: "small",
      align: "center",
      ...l,
      children: [
        x && /* @__PURE__ */ p.jsx(
          K,
          {
            className: i.BE("tree", "expansion-indicator"),
            enabled: f,
            enabledLoc: "bottom",
            disabledLoc: "right"
          },
          "caret"
        ),
        e,
        o && /* @__PURE__ */ p.jsx(z, {})
      ]
    }
  );
}, de = "tree-item", ce = [["Shift"]], ie = ({
  onExpand: e,
  nodes: n,
  initialExpanded: t = [],
  selected: o,
  onSelectedChange: r,
  sort: s
}) => {
  const [l, d, f] = V(t), [m, x] = W({
    initial: [],
    value: o,
    onChange: r
  }), E = Q(
    () => I({ nodes: n, expanded: l, sort: s }),
    [n, l, s]
  ), N = R(n), S = R(E), b = X({ triggers: ce }), k = g(
    (u, { clicked: c }) => {
      if (x((h) => u.length === 0 && h.length > 0 ? h.slice(0, 1) : u), c == null || b.current.held) return;
      const y = O(S.current, c);
      if (y == null || !y.hasChildren) return;
      const v = f.current, j = v.some((h) => h === c) ? "contract" : "expand";
      let _;
      j === "contract" ? _ = v.filter((h) => h !== c) : _ = [...v, c], d(_), e?.({ current: _, action: j, clicked: c });
    },
    [e, N, d, x]
  ), B = g(
    (u) => {
      d((c) => A.unique([...c, u])), e?.({ current: l, action: "expand", clicked: u });
    },
    [d]
  ), F = g(
    (...u) => {
      d((c) => c.filter((y) => !u.includes(y))), u.forEach((c) => {
        e?.({ current: l, action: "contract", clicked: c });
      });
    },
    [d]
  ), q = g(() => d([]), [d]);
  return {
    selected: m,
    expanded: l,
    contract: F,
    expand: B,
    clearExpanded: q,
    shape: E,
    onSelect: k
  };
}, ue = ({
  shape: e,
  children: n,
  selected: t,
  onSelect: o,
  getItem: r,
  subscribe: s,
  className: l,
  contract: d,
  expand: f,
  expanded: m,
  className: x,
  clearExpanded: E,
  showRules: N = !1,
  virtual: S = !1,
  ...b
}) => {
  const { keys: k } = e;
  return /* @__PURE__ */ p.jsx(se, { value: e.nodes, children: /* @__PURE__ */ p.jsx(
    Y,
    {
      multiple: !0,
      value: t,
      replaceOnSingle: !0,
      data: k,
      onChange: o,
      getItem: r,
      subscribe: s,
      itemHeight: 27,
      virtual: S,
      children: /* @__PURE__ */ p.jsx(
        J,
        {
          full: "y",
          className: i(i.B("tree"), l, N && i.M("show-rules")),
          ...b,
          children: n
        }
      )
    }
  ) });
}, Ie = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  HAUL_TYPE: de,
  Item: le,
  Tree: ue,
  deepCopy: D,
  filterShape: te,
  findNode: a,
  findNodeParent: C,
  findNodes: P,
  flatten: I,
  getAllNodesOfMinDepth: re,
  getDepth: oe,
  getDescendants: H,
  getNodeShape: O,
  moveNode: Z,
  removeNode: L,
  setNode: M,
  shouldExpand: T,
  updateNode: ee,
  updateNodeChildren: ne,
  use: ie,
  useContext: $
}, Symbol.toStringTag, { value: "Module" }));
export {
  Ie as e
};
