import { j as T } from "./jsx-runtime-tc70JA_2.js";
import { xy as p, location as r, unique as _, box as C } from "@synnaxlabs/x";
import { useRef as M, useState as b, useCallback as E, useMemo as N } from "react";
import { createPortal as g } from "react-dom";
import { C as O } from "./css-D90kZTM8.js";
import { a as S, e as L, f as I, g as P, c as j, C as y, d as A } from "./Animated-CO--9HPM.js";
import { a as w } from "./Box-Bi280MYh.js";
import "./Copy-COWBkO_v.js";
import { c as B } from "./Text-2MISZsc5.js";
import { b as F } from "./Editable-C17ILP50.js";
import "./types-CGRCBVD4.js";
import "./Provider-jFWpR0CX.js";
const x = {
  visible: !1,
  position: p.ZERO,
  cursor: p.ZERO,
  keys: []
}, R = O.BE("menu-context", "container"), G = (n) => {
  let t = n;
  for (; !t.classList.contains(A); ) {
    if (t.classList.contains(R) || t.parentElement == null) return n;
    t = t.parentElement;
  }
  return t;
}, H = (n) => {
  const t = G(n);
  if (t == null) return [];
  const o = Array.from(
    t.parentElement?.querySelectorAll(`.${j}`) ?? []
  );
  return o.includes(t) ? o : [t];
}, k = [
  { targetCorner: r.BOTTOM_RIGHT, dialogCorner: r.TOP_LEFT },
  { targetCorner: r.BOTTOM_LEFT, dialogCorner: r.TOP_RIGHT },
  { targetCorner: r.TOP_RIGHT, dialogCorner: r.BOTTOM_LEFT },
  { targetCorner: r.TOP_LEFT, dialogCorner: r.BOTTOM_RIGHT }
], Y = () => {
  const n = M(null), [t, o] = b(x), m = E((e) => {
    const s = p.construct(e);
    let u = [];
    if (typeof e == "object" && "preventDefault" in e) {
      e.preventDefault(), e.stopPropagation();
      const f = H(e.target);
      u = _.unique(f.map((d) => d.id).filter((d) => d.length > 0));
    }
    o({ visible: !0, keys: u, position: s, cursor: s });
  }, []), i = E(() => {
    const e = n.current;
    e != null && o((s) => {
      if (!s.visible) return s;
      const { adjustedDialog: u } = S({
        container: C.construct(0, 0, window.innerWidth, window.innerHeight),
        dialog: C.construct(e),
        target: C.construct(s.cursor, 0, 0),
        prefer: k
      }), f = C.topLeft(u);
      return p.equals(s.position, f) ? s : { ...s, position: f };
    });
  }, []), c = L(i, { enabled: t.visible }), a = F(n, c);
  I(i, { enabled: t.visible });
  const l = () => o(x);
  return P({ ref: n, onClickOutside: l }), {
    ...t,
    close: l,
    open: m,
    ref: a,
    className: R
  };
}, q = ({
  ref: n,
  menu: t,
  visible: o,
  open: m,
  close: i,
  position: c,
  keys: a,
  className: l,
  cursor: e,
  style: s,
  onClick: u,
  ...f
}) => o ? g(
  /* @__PURE__ */ T.jsx(
    w,
    {
      className: O(y, O.bordered(), l),
      ref: n,
      style: { ...p.css(c), ...s },
      onClick: (d) => {
        i(), u?.(d);
      },
      gap: "tiny",
      ...f,
      children: t?.({ keys: a, visible: o, position: c, cursor: e })
    }
  ),
  document.body
) : null, tt = ({
  menu: n,
  children: t,
  ...o
}) => /* @__PURE__ */ T.jsxs(T.Fragment, { children: [
  /* @__PURE__ */ T.jsx(q, { menu: n, ...o }),
  t
] }), [D, et] = B({
  defaultValue: { onClick: () => {
  }, selected: "" },
  displayName: "Menu.Context"
}), nt = ({
  children: n,
  onChange: t,
  level: o,
  gap: m,
  background: i,
  value: c = ""
}) => {
  const a = E(
    (e) => {
      typeof t == "function" ? t(e) : t && e in t && t[e](e);
    },
    [t]
  ), l = N(
    () => ({
      onClick: a,
      selected: c,
      level: o,
      gap: m,
      background: i
    }),
    [c, a, o, m, i]
  );
  return /* @__PURE__ */ T.jsx(D, { value: l, children: n });
};
export {
  tt as C,
  nt as M,
  Y as a,
  et as u
};
