import { array as w, unique as A } from "@synnaxlabs/x";
import { c as G, L as z } from "./Text-2MISZsc5.js";
import { j as p } from "./jsx-runtime-tc70JA_2.js";
import { A as K } from "./Animated-CLo1CLgn.js";
import { C as c } from "./css-D90kZTM8.js";
import { L as U, F as Y } from "./Tags-DvfGhiWv.js";
import "./Copy-COWBkO_v.js";
import "./Box-Bi280MYh.js";
import { a as J } from "./Items-DQba1BFQ.js";
import "./Animated-CO--9HPM.js";
import { useMemo as Q, useCallback as g } from "react";
import { f as V, d as R } from "./Editable-C17ILP50.js";
import { b as W } from "./types-CGRCBVD4.js";
import "./Provider-jFWpR0CX.js";
import "react-dom";
import "./Haul-CnZK3e-n.js";
import "./provider-Bt-E2O6g.js";
import { b as X } from "./hooks-QNj6PdUo.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: i, nodes: f } = I({
        nodes: s.children,
        expanded: n,
        sort: t,
        depth: o + 1
      });
      r.keys.push(...i), 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, (i) => i.key, !1), s = r.map((i) => i.key);
  if (n == null)
    return [
      ...r,
      ...e.filter((i) => !s.includes(i.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((i) => !s.includes(i.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((i) => i.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: i } = l, { expanded: f, depth: m, hasChildren: x } = $("Tree.Item")[i];
  return /* @__PURE__ */ p.jsxs(
    U,
    {
      className: c(
        c.BE("tree", "item"),
        m !== 0 && c.M("show-rules"),
        r && c.M("margin"),
        t
      ),
      style: {
        [c.var("tree-item-offset")]: `${m * s + 1.5}rem`,
        ...n
      },
      gap: "small",
      align: "center",
      ...l,
      children: [
        x && /* @__PURE__ */ p.jsx(
          K,
          {
            className: c.BE("tree", "expansion-indicator"),
            enabled: f,
            enabledLoc: "bottom",
            disabledLoc: "right"
          },
          "caret"
        ),
        e,
        o && /* @__PURE__ */ p.jsx(z, {})
      ]
    }
  );
}, ie = "tree-item", de = [["Shift"]], ce = ({
  onExpand: e,
  nodes: n,
  initialExpanded: t = [],
  selected: o,
  onSelectedChange: r,
  sort: s
}) => {
  const [l, i, 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: de }), k = g(
    (u, { clicked: d }) => {
      if (x((h) => u.length === 0 && h.length > 0 ? h.slice(0, 1) : u), d == null || b.current.held) return;
      const y = O(S.current, d);
      if (y == null || !y.hasChildren) return;
      const v = f.current, j = v.some((h) => h === d) ? "contract" : "expand";
      let _;
      j === "contract" ? _ = v.filter((h) => h !== d) : _ = [...v, d], i(_), e?.({ current: _, action: j, clicked: d });
    },
    [e, N, i, x]
  ), q = g(
    (u) => {
      i((d) => A.unique([...d, u])), e?.({ current: l, action: "expand", clicked: u });
    },
    [i]
  ), B = g(
    (...u) => {
      i((d) => d.filter((y) => !u.includes(y))), u.forEach((d) => {
        e?.({ current: l, action: "contract", clicked: d });
      });
    },
    [i]
  ), F = g(() => i([]), [i]);
  return {
    selected: m,
    expanded: l,
    contract: B,
    expand: q,
    clearExpanded: F,
    shape: E,
    onSelect: k
  };
}, ue = ({
  shape: e,
  children: n,
  selected: t,
  onSelect: o,
  getItem: r,
  subscribe: s,
  className: l,
  contract: i,
  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: c(c.B("tree"), l, N && c.M("show-rules")),
          ...b,
          children: n
        }
      )
    }
  ) });
}, je = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  HAUL_TYPE: ie,
  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: ce,
  useContext: $
}, Symbol.toStringTag, { value: "Module" }));
export {
  je as e
};
