import { j as u } from "./jsx-runtime-OVHDjVDe.js";
import { Q as d, C as l, K as c, c as S, j as p } from "./css-D6ZBcSwh.js";
import { useRef as b, useState as g, useCallback as C, createContext as L, use as D, useMemo as I } from "react";
import { createPortal as P } from "react-dom";
import { c as B, e as v, a as A, C as y, b as E } from "./Animated-M9ynNzcI.js";
import { a as x } from "./Box-D5Qv__kr.js";
import "./Button-BFoGC4WW.js";
import "./Text-BzTkWLeU.js";
import "./Editable-CxgdRDPb.js";
import "./types-BNX_Jj6k.js";
import "./Provider--RxgAoVG.js";
import "./provider-C8In649C.js";
const O = {
  visible: !1,
  position: d.ZERO,
  cursor: d.ZERO,
  keys: []
}, _ = l.BE("menu-context", "container"), F = (e) => {
  let t = e;
  for (; !t.classList.contains(E); ) {
    if (t.classList.contains(_) || t.parentElement == null) return e;
    t = t.parentElement;
  }
  return t.classList.contains(E) ? t : e;
}, w = (e) => {
  const t = F(e);
  if (t == null) return [];
  const r = Array.from(
    t.parentElement?.querySelectorAll(`.${A}`) ?? []
  );
  return r.includes(t) ? r : [t];
}, G = [
  { targetCorner: c.BOTTOM_RIGHT, dialogCorner: c.TOP_LEFT },
  { targetCorner: c.BOTTOM_LEFT, dialogCorner: c.TOP_RIGHT },
  { targetCorner: c.TOP_RIGHT, dialogCorner: c.BOTTOM_LEFT },
  { targetCorner: c.TOP_LEFT, dialogCorner: c.BOTTOM_RIGHT }
], J = () => {
  const e = b(null), [t, r] = g(O), a = C((n, o) => {
    const s = d.construct(n);
    if (typeof n == "object" && "preventDefault" in n) {
      n.preventDefault(), n.stopPropagation();
      const T = w(n.target);
      o ??= S.unique(T.map((m) => m.id).filter((m) => m.length > 0));
    } else o = [];
    r({ visible: !0, keys: o, position: s, cursor: s });
  }, []), f = C((n) => {
    e.current = n, n != null && r((o) => {
      if (!o.visible) return o;
      const { adjustedDialog: s } = B({
        container: p.construct(0, 0, window.innerWidth, window.innerHeight),
        dialog: p.construct(n),
        target: p.construct(o.cursor, 0, 0),
        prefer: G
      }), T = p.topLeft(s);
      return d.equals(o.position, T) ? o : { ...o, position: T };
    });
  }, []), i = () => r(O);
  return v({ ref: e, onClickOutside: i }), {
    ...t,
    close: i,
    open: a,
    ref: f,
    className: _
  };
}, H = ({
  ref: e,
  menu: t,
  visible: r,
  open: a,
  close: f,
  position: i,
  keys: n,
  className: o,
  cursor: s,
  style: T,
  onClick: m,
  ...N
}) => r ? P(
  /* @__PURE__ */ u.jsx(
    x,
    {
      className: l(y, l.bordered()),
      ref: e,
      style: { ...d.css(i), ...T },
      onClick: (R) => {
        f(), m?.(R);
      },
      gap: "tiny",
      ...N,
      children: t?.({ keys: n, visible: r, position: i, cursor: s })
    }
  ),
  document.body
) : null, Y = ({
  menu: e,
  children: t,
  ...r
}) => /* @__PURE__ */ u.jsxs(u.Fragment, { children: [
  /* @__PURE__ */ u.jsx(H, { menu: e, ...r }),
  t
] }), M = ({
  className: e,
  padded: t = !1,
  color: r,
  ...a
}) => (t === !0 && (t = "center"), /* @__PURE__ */ u.jsx(
  x,
  {
    className: l(
      l.B("divider"),
      t !== !1 && l.BM("divider", "padded"),
      typeof t == "string" && l.loc(t),
      e
    ),
    borderColor: r,
    color: r,
    ...a
  }
)), tt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  Divider: M
}, Symbol.toStringTag, { value: "Module" })), j = L({ onClick: () => {
}, selected: "" }), et = () => D(j), rt = ({
  children: e,
  onChange: t,
  level: r,
  gap: a,
  background: f,
  value: i = ""
}) => {
  const n = C(
    (s) => {
      typeof t == "function" ? t(s) : t && s in t && t[s](s);
    },
    [t]
  ), o = I(
    () => ({
      onClick: n,
      selected: i,
      level: r,
      gap: a,
      background: f
    }),
    [i, n, r, a, f]
  );
  return /* @__PURE__ */ u.jsx(j, { value: o, children: e });
}, nt = () => /* @__PURE__ */ u.jsx(M, { x: !0, padded: !0 });
export {
  Y as C,
  nt as D,
  rt as M,
  J as a,
  M as b,
  tt as c,
  et as u
};
