import { y as re, A as H, e as se, n as ae } from "./aggregator-J7DZKy5x.js";
import { j as N } from "./jsx-runtime-B6kdoens.js";
import { v as le, w as oe, e as ie, n as ce, q as de, C as fe, M as ue, x as he, y as pe, z as me, B as ge, A as X, E as xe, F as ye, G as Ce, H as Ee } from "./Toggle-B5c5wLIa.js";
import { memo as Ne, useState as be, useCallback as b, useMemo as ve } from "react";
import { L as Te } from "./Link-5Ul3dBhL.js";
import { A as Se } from "./Animated-e4uMRKwj.js";
import { C as u } from "./css-BHfxGWOW.js";
import "./Pack-CcDPYCmE.js";
import { L as _e } from "./List-BF7beadf.js";
const Y = (e, t) => t.includes(e.key), k = (e, t) => {
  t && e.sort((n, s) => re.stringsWithNumbers(n.name, s.name));
  let r = !1;
  for (let n = 0; n < e.length; n++) {
    const s = e[n];
    s.forcePosition != null && n !== s.forcePosition && (r = !0, e.splice(n, 1), e.splice(s.forcePosition, 0, s));
  }
  return r ? k(e, !1) : e;
}, G = ({
  nodes: e,
  expanded: t,
  depth: r = 0,
  sort: n = !0
}) => {
  r === 0 && n && (e = e.sort((a, o) => a.name.localeCompare(o.name)));
  const s = [];
  return e.forEach((a, o) => {
    const i = Y(a, t);
    s.push({ ...a, depth: r, expanded: i, index: o }), i && a.children != null && (a.children = k(a.children, n), s.push(
      ...G({ nodes: a.children, expanded: t, depth: r + 1, sort: n })
    ));
  }), s;
}, we = ({ tree: e, destination: t, keys: r }) => (r = H(r), r.forEach((n) => {
  const s = v({ tree: e, key: n });
  s != null && (q({ tree: e, keys: n }), V({ tree: e, destination: t, additions: s }));
}), e), q = ({ tree: e, keys: t }) => {
  t = H(t);
  const r = e.map((n) => n.key);
  return t.forEach((n) => {
    var a;
    const s = r.indexOf(n);
    if (s !== -1) e.splice(s, 1);
    else {
      const o = $({ tree: e, key: n });
      o != null && (o.children = (a = o.children) == null ? void 0 : a.filter((i) => i.key !== n));
    }
  }), e;
}, V = ({ tree: e, destination: t, additions: r }) => {
  r = H(r);
  const n = v({ tree: e, key: t });
  if (n == null) throw new Error(`Could not find node with key ${t}`);
  n.children == null && (n.children = []);
  const s = r.map((a) => a.key);
  return n.children = [
    ...r,
    ...n.children.filter((a) => !s.includes(a.key))
  ], e;
}, Ie = ({
  tree: e,
  key: t,
  updater: r,
  throwOnMissing: n = !0
}) => {
  var o, i;
  const s = v({ tree: e, key: t });
  if (s == null) {
    if (n) throw new Error(`Could not find node with key ${t}`);
    return e;
  }
  const a = $({ tree: e, key: t });
  if (a != null) {
    const l = (o = a.children) == null ? void 0 : o.findIndex((x) => x.key === t);
    l != null && l !== -1 && ((i = a.children) == null || i.splice(l, 1, r(s)));
  } else
    e.splice(
      e.findIndex((l) => l.key === t),
      1,
      r(s)
    );
  return e;
}, Le = ({
  tree: e,
  parent: t,
  updater: r,
  throwOnMissing: n = !0
}) => {
  const s = v({ tree: e, key: t });
  if (s == null) {
    if (n) throw new Error(`Could not find node with key ${t}`);
    return e;
  }
  return s.children = r(s.children ?? []), e;
}, v = ({
  tree: e,
  key: t,
  depth: r = 0
}) => {
  for (let n = 0; n < e.length; n++) {
    const s = e[n];
    if (s.key === t) {
      const a = s;
      return a.depth = r, a.position = n, a;
    }
    if (s.children != null) {
      const a = v({ tree: s.children, key: t, depth: r + 1 });
      if (a != null) return a;
    }
  }
  return null;
}, je = ({ tree: e, keys: t }) => {
  const r = [];
  for (const n of t) {
    const s = v({ tree: e, key: n });
    s != null && r.push(s);
  }
  return r;
}, $ = ({ tree: e, key: t }) => {
  for (const r of e)
    if (r.children != null) {
      if (r.children.some((s) => s.key === t)) return r;
      const n = $({ tree: r.children, key: t });
      if (n != null) return n;
    }
  return null;
}, J = (e) => e.map((t) => ({ ...t, children: J(t.children ?? []) })), Q = (...e) => {
  const t = [];
  return e.forEach((r) => {
    t.push(r), r.children != null && t.push(...Q(...r.children));
  }), t;
}, M = "tree-item", Pe = [["Shift"]], Re = (e) => {
  const {
    onExpand: t,
    nodes: r,
    initialExpanded: n = [],
    sort: s = !0,
    selected: a,
    onSelectedChange: o
  } = e ?? {}, [i, l, x] = he(n), [c, T] = se({
    initial: [],
    value: a,
    onChange: o
  }), S = ve(() => G({ nodes: r, expanded: i, sort: s }), [r, i, s]), _ = pe(S), L = me({ triggers: Pe }), R = b(
    (f, { clicked: d }) => {
      T(f);
      const g = _.current.find((E) => E.key === d);
      if ((g == null ? void 0 : g.hasChildren) === !1 || d == null || L.current.held) return;
      const y = x.current, I = y.some((E) => E === d) ? "contract" : "expand";
      let C = y;
      I === "contract" ? C = y.filter((E) => E !== d) : C = [...y, d], l(C), t == null || t({ current: C, action: I, clicked: d });
    },
    [t, _, l, T]
  ), h = b(
    (f) => {
      l((d) => ae([...d, f])), t == null || t({ current: i, action: "expand", clicked: f });
    },
    [l]
  ), j = b(
    (f) => {
      l((d) => d.filter((g) => g !== f)), t == null || t({ current: i, action: "contract", clicked: f });
    },
    [l]
  ), w = b(() => l([]), [l]);
  return {
    onSelect: R,
    selected: c,
    expanded: i,
    contract: j,
    expand: h,
    nodes: S,
    clearExpanded: w
  };
}, K = Ne(
  ({
    entry: e,
    selected: t,
    onSelect: r,
    onDrop: n,
    onRename: s,
    onSuccessfulDrop: a,
    onDoubleClick: o,
    loading: i = !1,
    useMargin: l = !0,
    translate: x
  }) => {
    const {
      key: c,
      hasChildren: T = !1,
      allowRename: S = !1,
      children: _,
      icon: L,
      name: R,
      depth: h,
      expanded: j,
      href: w,
      haulItems: f = []
    } = e, { getSelected: d } = le(), { getSourceData: g } = oe(), y = T || _ != null && _.length > 0, I = [];
    y && I.push(
      /* @__PURE__ */ N.jsx(Se, { enabled: j, enabledLoc: "bottom", disabledLoc: "right" })
    ), L != null && I.push(L);
    const C = [];
    i && C.push(/* @__PURE__ */ N.jsx(ie.Loading, { className: u.B("loading-indicator") }));
    const [E, A] = be(!1), { startDrag: U, ...Z } = ce({
      type: "Tree.Item",
      key: c,
      canDrop: b(({ items: p, source: O }) => {
        const m = p.map((P) => P.key);
        return A(!1), O.type === "Tree.Item" && !m.includes(c);
      }, []),
      onDrop: b((p) => (n == null ? void 0 : n(c, p)) ?? [], [c, n]),
      onDragOver: b(() => A(!0), [])
    }), D = () => {
      const p = d(), O = g().filter(
        (m) => p.includes(m.key)
      );
      if (p.includes(c)) {
        const m = O.map(({ key: P, haulItems: B, depth: F }) => [
          { type: M, key: P, data: { depth: F } },
          ...(B == null ? void 0 : B.map((W) => ({
            ...W,
            data: { ...W.data, depth: F }
          }))) ?? []
        ]).flat();
        return U(m, (P) => a == null ? void 0 : a(c, P));
      }
      U(
        [
          { type: M, key: c, data: { depth: h } },
          ...f.map((m) => ({ ...m, data: { ...m.data, depth: h } }))
        ],
        (m) => a == null ? void 0 : a(c, m)
      );
    }, ee = l ? "marginLeft" : "paddingLeft";
    let z = h * 2 + 1;
    y && l && (z -= 1);
    const te = {
      id: c,
      variant: "text",
      draggable: f.length > 0,
      className: u(
        u.BE("list", "item"),
        de,
        E && u.M("dragging-over"),
        t && fe,
        u.selected(t),
        y && u.M("has-children"),
        u.BM("depth", h.toString())
      ),
      onDragLeave: () => A(!1),
      onDragStart: D,
      onClick: () => r == null ? void 0 : r(c),
      style: {
        border: "none",
        position: x != null ? "absolute" : "relative",
        transform: `translateY(${x}px)`,
        [ee]: `${z}rem`,
        // @ts-expect-error - CSS variable
        "--pluto-tree-indicator-offset": `${h * 1.5 + (h === 0 ? 0 : 0.5)}rem`
      },
      startIcon: I,
      iconSpacing: "small",
      noWrap: !0,
      endIcon: C,
      onDoubleClick: (p) => o == null ? void 0 : o(c, p),
      href: w,
      ...Z
    }, ne = w != null ? Te : ge;
    return /* @__PURE__ */ N.jsx(ne, { className: u.BE("list", "item"), ...te, children: /* @__PURE__ */ N.jsx(
      ue,
      {
        id: `text-${c}`,
        level: "p",
        allowDoubleClick: !1,
        value: R,
        disabled: !S,
        onChange: (p) => s == null ? void 0 : s(c, p)
      }
    ) });
  }
);
K.displayName = "Tree.Item";
const $e = Ee(K), Ae = ({
  nodes: e,
  selected: t = [],
  onSelect: r,
  onDrop: n,
  onRename: s,
  onSuccessfulDrop: a,
  onDoubleClick: o,
  className: i,
  children: l = $e,
  itemHeight: x = 27,
  useMargin: c = !1,
  showRules: T = !1,
  virtual: S = !0,
  clearExpanded: _,
  expand: L,
  contract: R,
  emptyContent: h,
  loading: j,
  ...w
}) => {
  const f = S ? X.Virtual : X;
  return /* @__PURE__ */ N.jsx(_e, { data: e, emptyContent: h, children: /* @__PURE__ */ N.jsx(
    xe,
    {
      value: t,
      onChange: r,
      allowMultiple: !0,
      replaceOnSingle: !0,
      children: /* @__PURE__ */ N.jsx(
        f,
        {
          itemHeight: x,
          className: u(i, u.B("tree"), T && u.M("rules")),
          ...w,
          children: ({ key: d, ...g }) => l({
            ...g,
            key: d,
            loading: j === d,
            useMargin: c,
            onDrop: n,
            onRename: s,
            onSuccessfulDrop: a,
            onDoubleClick: o
          })
        }
      )
    }
  ) });
}, Oe = (e, t) => ye(`text-${e}`, t), Be = (e) => Ce(`text-${e}`), Xe = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  DefaultItem: K,
  HAUL_TYPE: M,
  Tree: Ae,
  asyncRename: Be,
  deepCopy: J,
  findNode: v,
  findNodeParent: $,
  findNodes: je,
  flatten: G,
  getDescendants: Q,
  moveNode: we,
  removeNode: q,
  setNode: V,
  shouldExpand: Y,
  sortAndSplice: k,
  startRenaming: Oe,
  updateNode: Ie,
  updateNodeChildren: Le,
  use: Re
}, Symbol.toStringTag, { value: "Module" }));
export {
  Xe as e
};
