import { c as T, C as i } from "./css-BUXMI9PB.js";
import { v as A, x as F } from "./eraser-DXDDmPgV.js";
import { j as p } from "./jsx-runtime-C5mzlN2N.js";
import { A as G } from "./Animated-B9bcEALW.js";
import { ar as z, as as K, at as U, au as L, ag as Y, s as J, al as Q } from "./main-jKCzbrWn.js";
import { useMemo as V, useCallback as _ } from "react";
import "react-dom";
const D = (e, n) => n.includes(e.key), v = ({
  nodes: e,
  expanded: n,
  sort: t,
  depth: s = 0
}) => {
  const r = { keys: [], nodes: [] };
  return t != null && e.sort(t), e.forEach((o) => {
    const l = D(o, n);
    if (r.keys.push(o.key), r.nodes.push({
      depth: s,
      expanded: l,
      hasChildren: o.children != null
    }), l && o.children != null) {
      const { keys: c, nodes: f } = v({
        nodes: o.children,
        expanded: n,
        sort: t,
        depth: s + 1
      });
      r.keys.push(...c), r.nodes.push(...f);
    }
  }), r;
}, W = ({
  tree: e,
  destination: n,
  keys: t
}) => (t = A.toArray(t), n == null ? [...P({ tree: e, keys: t }), ...e.filter((r) => !t.includes(r.key))] : (t.forEach((s) => {
  const r = a({ tree: e, key: s });
  r != null && (H({ tree: e, keys: s }), O({ tree: e, destination: n, additions: r }));
}), e)), H = ({
  tree: e,
  keys: n
}) => (n = A.toArray(n), n.forEach((t) => {
  const s = e.findIndex((r) => r.key === t);
  if (s !== -1) e.splice(s, 1);
  else {
    const r = E({ tree: e, key: t });
    r != null && (r.children = r.children?.filter((o) => o.key !== t));
  }
}), e), O = ({
  tree: e,
  destination: n,
  additions: t,
  throwOnMissing: s = !0
}) => {
  t = A.toArray(t);
  const r = T.by(t, (c) => c.key, !1), o = r.map((c) => c.key);
  if (n == null)
    return [
      ...r,
      ...e.filter((c) => !o.includes(c.key))
    ];
  const l = a({ tree: e, key: n });
  if (l == null) {
    if (s) throw new Error(`Could not find node with key ${n}`);
    return e;
  }
  return l.children ??= [], l.children = [
    ...r,
    ...l.children.filter((c) => !o.includes(c.key))
  ], e;
}, X = ({
  tree: e,
  key: n,
  updater: t,
  throwOnMissing: s = !0
}) => {
  const r = a({ tree: e, key: n });
  if (r == null) {
    if (s) throw new Error(`Could not find node with key ${n}`);
    return e;
  }
  const o = E({ tree: e, key: n });
  if (o != null) {
    const l = o.children?.findIndex((c) => c.key === n);
    l != null && l !== -1 && o.children?.splice(l, 1, t(r));
  } else
    e.splice(
      e.findIndex((l) => l.key === n),
      1,
      t(r)
    );
  return e;
}, Z = ({
  tree: e,
  parent: n,
  updater: t,
  throwOnMissing: s = !0
}) => {
  const r = a({ tree: e, key: n });
  if (r == null) {
    if (s) 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 s = 0; s < e.length; s++) {
    const r = e[s];
    if (r.key === n) return r;
    if (r.children != null) {
      const o = a({ tree: r.children, key: n, depth: t + 1 });
      if (o != null) return o;
    }
  }
  return null;
}, P = ({
  tree: e,
  keys: n
}) => {
  const t = [];
  for (const s of n) {
    const r = a({ tree: e, key: s });
    r != null && t.push(r);
  }
  return t;
}, E = ({
  tree: e,
  key: n
}) => {
  for (const t of e)
    if (t.children != null) {
      if (t.children.some((r) => r.key === n)) return t;
      const s = E({ tree: t.children, key: n });
      if (s != null) return s;
    }
  return null;
}, $ = (e) => e.map((n) => ({
  ...n,
  children: n.children != null ? $(n.children) : void 0
})), B = (...e) => {
  const n = [];
  return e.forEach((t) => {
    n.push(t), t.children != null && n.push(...B(...t.children));
  }), n;
}, ee = (e, n) => {
  const t = { keys: [], nodes: [] };
  return e.keys.forEach((s, r) => {
    n(s, e.nodes[r].depth) && (t.keys.push(s), t.nodes.push(e.nodes[r]));
  }), t;
}, ne = (e) => {
  const n = Math.min(...e.nodes.map((t) => t.depth));
  return e.keys.filter((t, s) => e.nodes[s].depth === n);
}, te = (e, n) => {
  const t = n.keys.findIndex((s) => s === e);
  return n.nodes[t].depth;
}, q = (e, n) => {
  const t = e.keys.findIndex((s) => s === n);
  return e.nodes[t];
}, re = ({
  depth: e,
  hasChildren: n,
  expanded: t,
  children: s,
  style: r,
  className: o,
  loading: l,
  useMargin: c = !1,
  offsetMultiplier: f = 2.5,
  ...m
}) => (
  // @ts-expect-error - generic element issues
  /* @__PURE__ */ p.jsxs(
    z,
    {
      className: i(
        i.BE("tree", "item"),
        e !== 0 && i.M("show-rules"),
        c && i.M("margin"),
        o
      ),
      style: {
        [i.var("tree-item-offset")]: `${e * f + 1.5}rem`,
        ...r
      },
      gap: "small",
      align: "center",
      ...m,
      children: [
        n && /* @__PURE__ */ p.jsx(
          G,
          {
            className: i.BE("tree", "expansion-indicator"),
            enabled: t,
            enabledLoc: "bottom",
            disabledLoc: "right"
          },
          "caret"
        ),
        s,
        l && /* @__PURE__ */ p.jsx(K, {})
      ]
    }
  )
), se = "tree-item", oe = [["Shift"]], le = ({
  onExpand: e,
  nodes: n,
  initialExpanded: t = [],
  selected: s,
  onSelectedChange: r,
  sort: o
}) => {
  const [l, c, f] = U(t), [m, N] = F({
    initial: [],
    value: s,
    onChange: r
  }), S = V(
    () => v({ nodes: n, expanded: l, sort: o }),
    [n, l, o]
  ), C = L(n), I = L(S), w = Y({ triggers: oe }), b = _(
    (u, { clicked: d }) => {
      if (N((h) => u.length === 0 && h.length > 0 ? h.slice(0, 1) : u), d == null || w.current.held) return;
      const y = q(I.current, d);
      if (y == null || !y.hasChildren) return;
      const j = f.current, M = j.some((h) => h === d) ? "contract" : "expand";
      let g;
      M === "contract" ? g = j.filter((h) => h !== d) : g = [...j, d], c(g), e?.({ current: g, action: M, clicked: d });
    },
    [e, C, c, N]
  ), k = _(
    (u) => {
      c((d) => T.unique([...d, u])), e?.({ current: l, action: "expand", clicked: u });
    },
    [c]
  ), x = _(
    (...u) => {
      c((d) => d.filter((y) => !u.includes(y))), u.forEach((d) => {
        e?.({ current: l, action: "contract", clicked: d });
      });
    },
    [c]
  ), R = _(() => c([]), [c]);
  return {
    selected: m,
    expanded: l,
    contract: x,
    expand: k,
    clearExpanded: R,
    shape: S,
    onSelect: b
  };
}, ce = ({
  shape: e,
  children: n,
  selected: t,
  onSelect: s,
  getItem: r,
  subscribe: o,
  className: l,
  contract: c,
  expand: f,
  expanded: m,
  className: N,
  clearExpanded: S,
  showRules: C = !1,
  virtual: I = !1,
  ...w
}) => {
  const { keys: b, nodes: k } = e;
  return /* @__PURE__ */ p.jsx(
    J,
    {
      multiple: !0,
      value: t,
      replaceOnSingle: !0,
      data: b,
      onChange: s,
      getItem: r,
      subscribe: o,
      itemHeight: 27,
      virtual: I,
      children: /* @__PURE__ */ p.jsx(
        Q,
        {
          full: "y",
          className: i(i.B("tree"), l, C && i.M("show-rules")),
          ...w,
          displayItems: 1 / 0,
          children: ({ index: x, ...R }) => n({ index: x, ...k[x], ...R })
        }
      )
    }
  );
}, me = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  HAUL_TYPE: se,
  Item: re,
  Tree: ce,
  deepCopy: $,
  filterShape: ee,
  findNode: a,
  findNodeParent: E,
  findNodes: P,
  flatten: v,
  getAllNodesOfMinDepth: ne,
  getDepth: te,
  getDescendants: B,
  getNodeShape: q,
  moveNode: W,
  removeNode: H,
  setNode: O,
  shouldExpand: D,
  updateNode: X,
  updateNodeChildren: Z,
  use: le
}, Symbol.toStringTag, { value: "Module" }));
export {
  me as e
};
