import { h as k, c as A, C as i } from "./css-D6ZBcSwh.js";
import { j as f } from "./jsx-runtime-OVHDjVDe.js";
import { A as z } from "./Animated-otpyxn5i.js";
import { L as K } from "./Text-BzTkWLeU.js";
import { L as U, F as Y } from "./Tags-Dpu9kD_T.js";
import "./Button-BFoGC4WW.js";
import "./Box-D5Qv__kr.js";
import { a as J } from "./Items-G25zb57I.js";
import { f as Q } from "./Animated-M9ynNzcI.js";
import { createContext as V, useMemo as L, useCallback as g } from "react";
import { d as W, c as R } from "./Editable-CxgdRDPb.js";
import { b as X } from "./types-BNX_Jj6k.js";
import "./Provider--RxgAoVG.js";
import "./provider-C8In649C.js";
import "react-dom";
import "./Haul-BuI0PZpo.js";
import { b as Z } from "./hooks-15yB7IKE.js";
const M = (e, n) => n.includes(e.key), v = ({
  nodes: e,
  expanded: n,
  sort: t,
  depth: o = 0
}) => {
  const r = { keys: [], nodes: [] };
  return t != null && e.sort(t), e.forEach((s) => {
    const l = M(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: c, nodes: h } = v({
        nodes: s.children,
        expanded: n,
        sort: t,
        depth: o + 1
      });
      r.keys.push(...c), r.nodes.push(...h);
    }
  }), r;
}, ee = ({
  tree: e,
  destination: n,
  keys: t
}) => (t = k.toArray(t), n == null ? [...D({ tree: e, keys: t }), ...e.filter((r) => !t.includes(r.key))] : (t.forEach((o) => {
  const r = a({ tree: e, key: o });
  r != null && (P({ tree: e, keys: o }), T({ tree: e, destination: n, additions: r }));
}), e)), P = ({
  tree: e,
  keys: n,
  parent: t
}) => {
  if (n = k.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;
}, T = ({
  tree: e,
  destination: n,
  additions: t,
  throwOnMissing: o = !0
}) => {
  t = k.toArray(t);
  const r = A.by(t, (c) => c.key, !1), s = r.map((c) => c.key);
  if (n == null)
    return [
      ...r,
      ...e.filter((c) => !s.includes(c.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((c) => !s.includes(c.key))
  ], e;
}, ne = ({
  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((c) => c.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;
}, te = ({
  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;
}, D = ({
  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;
}, H = (e) => e.map((n) => ({
  ...n,
  children: n.children != null ? H(n.children) : void 0
})), O = (...e) => {
  const n = [];
  return e.forEach((t) => {
    n.push(t), t.children != null && n.push(...O(...t.children));
  }), n;
}, re = (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;
}, oe = (e) => {
  const n = Math.min(...e.nodes.map((t) => t.depth));
  return e.keys.filter((t, o) => e.nodes[o].depth === n);
}, se = (e, n) => {
  const t = n.keys.findIndex((o) => o === e);
  return n.nodes[t].depth;
}, $ = (e, n) => {
  const t = e.keys.findIndex((o) => o === n);
  return e.nodes[t];
}, q = V(null), le = () => Q(q), ce = (e) => {
  const { shape: n } = le();
  return n.nodes[e];
}, de = ({
  shape: e,
  children: n
}) => {
  const t = L(() => ({ shape: e }), [e]);
  return /* @__PURE__ */ f.jsx(q.Provider, { value: t, children: n });
}, ie = ({
  children: e,
  style: n,
  className: t,
  loading: o,
  useMargin: r = !1,
  offsetMultiplier: s = 2.5,
  ...l
}) => {
  const { index: c } = l, { expanded: h, depth: m, hasChildren: x } = ce(c);
  return /* @__PURE__ */ f.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__ */ f.jsx(
          z,
          {
            className: i.BE("tree", "expansion-indicator"),
            enabled: h,
            enabledLoc: "bottom",
            disabledLoc: "right"
          },
          "caret"
        ),
        e,
        o && /* @__PURE__ */ f.jsx(K, {})
      ]
    }
  );
}, ue = "tree-item", ae = [["Shift"]], fe = ({
  onExpand: e,
  nodes: n,
  initialExpanded: t = [],
  selected: o,
  onSelectedChange: r,
  sort: s
}) => {
  const [l, c, h] = W(t), [m, x] = X({
    initial: [],
    value: o,
    onChange: r
  }), E = L(
    () => v({ nodes: n, expanded: l, sort: s }),
    [n, l, s]
  ), N = R(n), S = R(E), b = Z({ triggers: ae }), I = g(
    (u, { clicked: d }) => {
      if (x((p) => u.length === 0 && p.length > 0 ? p.slice(0, 1) : u), d == null || b.current.held) return;
      const y = $(S.current, d);
      if (y == null || !y.hasChildren) return;
      const j = h.current, w = j.some((p) => p === d) ? "contract" : "expand";
      let _;
      w === "contract" ? _ = j.filter((p) => p !== d) : _ = [...j, d], c(_), e?.({ current: _, action: w, clicked: d });
    },
    [e, N, c, x]
  ), B = g(
    (u) => {
      c((d) => A.unique([...d, u])), e?.({ current: l, action: "expand", clicked: u });
    },
    [c]
  ), F = g(
    (...u) => {
      c((d) => d.filter((y) => !u.includes(y))), u.forEach((d) => {
        e?.({ current: l, action: "contract", clicked: d });
      });
    },
    [c]
  ), G = g(() => c([]), [c]);
  return {
    selected: m,
    expanded: l,
    contract: F,
    expand: B,
    clearExpanded: G,
    shape: E,
    onSelect: I
  };
}, he = ({
  shape: e,
  children: n,
  selected: t,
  onSelect: o,
  getItem: r,
  subscribe: s,
  className: l,
  contract: c,
  expand: h,
  expanded: m,
  className: x,
  clearExpanded: E,
  showRules: N = !1,
  virtual: S = !1,
  ...b
}) => {
  const { keys: I } = e;
  return /* @__PURE__ */ f.jsx(de, { shape: e, children: /* @__PURE__ */ f.jsx(
    Y,
    {
      multiple: !0,
      value: t,
      replaceOnSingle: !0,
      data: I,
      onChange: o,
      getItem: r,
      subscribe: s,
      itemHeight: 27,
      virtual: S,
      children: /* @__PURE__ */ f.jsx(
        J,
        {
          full: "y",
          className: i(i.B("tree"), l, N && i.M("show-rules")),
          ...b,
          displayItems: 1 / 0,
          children: n
        }
      )
    }
  ) });
}, Ae = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  HAUL_TYPE: ue,
  Item: ie,
  Tree: he,
  deepCopy: H,
  filterShape: re,
  findNode: a,
  findNodeParent: C,
  findNodes: D,
  flatten: v,
  getAllNodesOfMinDepth: oe,
  getDepth: se,
  getDescendants: O,
  getNodeShape: $,
  moveNode: ee,
  removeNode: P,
  setNode: T,
  shouldExpand: M,
  updateNode: ne,
  updateNodeChildren: te,
  use: fe
}, Symbol.toStringTag, { value: "Module" }));
export {
  Ae as e
};
