import { jsx as N, jsxs as as } from "react/jsx-runtime";
import { memo as rs, forwardRef as os, useRef as ns, useEffect as ds, useCallback as v, Children as gt, useMemo as z, createElement as ls } from "react";
import { useDndContext as ps, useDndMonitor as us } from "@dnd-kit/core";
import { useSortable as is } from "@dnd-kit/sortable";
import { CSS as cs } from "@dnd-kit/utilities";
import { noop as ms } from "lodash-es";
import { TreeItemColorsClassMap as fs, TreeItemBorderRadiusClassMap as L, TreeItemSpacingClassMap as ws, TreeItemBorderClassMap as Tt, TreeItemBorderStyleClassMap as hs, TreeItemShadowClassMap as bs } from "../types.es.js";
import { useDebounce as St } from "../../../hooks/useDebounce.es.js";
import { PARENT_FOCUS_VISIBLE_STYLE as ys, FOCUS_VISIBLE_STYLE as Is } from "../../../utilities/focusStyle.es.js";
import { merge as b } from "../../../utilities/merge.es.js";
import { DragHandle as gs } from "./DragHandle.es.js";
import { ExpandButton as Ts } from "./ExpandButton.es.js";
import { useTreeItem as Ss } from "./useTreeItem.es.js";
import { removeFragmentsAndEnrichChildren as Ds } from "../utils/removeFragmentsAndEnrichChildren.es.js";
import { useDeepCompareEffect as Ns } from "../utils/useDeepCompareEffect.es.js";
import { INDENTATION_WIDTH as Dt, EXPAND_ONHOVER_DELAY as xs } from "../helpers/constants.es.js";
const As = ({ isSorting: a, wasDragging: M }) => !(a || M), Es = rs(
  os(
    ({
      id: a,
      type: M,
      label: y,
      onDrop: W,
      accepts: Nt,
      showCaret: U,
      children: I,
      parentId: _,
      level: u = 0,
      contentComponent: g,
      onClick: H,
      onSelect: k,
      onExpand: m,
      onShrink: R,
      registerOverlay: x,
      registerNodeChildren: F,
      unregisterNodeChildren: d,
      draggable: K = !0,
      expandable: O = !0,
      showDragHandlerOnHoverOnly: xt = !0,
      dragHandlerPosition: i = "left",
      showContentWhileDragging: A = !0,
      itemStyle: X,
      ignoreItemDoubleClick: q = !1,
      expandOnSelect: At = !1,
      "data-test-id": Et = "fondue-tree-item"
    }, f) => {
      var rt, ot, nt, dt, lt, pt, ut, it, ct, mt, ft, wt, ht, bt, yt;
      const { active: o, over: E } = ps(), { isSelected: p, isExpanded: c, projection: s } = Ss(a), G = ns(), e = (o == null ? void 0 : o.id) === a, t = e && s !== null && s !== void 0 ? s : null, Bt = typeof ((ot = (rt = E == null ? void 0 : E.data) == null ? void 0 : rt.current) == null ? void 0 : ot.accepts) == "string" ? (nt = E.data.current.accepts) == null ? void 0 : nt.split(", ") : [], Ct = typeof (t == null ? void 0 : t.accepts) == "string" ? (dt = t.accepts) == null ? void 0 : dt.split(", ") : [], T = ((lt = o == null ? void 0 : o.data.current) == null ? void 0 : lt.type) || "", S = ((pt = s == null ? void 0 : s.previousNode) == null ? void 0 : pt.depth) !== void 0 && (s == null ? void 0 : s.depth) > ((ut = s == null ? void 0 : s.previousNode) == null ? void 0 : ut.depth), vt = S && s && s.depth - 1 === ((it = s == null ? void 0 : s.previousNode) == null ? void 0 : it.depth), Y = S && ((ct = s == null ? void 0 : s.previousNode) == null ? void 0 : ct.accepts) !== void 0 && (((mt = s == null ? void 0 : s.previousNode) == null ? void 0 : mt.accepts.includes(`${T}-deeper`)) || ((ft = s == null ? void 0 : s.previousNode) == null ? void 0 : ft.accepts.includes(`${T}-within`))), Lt = e && vt && ((wt = t == null ? void 0 : t.previousNode) == null ? void 0 : wt.accepts) !== void 0 && ((ht = t == null ? void 0 : t.previousNode) == null ? void 0 : ht.accepts.includes(`${T}-within`)) || (t == null ? void 0 : t.isWithinParent) && Ct.includes(`${T}-within`), D = e && (o == null ? void 0 : o.data.current) && (Bt.includes(T) && !S || Lt), J = St((r) => {
        G.current === r && (m == null || m(r));
      }, xs);
      ds(() => {
        var r, l, h;
        e && (G.current = S ? (r = t == null ? void 0 : t.previousNode) == null ? void 0 : r.id : null), e && Y && (t != null && t.parentId) && t.previousNode && t.parentId === t.previousNode.id && t.parentId !== ((h = (l = o == null ? void 0 : o.data) == null ? void 0 : l.current) == null ? void 0 : h.parentId) && J(t == null ? void 0 : t.parentId);
      }, [
        o == null ? void 0 : o.data,
        t == null ? void 0 : t.parentId,
        t == null ? void 0 : t.previousNode,
        J,
        e,
        Y,
        S
      ]);
      const Mt = v(
        (r) => {
          var It;
          const { over: l, active: h } = r;
          !e || !t || h.id === (l == null ? void 0 : l.id) && (t == null ? void 0 : t.depth) === ((It = h.data.current) == null ? void 0 : It.level) || e && l && D && W && W({
            id: h.id,
            parentId: t.parentId,
            sort: t.position,
            contentComponent: g,
            parentType: t.type
          });
        },
        [e, t, D, W, g]
      ), Wt = v(
        (r) => {
          r.active.id === a && (x == null || x({
            contentComponent: g,
            children: I,
            id: a,
            label: y,
            level: u,
            dragHandlerPosition: i,
            showContentWhileDragging: A
          }));
        },
        [
          I,
          g,
          i,
          a,
          y,
          u,
          x,
          A
        ]
      ), _t = v(
        (r) => {
          r.active.id === a && document.body.style.setProperty("cursor", D ? "grabbing" : "no-drop");
        },
        [D, a]
      );
      us({
        onDragEnd: Mt,
        onDragStart: Wt,
        onDragMove: _t
      });
      const Q = v(
        (r) => {
          r == null || r.stopPropagation(), c ? R && R(a) : m && m(a);
        },
        [a, c, m, R]
      ), Ht = St(
        (r) => {
          r.stopPropagation(), !(q && r.detail >= 2) && (At && Q(), k == null || k(a), H == null || H(a));
        },
        q ? 300 : 0
      ), Z = _ ? (o == null ? void 0 : o.id) === _ : !1, P = gt.count(I) > 0, { enrichedChildren: $, childrenIds: kt } = z(() => {
        const r = Ds(I, { parentId: a, level: u + 1 });
        return {
          enrichedChildren: r,
          childrenIds: r.map((l) => l.props.id)
        };
      }, [I, a, u]), {
        attributes: Rt,
        listeners: Ft,
        transform: B,
        transition: Yt,
        setDraggableNodeRef: $t,
        setDroppableNodeRef: Vt,
        setActivatorNodeRef: zt
      } = is({
        id: a,
        disabled: !K,
        data: { type: M, accepts: Nt, parentId: _, level: u },
        animateLayoutChanges: As,
        transition: null
      });
      Ns(() => {
        if (gt.count($) === 0) {
          d == null || d(a);
          return;
        }
        if (e || Z) {
          d == null || d(a);
          return;
        }
        c ? F == null || F({ id: a, children: $ }) : d == null || d(a);
      }, [e, c, Z, $, a]);
      const n = z(() => ({
        spacingY: "none",
        contentHight: "single-line",
        shadow: "none",
        borderRadius: "small",
        borderWidth: "none",
        borderStyle: "none",
        activeColorStyle: "neutral",
        ...X
      }), [X]), w = fs[n.activeColorStyle ?? "neutral"], { liClassName: Ut, backgroundClassName: Kt } = z(() => ({
        liClassName: b([
          Is,
          "tw-box-content tw-relative tw-cursor-default tw-transition-colors tw-outline-none tw-ring-inset tw-group tw-no-underline tw-leading-5",
          !e && p ? w.selectedTextColor : w.textColor,
          ws[n.spacingY ?? "none"]
        ]),
        backgroundClassName: b([
          "tw-block tw-absolute tw-inset-0 tw-transition-colors -tw-z-10",
          n.borderWidth !== "none" ? L[n.borderRadius ?? "small"] : "",
          !e && (!p || n.activeColorStyle !== "neutral") && w.pressedBackgroundColor,
          !e && p ? w.selectedBackgroundColor : w.backgroundColor,
          p && ys
        ])
      }), [e, p, n, w]), Ot = A ? !0 : !e, Xt = b([
        "tw-max-w-full tw-grow tw-overflow-hidden",
        e && A ? "tw-opacity-75 tw-blur-sm tw-grayscale" : ""
      ]), qt = c && !e, j = K && !e, Gt = y !== void 0 && !e, C = !e && O && (U === void 0 ? P : U);
      let V = "";
      !e && !c && C && Y && ((bt = s == null ? void 0 : s.previousNode) == null ? void 0 : bt.id) === a && (s == null ? void 0 : s.depth) > ((yt = s == null ? void 0 : s.previousNode) == null ? void 0 : yt.depth) && (V = b([
        "tw-border-solid tw-border-box-selected-strong",
        L[n.borderRadius ?? "small"],
        Tt.small
      ]));
      const Jt = n.borderWidth !== "none" && V === "" ? b([
        Tt[n.borderWidth ?? "none"],
        L[n.borderRadius ?? "small"],
        hs[n.borderStyle ?? "none"]
      ]) : "", Qt = n.contentHight === "single-line" ? "tw-h-10" : "tw-h-fit", Zt = n.contentHight === "single-line" ? "tw-h-12" : "tw-h-fit", Pt = b([
        "tw-relative tw-z-0 tw-transition-colors tw-flex tw-items-center tw-leading-5 tw-width-fit tw-justify-between tw-group",
        bs[n.shadow ?? "none"],
        e ? "tw-border-dashed tw-border-2 tw-pr-0" : Jt,
        e && L[n.borderRadius ?? "small"],
        e ? Zt : Qt,
        e && (D ? "tw-border-box-selected-strong tw-bg-box-selected-hover" : "tw-bg-box-negative-hover tw-border-box-negative-strong-hover"),
        V
      ]), tt = t != null && t.depth ? t.depth * Dt : void 0, st = e ? 0 : u * Dt, jt = { paddingLeft: tt ?? st }, ts = n.borderWidth !== "none" ? {} : { marginLeft: -1 * (tt ?? st) }, ss = {
        transform: cs.Transform.toString(B),
        transition: Yt
      }, et = { ...Ft, ...Rt }, at = i !== "none" ? /* @__PURE__ */ N(
        gs,
        {
          ...i !== "none" ? et : {},
          active: p,
          ref: zt,
          disabled: !j,
          "aria-hidden": !j,
          showDragHandlerOnHoverOnly: xt,
          activeColorStyle: n.activeColorStyle ?? "neutral"
        }
      ) : null, es = i === "none" ? { ...et } : {};
      return /* @__PURE__ */ ls(
        "li",
        {
          ...es,
          id: a,
          key: a,
          tabIndex: 0,
          role: "treeitem",
          style: jt,
          onKeyDown: ms,
          "aria-label": y,
          "aria-level": u + 1,
          onClick: Ht,
          className: Ut,
          ref: (r) => {
            Vt(r), typeof f == "function" ? f(r) : f != null && f.current && (f.current = r);
          },
          "data-test-id": Et,
          "aria-selected": p,
          "aria-expanded": c,
          "data-has-children": P,
          "aria-owns": kt.join(" ")
        },
        /* @__PURE__ */ as("div", { ref: $t, className: Pt, style: ss, children: [
          /* @__PURE__ */ N("span", { className: Kt, style: ts, "aria-hidden": !0 }),
          i === "left" && at,
          O && /* @__PURE__ */ N(
            Ts,
            {
              active: B != null && B.y ? !1 : p,
              onClick: Q,
              expanded: qt,
              disabled: !C,
              "aria-hidden": !C,
              className: C ? "tw-visible" : "tw-invisible tw-pointer-events-none"
            }
          ),
          Gt && /* @__PURE__ */ N("span", { className: "first:tw-ml-3.5 tw-w-full tw-h-full tw-flex tw-items-center", children: y }),
          Ot && /* @__PURE__ */ N("div", { className: Xt, children: g }),
          i === "right" && at
        ] })
      );
    }
  )
);
Es.displayName = "FondueTreeItem";
export {
  Es as TreeItem
};
//# sourceMappingURL=TreeItem.es.js.map
