import { a as v, c as A, C as i } from "./css-BYwooBtA.js";
import { j as f } from "./jsx-runtime-OVHDjVDe.js";
import { aL as K, aM as z, aN as J, aJ as U, aO as Y, aK as b, af as Q, r as V, ak as W } from "./main-BINuQ9na.js";
import { createContext as X, useMemo as M, useCallback as g } from "react";
import { A as Z } from "./provider-CKYgKu3z.js";
import "react-dom";
const L = (e, n) => n.includes(e.key), w = ({
  nodes: e,
  expanded: n,
  sort: t,
  depth: s = 0
}) => {
  const r = { keys: [], nodes: [] };
  return t != null && e.sort(t), e.forEach((o) => {
    const l = L(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: h } = w({
        nodes: o.children,
        expanded: n,
        sort: t,
        depth: s + 1
      });
      r.keys.push(...c), r.nodes.push(...h);
    }
  }), r;
}, ee = ({
  tree: e,
  destination: n,
  keys: t
}) => (t = v.toArray(t), n == null ? [...T({ tree: e, keys: t }), ...e.filter((r) => !t.includes(r.key))] : (t.forEach((s) => {
  const r = a({ tree: e, key: s });
  r != null && (P({ tree: e, keys: s }), O({ tree: e, destination: n, additions: r }));
}), e)), P = ({
  tree: e,
  keys: n,
  parent: t
}) => {
  if (n = v.toArray(n), t !== void 0) {
    if (t === null) return e.filter((r) => !n.includes(r.key));
    const s = a({ tree: e, key: t });
    return s == null || (s.children = s.children?.filter(
      (r) => !n.includes(r.key)
    )), e;
  }
  return n.forEach((s) => {
    const r = e.findIndex((o) => o.key === s);
    if (r !== -1) e.splice(r, 1);
    else {
      const o = C({ tree: e, key: s });
      o != null && (o.children = o.children?.filter((l) => l.key !== s));
    }
  }), e;
}, O = ({
  tree: e,
  destination: n,
  additions: t,
  throwOnMissing: s = !0
}) => {
  t = v.toArray(t);
  const r = A.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;
}, ne = ({
  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 = C({ 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;
}, te = ({
  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;
}, T = ({
  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;
}, 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 s = C({ tree: t.children, key: n });
      if (s != null) return s;
    }
  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;
}, re = (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;
}, se = (e) => {
  const n = Math.min(...e.nodes.map((t) => t.depth));
  return e.keys.filter((t, s) => e.nodes[s].depth === n);
}, oe = (e, n) => {
  const t = n.keys.findIndex((s) => s === e);
  return n.nodes[t].depth;
}, $ = (e, n) => {
  const t = e.keys.findIndex((s) => s === n);
  return e.nodes[t];
}, q = X(null), le = () => K(q), ce = (e) => {
  const { shape: n } = le();
  return n.nodes[e];
}, de = ({
  shape: e,
  children: n
}) => {
  const t = M(() => ({ shape: e }), [e]);
  return /* @__PURE__ */ f.jsx(q.Provider, { value: t, children: n });
}, ie = ({
  children: e,
  style: n,
  className: t,
  loading: s,
  useMargin: r = !1,
  offsetMultiplier: o = 2.5,
  ...l
}) => {
  const { index: c } = l, { expanded: h, depth: m, hasChildren: x } = ce(c);
  return /* @__PURE__ */ f.jsxs(
    z,
    {
      className: i(
        i.BE("tree", "item"),
        m !== 0 && i.M("show-rules"),
        r && i.M("margin"),
        t
      ),
      style: {
        [i.var("tree-item-offset")]: `${m * o + 1.5}rem`,
        ...n
      },
      gap: "small",
      align: "center",
      ...l,
      children: [
        x && /* @__PURE__ */ f.jsx(
          J,
          {
            className: i.BE("tree", "expansion-indicator"),
            enabled: h,
            enabledLoc: "bottom",
            disabledLoc: "right"
          },
          "caret"
        ),
        e,
        s && /* @__PURE__ */ f.jsx(U, {})
      ]
    }
  );
}, ue = "tree-item", ae = [["Shift"]], fe = ({
  onExpand: e,
  nodes: n,
  initialExpanded: t = [],
  selected: s,
  onSelectedChange: r,
  sort: o
}) => {
  const [l, c, h] = Y(t), [m, x] = Z({
    initial: [],
    value: s,
    onChange: r
  }), E = M(
    () => w({ nodes: n, expanded: l, sort: o }),
    [n, l, o]
  ), N = b(n), S = b(E), k = Q({ triggers: ae }), I = g(
    (u, { clicked: d }) => {
      if (x((p) => u.length === 0 && p.length > 0 ? p.slice(0, 1) : u), d == null || k.current.held) return;
      const y = $(S.current, d);
      if (y == null || !y.hasChildren) return;
      const j = h.current, R = j.some((p) => p === d) ? "contract" : "expand";
      let _;
      R === "contract" ? _ = j.filter((p) => p !== d) : _ = [...j, d], c(_), e?.({ current: _, action: R, 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: s,
  getItem: r,
  subscribe: o,
  className: l,
  contract: c,
  expand: h,
  expanded: m,
  className: x,
  clearExpanded: E,
  showRules: N = !1,
  virtual: S = !1,
  ...k
}) => {
  const { keys: I } = e;
  return /* @__PURE__ */ f.jsx(de, { shape: e, children: /* @__PURE__ */ f.jsx(
    V,
    {
      multiple: !0,
      value: t,
      replaceOnSingle: !0,
      data: I,
      onChange: s,
      getItem: r,
      subscribe: o,
      itemHeight: 27,
      virtual: S,
      children: /* @__PURE__ */ f.jsx(
        W,
        {
          full: "y",
          className: i(i.B("tree"), l, N && i.M("show-rules")),
          ...k,
          displayItems: 1 / 0,
          children: n
        }
      )
    }
  ) });
}, Ce = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  HAUL_TYPE: ue,
  Item: ie,
  Tree: he,
  deepCopy: D,
  filterShape: re,
  findNode: a,
  findNodeParent: C,
  findNodes: T,
  flatten: w,
  getAllNodesOfMinDepth: se,
  getDepth: oe,
  getDescendants: H,
  getNodeShape: $,
  moveNode: ee,
  removeNode: P,
  setNode: O,
  shouldExpand: L,
  updateNode: ne,
  updateNodeChildren: te,
  use: fe
}, Symbol.toStringTag, { value: "Module" }));
export {
  Ce as e
};
