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