import { j as T } from "./jsx-runtime-BbD0H88J.js";
import { A as m, C as p, M as a, b as _, E as d } from "./css-BkBrZtKZ.js";
import { useRef as N, useState as R, useCallback as E, useMemo as S } from "react";
import { createPortal as g } from "react-dom";
import { a as L, e as A, c as I, C as b, d as j } from "./Animated-C4xrEVd-.js";
import { a as P } from "./Box-DLO7l9hM.js";
import "./Button-DLtxKGH5.js";
import { c as y } from "./Text-XeD58Fq8.js";
import "./Editable-DjQORuR1.js";
import "./types-B5-Tni3G.js";
import "./Provider-BkxUzTWS.js";
const C = {
  visible: !1,
  position: m.ZERO,
  cursor: m.ZERO,
  keys: []
}, O = p.BE("menu-context", "container"), B = (n) => {
  let t = n;
  for (; !t.classList.contains(j); ) {
    if (t.classList.contains(O) || t.parentElement == null) return n;
    t = t.parentElement;
  }
  return t;
}, F = (n) => {
  const t = B(n);
  if (t == null) return [];
  const r = Array.from(
    t.parentElement?.querySelectorAll(`.${I}`) ?? []
  );
  return r.includes(t) ? r : [t];
}, k = [
  { targetCorner: a.BOTTOM_RIGHT, dialogCorner: a.TOP_LEFT },
  { targetCorner: a.BOTTOM_LEFT, dialogCorner: a.TOP_RIGHT },
  { targetCorner: a.TOP_RIGHT, dialogCorner: a.BOTTOM_LEFT },
  { targetCorner: a.TOP_LEFT, dialogCorner: a.BOTTOM_RIGHT }
], z = () => {
  const n = N(null), [t, r] = R(C), l = E((e) => {
    const o = m.construct(e);
    let s = [];
    if (typeof e == "object" && "preventDefault" in e) {
      e.preventDefault(), e.stopPropagation();
      const u = F(e.target);
      s = _.unique(u.map((f) => f.id).filter((f) => f.length > 0));
    }
    r({ visible: !0, keys: s, position: o, cursor: o });
  }, []), c = E((e) => {
    n.current = e, e != null && r((o) => {
      if (!o.visible) return o;
      const { adjustedDialog: s } = L({
        container: d.construct(0, 0, window.innerWidth, window.innerHeight),
        dialog: d.construct(e),
        target: d.construct(o.cursor, 0, 0),
        prefer: k
      }), u = d.topLeft(s);
      return m.equals(o.position, u) ? o : { ...o, position: u };
    });
  }, []), i = () => r(C);
  return A({ ref: n, onClickOutside: i }), {
    ...t,
    close: i,
    open: l,
    ref: c,
    className: O
  };
}, w = ({
  ref: n,
  menu: t,
  visible: r,
  open: l,
  close: c,
  position: i,
  keys: e,
  className: o,
  cursor: s,
  style: u,
  onClick: f,
  ...x
}) => r ? g(
  /* @__PURE__ */ T.jsx(
    P,
    {
      className: p(b, p.bordered(), o),
      ref: n,
      style: { ...m.css(i), ...u },
      onClick: (M) => {
        c(), f?.(M);
      },
      gap: "tiny",
      ...x,
      children: t?.({ keys: e, visible: r, position: i, cursor: s })
    }
  ),
  document.body
) : null, J = ({
  menu: n,
  children: t,
  ...r
}) => /* @__PURE__ */ T.jsxs(T.Fragment, { children: [
  /* @__PURE__ */ T.jsx(w, { menu: n, ...r }),
  t
] }), [G, K] = y({
  defaultValue: { onClick: () => {
  }, selected: "" },
  displayName: "Menu.Context"
}), Q = ({
  children: n,
  onChange: t,
  level: r,
  gap: l,
  background: c,
  value: i = ""
}) => {
  const e = E(
    (s) => {
      typeof t == "function" ? t(s) : t && s in t && t[s](s);
    },
    [t]
  ), o = S(
    () => ({
      onClick: e,
      selected: i,
      level: r,
      gap: l,
      background: c
    }),
    [i, e, r, l, c]
  );
  return /* @__PURE__ */ T.jsx(G, { value: o, children: n });
};
export {
  J as C,
  Q as M,
  z as a,
  K as u
};
