import { j as A } from "./jsx-runtime-OVHDjVDe.js";
import { j as r, Q as G, C as m, K as t, h as ee, p as Ee } from "./css-D6ZBcSwh.js";
import { useCallback as p, useEffect as Y, use as xe, useRef as N, createContext as se, useContext as Te, useState as he, useMemo as te } from "react";
import { a as ie } from "./Box-D5Qv__kr.js";
import { c as k, f as re } from "./Editable-CxgdRDPb.js";
import { u as ye } from "./hooks-15yB7IKE.js";
import { a as Oe, E as pe } from "./Text-BzTkWLeU.js";
import { b as Re } from "./types-BNX_Jj6k.js";
import "./Provider--RxgAoVG.js";
import { h as be } from "./provider-C8In649C.js";
import { o as we } from "./debounce-CW9Q-DsR.js";
const Le = ({
  ref: e,
  onClickOutside: n,
  exclude: s
}) => {
  const g = k(s), o = p(
    (c) => {
      const i = e.current, l = r.construct(window.document.documentElement), d = G.construct(c), a = g.current;
      if (a != null) {
        if (typeof a == "function") {
          if (a(c)) return;
        } else if (a.some((u) => u.current?.contains(c.target))) return;
      }
      i == null || i.contains(c.target) || r.contains(i, d) || !r.contains(l, d) || n();
    },
    [n]
  );
  Y(() => (document.addEventListener("mousedown", o), () => document.removeEventListener("mousedown", o)), [o]);
}, Ne = (e) => {
  const n = xe(e);
  if (n == null)
    throw new be(
      `useRequiredContext: context value is null for ${e.displayName}`
    );
  return n;
}, ne = (e, n = {}) => {
  const { triggers: s = [], debounce: g = 0, enabled: o = !0 } = n, c = N(r.ZERO), i = N(null), l = N(null), d = Oe(() => s, pe, [
    s
  ]), a = p(
    (u) => {
      l.current != null && l.current.disconnect(), c.current ??= r.ZERO;
      const C = we(() => {
        const f = r.construct(u);
        Be(d, c.current, f) && (c.current = f, e(f, i.current));
      }, g);
      l.current = new ResizeObserver(C), l.current.observe(u);
    },
    [d, e, g]
  );
  return Y(() => (i.current != null && o && a(i.current), () => l.current?.disconnect()), [a, o]), p(
    (u) => {
      i.current = u, u != null && o && a(u);
    },
    [a]
  );
}, Be = (e, n, s) => e.length === 0 ? !r.equals(s, n) : !!(e.includes("x") && r.width(n) !== r.width(s) || e.includes("y") && r.height(n) !== r.height(s)), Me = (e) => {
  const n = k(e);
  Y(() => {
    const s = () => n.current(
      r.construct(window.innerWidth, window.innerHeight),
      window.document.documentElement
    );
    return window.addEventListener("resize", s), () => window.removeEventListener("resize", s);
  }, []);
}, H = m.BE("dialog", "bg"), Ke = ({
  children: e,
  visible: n,
  ...s
}) => /* @__PURE__ */ A.jsx(
  ie,
  {
    className: m(H, m.visible(n)),
    empty: !0,
    align: "center",
    ...s,
    children: e
  }
), M = (e) => e == null ? { x: void 0, y: void 0 } : typeof e != "string" ? e : Ee.isX(e) ? { x: e, y: void 0 } : { x: void 0, y: e }, z = (e) => typeof e == "string" || "x" in e || "y" in e ? { targetCorner: e, dialogCorner: void 0 } : e, _e = ({
  initial: e,
  prefer: n = [],
  disable: s = []
}) => {
  const g = ee.toArray(n).map(z), o = ee.toArray(s).map(z), c = [];
  if (e != null) {
    const i = z(e), l = M(i.targetCorner), d = M(i.dialogCorner);
    t.XY_LOCATIONS.forEach((u) => {
      t.xyEquals(u, t.CENTER) || l.x != null && u.x !== l.x || l.y != null && u.y !== l.y || t.XY_LOCATIONS.forEach((C) => {
        if (t.xyEquals(C, t.CENTER) || d.x != null && C.x !== d.x || d.y != null && C.y !== d.y) return;
        const f = { targetCorner: u, dialogCorner: C };
        c.push(f);
      });
    });
    const a = c.filter((u) => !X(u, o));
    return a.length == 0 ? c.slice(0, 1) : a;
  }
  return g.forEach((i) => {
    const l = M(i.targetCorner), d = M(i.dialogCorner);
    t.XY_LOCATIONS.forEach((a) => {
      t.xyEquals(a, t.CENTER) || t.xyMatches(a, l) && t.XY_LOCATIONS.forEach((u) => {
        if (t.xyEquals(u, t.CENTER) || !t.xyMatches(u, d)) return;
        const C = { targetCorner: a, dialogCorner: u };
        !X(C, o) && !c.some(
          (f) => t.xyEquals(f.targetCorner, a) && t.xyEquals(f.dialogCorner, u)
        ) && c.push(C);
      });
    });
  }), t.XY_LOCATIONS.forEach((i) => {
    t.xyEquals(i, t.CENTER) || t.XY_LOCATIONS.forEach((l) => {
      if (t.xyEquals(l, t.CENTER)) return;
      const d = { targetCorner: i, dialogCorner: l };
      !X(d, o) && !c.some(
        (a) => t.xyEquals(a.targetCorner, i) && t.xyEquals(a.dialogCorner, l)
      ) && c.push(d);
    });
  }), c;
}, X = (e, n) => n.some((s) => {
  const g = M(s.targetCorner), o = M(s.dialogCorner);
  return t.xyEquals(s.dialogCorner, t.CENTER) ? t.xyMatches(e.targetCorner, g) : t.xyMatches(e.targetCorner, g) && t.xyMatches(e.dialogCorner, o);
}), Pe = ({
  container: e,
  target: n,
  dialog: s,
  initial: g,
  prefer: o,
  disable: c,
  offset: i
}) => {
  const l = _e({ initial: g, prefer: o, disable: c }), d = r.construct(e), a = r.construct(n), u = r.construct(s), C = l.reduce(
    (f, T) => {
      const h = ve({
        ...T,
        container: d,
        target: a,
        dialog: u,
        offset: i
      });
      return h.area > f.area ? h : f;
    },
    {
      area: -1 / 0,
      targetCorner: t.CENTER,
      dialogCorner: t.CENTER,
      adjustedDialog: u
    }
  );
  return {
    targetCorner: C.targetCorner,
    dialogCorner: C.dialogCorner,
    adjustedDialog: C.adjustedDialog
  };
}, ve = ({
  targetCorner: e,
  dialogCorner: n,
  container: s,
  target: g,
  dialog: o,
  offset: c
}) => {
  let i = r.xyLoc(g, e);
  if (c != null) {
    const a = G.construct(c);
    e.x === n.x && (a.x = 0), e.y === n.y && (a.y = 0), i = G.translate(i, e, a);
  }
  const l = r.constructWithAlternateRoot(
    i.x,
    i.y,
    r.width(o),
    r.height(o),
    n,
    t.TOP_LEFT
  );
  let d = r.area(r.intersection(l, s));
  return r.area(r.intersection(g, l)) !== 0 && (d = 0), {
    targetCorner: e,
    dialogCorner: n,
    adjustedDialog: l,
    area: d
  };
}, Ue = m.BM("context", "selected"), Qe = m.BE("context", "target"), Ie = m.B("menu-context"), Se = {
  targetCorner: t.BOTTOM_LEFT,
  dialogCorner: t.BOTTOM_LEFT,
  style: {},
  modalPosition: "base"
}, ae = se({
  close: () => {
  },
  open: () => {
  },
  toggle: () => {
  },
  variant: "floating",
  visible: !1,
  location: t.BOTTOM_LEFT
}), ce = se(null), Je = () => Ne(ce), et = () => Te(ae), qe = [["Escape"]], Ae = (e, n, s) => {
  if (s == null) return !1;
  const o = Math.abs(r.left(n) - r.left(s)) <= 1 && Math.abs(r.top(n) - r.top(s)) <= 1;
  if (e === "floating") return o;
  const c = Math.abs(r.width(n) - r.width(s)) <= 1;
  return o && c;
}, oe = [
  {
    targetCorner: t.BOTTOM_LEFT,
    dialogCorner: t.TOP_LEFT
  },
  {
    targetCorner: t.TOP_LEFT,
    dialogCorner: t.BOTTOM_LEFT
  },
  {
    targetCorner: t.BOTTOM_RIGHT,
    dialogCorner: t.TOP_RIGHT
  },
  {
    targetCorner: t.TOP_RIGHT,
    dialogCorner: t.BOTTOM_RIGHT
  },
  {
    targetCorner: t.TOP_RIGHT,
    dialogCorner: t.TOP_LEFT
  }
], je = (e, n) => e / n > 0.8 ? "slammed" : e / n > 0.6 ? "shifted" : "base", De = ({
  children: e,
  location: n,
  onPointerEnter: s,
  className: g,
  variant: o = "floating",
  maxHeight: c,
  zIndex: i,
  initialVisible: l = !1,
  visible: d,
  onVisibleChange: a,
  modalPosition: u = "base",
  ...C
}) => {
  const [f, T] = Re({
    initial: l,
    value: d,
    onChange: a
  }), h = p(() => T(!1), [T]), V = p(() => T(!0), [T]), W = p(() => T((E) => !E), [T]), v = k(f), _ = N(null), j = N(void 0), Z = N(void 0), y = N(null), [{ targetCorner: R, dialogCorner: I, style: $, modalPosition: K }, U] = he({ ...Se, modalPosition: u }), D = p(() => {
    if (_.current == null || y.current == null || !v.current)
      return;
    const E = r.construct(_.current);
    if (r.areaIsZero(E) && o !== "modal") return T(!1);
    let x = r.construct(y.current);
    o === "connected" && (x = r.resize(x, "x", r.width(E)));
    const b = r.construct(0, 0, window.innerWidth, window.innerHeight);
    if (o === "modal") {
      const q = je(
        r.height(x),
        r.height(b)
      );
      return U((F) => q === F.modalPosition ? F : { ...F, modalPosition: q });
    }
    let w = oe;
    j.current != null && (w = [j.current, ...oe]);
    const { adjustedDialog: O, ...P } = Pe({
      target: E,
      dialog: x,
      container: b,
      prefer: w,
      initial: n,
      offset: 3
    });
    j.current = P;
    const { targetCorner: S, dialogCorner: J } = P, B = r.round(O);
    if (Ae(o, B, Z.current)) return;
    Z.current = B;
    const L = {};
    L.left = r.left(B), S.y === "top" && J.x === S.x ? L.bottom = r.height(b) - r.bottom(B) : L.top = r.top(B), o === "connected" && (L.width = r.width(B)), typeof c == "number" && (L.maxHeight = c), f && (L.zIndex = i), U((q) => ({ ...q, targetCorner: S, dialogCorner: J, style: L }));
  }, [n, o]), le = ne(D, { enabled: f }), Q = re(y, le), ue = ne(D, { enabled: f }), de = re(_, ue);
  Me(D);
  const fe = p(
    (E) => {
      if (!v.current || y.current == null || _.current == null)
        return !0;
      if (o !== "modal") {
        const w = y.current.getElementsByClassName(m.visible(!0));
        let O = w != null && w.length > 0;
        return O || (O = _.current.contains(E.target)), document.getElementsByClassName(Ie).length > 0 && (O = !0), O || E.stopImmediatePropagation(), O;
      }
      if (!(E.target instanceof HTMLElement)) return !0;
      let x = E.target;
      return x.className.includes(H) && (x = x.children[0]), x !== y.current;
    },
    [i, v]
  ), ge = p(
    ({ stage: E }) => {
      if (E !== "start" || v.current === !1 || y.current == null)
        return;
      if (o !== "modal") return h();
      const x = y.current, b = Array.from(document.getElementsByClassName(H)), w = b.findIndex((P) => P.contains(x)), O = x.getElementsByClassName(m.visible(!0));
      if (w === b.length - 1 && O.length === 0)
        return h();
    },
    [h, o]
  );
  Le({ ref: y, exclude: fe, onClickOutside: h }), ye({ triggers: qe, callback: ge, loose: !0 });
  const me = te(
    () => ({
      ref: Q,
      targetCorner: R,
      dialogCorner: I,
      style: $,
      modalPosition: K
    }),
    [Q, R, I, $, K]
  ), Ce = te(
    () => ({
      close: h,
      open: V,
      toggle: W,
      visible: f,
      onPointerEnter: s,
      variant: o,
      location: R
    }),
    [h, V, W, f, R]
  );
  return /* @__PURE__ */ A.jsx(ae.Provider, { value: Ce, children: /* @__PURE__ */ A.jsx(ce.Provider, { value: me, children: /* @__PURE__ */ A.jsx(
    ie,
    {
      ...C,
      ref: de,
      className: m(
        g,
        m.BE("dialog", "frame"),
        m.visible(f),
        m.M(o),
        m.loc(R.x),
        m.loc(R.y),
        m.BEM("dialog", "dialog", I.x),
        m.BEM("dialog", "dialog", I.y)
      ),
      y: !0,
      reverse: R.y === "top",
      children: e
    }
  ) }) });
};
De.displayName = "Dialog.Frame";
export {
  Ke as B,
  Ie as C,
  De as F,
  Ue as a,
  Qe as b,
  Pe as c,
  Je as d,
  Le as e,
  Ne as f,
  ne as g,
  Me as h,
  M as p,
  et as u
};
