import { a3 as F, H as l, mO as C, mP as _, ad as y, I as j, cY as B, mQ as T, mR as E, mS as V, mT as q, mU as Q, _ as U } from "./56c67d3b.js";
import M, { useContext as O, Fragment as Y, useState as N, useMemo as G } from "react";
const A = M.createContext({
  toggleFold: () => {
  },
  foldedStatus: {}
}), J = F.div`
  position: relative;
  width: auto;
  height: ${(e) => e.$itemHeight ?? 30}px;
  /* border: 1px solid #d7d9e0; */
  border-radius: 4px;
  margin-bottom: ${(e) => e.$positionLineHeight ?? 4}px;
  display: flex;
  /* padding: 0 8px; */
  align-items: center;
  opacity: ${(e) => e.$dragging ? "0.5" : 1};

  &::before {
    content: "";
    display: ${(e) => (e.$showPositionLineDot ?? !1) && e.$isOver && e.$showPositionLine ? "block" : "none"};
    position: absolute;
    background-color: #315efb;
    height: ${(e) => e.$positionLineDotDiameter}px;
    width: ${(e) => e.$positionLineDotDiameter}px;
    left: 0;
    border-radius: 100%;
    bottom: ${(e) => -(e.$positionLineHeight ?? 4) - ((e.$positionLineDotDiameter ?? 8) - (e.$positionLineHeight ?? 4)) / 2}px;
    left: ${(e) => e.$positionLineIndent ?? (e.$dropInAsSub ? 15 : -1)}px;
    z-index: 1;
  }

  &::after {
    content: "";
    display: ${(e) => e.$isOver && e.$showPositionLine ? "block" : "none"};
    height: ${(e) => e.$positionLineHeight ?? 4}px;
    border-radius: 4px;
    position: absolute;
    left: ${(e) => e.$positionLineIndent ?? (e.$dropInAsSub ? 15 : -1)}px;
    right: 0;
    background-color: #315efb;
    bottom: ${(e) => -(e.$positionLineHeight ?? 4)}px;
  }
`;
function K(e, o) {
  var u;
  const {
    extra: a,
    dragging: s = !1,
    dropInAsSub: c = !0,
    isOver: h = !1,
    dragListeners: g,
    children: d,
    path: n,
    ...$
  } = e, t = O(A), i = (u = t.positionLineIndent) == null ? void 0 : u.call(t, n, c), r = (t.showDropInPositionLine ?? !0) || !c;
  return /* @__PURE__ */ l(J, {
    $showPositionLine: r,
    $positionLineIndent: i,
    $positionLineDotDiameter: t.positionLineDotDiameter,
    $showPositionLineDot: t.showPositionLineDot,
    $positionLineHeight: t.positionLineHeight,
    $itemHeight: t.itemHeight,
    $isOver: h,
    $dragging: s,
    $dropInAsSub: c,
    ref: o,
    ...$,
    ...g,
    children: d
  });
}
const X = M.forwardRef(K), Z = F.div`
  pointer-events: none;
  position: absolute;
  width: 100%;
  top: -${(e) => e.$positionLineHeight ?? 4}px;
  height: ${(e) => (e.$itemHeight ?? 30) * 0.6}px;
  z-index: 10;
  /* background-color: rgba(0, 0, 0, 0.2); */
  .position-line {
    height: ${(e) => e.$positionLineHeight ?? 4}px;
    border-radius: 4px;
    background-color: ${(e) => e.$active ? "#315efb" : "transparent"};
    width: 100%;
    margin-left: ${(e) => e.$positionLineIndent ?? 0}px;
    position: relative;
    &::after {
      content: "";
      position: absolute;
      background-color: #315efb;
      display: ${(e) => (e.$showPositionLineDot ?? !1) && e.$active ? "block" : "none"};
      left: 0;
      height: ${(e) => e.$positionLineDotDiameter ?? 8}px;
      width: ${(e) => e.$positionLineDotDiameter ?? 8}px;
      top: ${(e) => -((e.$positionLineDotDiameter ?? 8) - (e.$positionLineHeight ?? 4)) / 2}px;
      border-radius: 100%;
    }
  }
`;
function W(e) {
  var n;
  const {
    path: o,
    disabled: a,
    targetListSize: s
  } = e, c = {
    targetPath: o,
    targetListSize: s,
    dropInAsSub: !1
  }, {
    setNodeRef: h,
    isOver: g
  } = C({
    id: `p_${o.join("_")}`,
    disabled: a,
    data: c
  }), d = O(A);
  return /* @__PURE__ */ l(Z, {
    $itemHeight: d.itemHeight,
    $showPositionLineDot: d.showPositionLineDot,
    $positionLineDotDiameter: d.positionLineDotDiameter,
    $positionLineHeight: d.positionLineHeight,
    $positionLineIndent: (n = d.positionLineIndent) == null ? void 0 : n.call(d, o, !1),
    $active: g,
    ref: h,
    children: /* @__PURE__ */ l("div", {
      className: "position-line"
    })
  });
}
function P(e, o) {
  return e ? typeof e == "function" ? e(o) : e : !0;
}
const ee = F.div`
  position: relative;
`;
function k(e) {
  const {
    item: o,
    path: a,
    activeNode: s,
    disabled: c,
    parentDragging: h,
    disableDropIn: g,
    forceFold: d = !1,
    dropInAsSub: n = !0,
    isOverlay: $ = !1,
    // onAddSubMenu,
    onDelete: t,
    renderContent: i
  } = e, r = o.id ?? a.join("_"), u = o.items, b = O(A), w = (d || b.foldedStatus[r]) && !b.unfoldAll, f = {
    path: a,
    node: o
  }, {
    listeners: L,
    setNodeRef: D,
    isDragging: I
  } = _({
    id: r,
    data: f,
    disabled: b.disable ?? !1
  }), x = P(o.canDropIn, s == null ? void 0 : s.data), p = P(o.canDropBefore, s == null ? void 0 : s.data), S = P(o.canDropAfter, s == null ? void 0 : s.data), m = {
    targetListSize: u.length,
    targetPath: n ? [...a, 0] : [...a.slice(0, -1), a[a.length - 1] + 1],
    dropInAsSub: n
  }, {
    setNodeRef: v,
    isOver: R
  } = C({
    id: r,
    disabled: I || c || g || !n && S === !1,
    data: m
  });
  return /* @__PURE__ */ y(j, {
    children: [/* @__PURE__ */ y(ee, {
      children: [s && p && /* @__PURE__ */ l(W, {
        path: a,
        targetListSize: u.length,
        disabled: I || c
      }), /* @__PURE__ */ y(B, {
        style: {
          paddingLeft: "15px"
        },
        align: "center",
        children: [/* @__PURE__ */ l(X, {
          path: a,
          id: r,
          dropInAsSub: n && x !== !1,
          isOver: R,
          ref: (H) => {
            D(H), v(H);
          },
          ...L,
          children: /* @__PURE__ */ l("span", {
            style: {
              cursor: "default"
            },
            children: "⣿"
          })
        }), /* @__PURE__ */ l("div", {
          style: {
            flex: 1,
            maxWidth: "calc(100% - 12px)"
          },
          children: (i == null ? void 0 : i({
            node: o,
            isOver: R,
            path: a,
            isOverlay: $,
            hasChildren: u.length > 0,
            dragging: !!(I || h),
            isFolded: w,
            onDelete: () => t == null ? void 0 : t(a),
            onToggleFold: () => b.toggleFold(r)
          })) || null
        })]
      })]
    }), u.length > 0 && !w && /* @__PURE__ */ l("div", {
      className: "sub-menu-list",
      children: u.map((H, z) => /* @__PURE__ */ l(Y, {
        children: /* @__PURE__ */ l(k, {
          path: [...a, z],
          activeNode: s,
          dropInAsSub: !1,
          item: H,
          renderContent: i,
          disabled: c || I || g,
          onDelete: t,
          parentDragging: I
        })
      }, z))
    })]
  });
}
const te = F.div`
  .menu-title {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .menu-list {
    margin-top: 8px;
    position: relative;
  }
`;
function ne(e) {
  const {
    node: o,
    onAddItem: a,
    onDeleteItem: s,
    onMoveItem: c,
    onAddSubItem: h,
    renderItemContent: g
  } = e, d = O(A), [n, $] = N(null), t = n && n.node.items.length > 0;
  function i(f) {
    $(f.active.data.current);
  }
  function r(f) {
    var I;
    const L = f.active.data.current, D = (I = f.over) == null ? void 0 : I.data.current;
    if (D) {
      const x = L.path, p = D.targetPath;
      if (x.length === p.length && U.isEqual(x.slice(0, -1), p.slice(0, -1))) {
        const S = x[x.length - 1];
        let m = p[p.length - 1];
        S < m && (m -= 1), c(p, S, m);
      } else {
        let S = p[p.length - 1], m = p, v = 0;
        s(x), D.dropInAsSub ? (m = m.slice(0, -1), v = h(m, L.node.data)) : v = a(m, L.node.data), D.targetListSize !== -1 && c(p, v, S);
      }
    }
    $(null);
  }
  const u = T(E(V, {
    activationConstraint: {
      distance: 8
    }
  })), b = o.items[o.items.length - 1], w = P(b == null ? void 0 : b.canDropAfter, n == null ? void 0 : n.node.data);
  return /* @__PURE__ */ l(te, {
    children: /* @__PURE__ */ y(q, {
      sensors: u,
      onDragStart: i,
      onDragEnd: r,
      children: [o.items.map((f, L) => {
        const D = P(f.canDropIn, n == null ? void 0 : n.node.data);
        return /* @__PURE__ */ l(k, {
          activeNode: n == null ? void 0 : n.node,
          dropInAsSub: !t && D !== !1,
          disableDropIn: !!t || D === !1,
          renderContent: g,
          onAddSubMenu: h,
          onDelete: s,
          path: [L],
          item: f
        }, L);
      }), w && /* @__PURE__ */ l("div", {
        style: {
          position: "relative"
        },
        children: n && /* @__PURE__ */ l(W, {
          targetListSize: -1,
          path: [o.items.length]
        })
      }), /* @__PURE__ */ l(Q, {
        dropAnimation: null,
        children: n && /* @__PURE__ */ l(k, {
          isOverlay: !0,
          forceFold: d.showSubInDragOverlay === !1,
          path: [],
          item: n.node,
          renderContent: g
        })
      })]
    })
  });
}
function se(e) {
  const {
    node: o,
    renderItemContent: a,
    ...s
  } = e, [c, h] = N({}), g = (t, i) => (i || (i = o.items), t.length === 1 ? i[t[0]] : g(t.slice(1), i[t[0]].items)), d = (t, i) => (i || (i = e.node), t.length === 1 ? i : d(t.slice(1), i.items[t[0]])), n = (t) => {
    h((i) => {
      const r = {
        ...i
      };
      return r[t] = !r[t], r;
    });
  }, $ = G(() => ({
    ...s,
    toggleFold: n,
    foldedStatus: c
  }), [c, s]);
  return /* @__PURE__ */ l(A.Provider, {
    value: $,
    children: /* @__PURE__ */ l(ne, {
      node: o,
      renderItemContent: a,
      onAddItem: (t, i) => {
        const r = d(t);
        return r.addItem(i), r.items.length;
      },
      onDeleteItem: (t) => {
        d(t).deleteItem(t[t.length - 1]);
      },
      onAddSubItem: (t, i) => {
        const r = g(t);
        return r.addSubItem(i), r.items.length;
      },
      onMoveItem: (t, i, r) => {
        d(t).moveItem(i, r);
      }
    })
  });
}
export {
  se as D
};
