import { j as F } from "./jsx-runtime-BbD0H88J.js";
import { E as r, A as G, F as me, C as m, M as t, d as ee, v as Ee } from "./css-BkBrZtKZ.js";
import { useCallback as p, useEffect as Y, useRef as w, useState as Ce, useMemo as te } from "react";
import { a as xe, F as Te, c as se } from "./Text-XeD58Fq8.js";
import { a as ie } from "./Box-DLO7l9hM.js";
import { c as k, f as re } from "./Editable-DjQORuR1.js";
import { u as he } from "./hooks-BeSuo_di.js";
import { b as ye } from "./types-B5-Tni3G.js";
import "./Provider-BkxUzTWS.js";
const Oe = ({
  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, n = {}) => {
  const { triggers: s = [], debounce: g = 0, enabled: o = !0 } = n, c = w(r.ZERO), i = w(null), l = w(null), d = xe(() => s, Te, [
    s
  ]), a = p(
    (u) => {
      l.current != null && l.current.disconnect(), c.current ??= r.ZERO;
      const E = me(() => {
        const f = r.construct(u);
        pe(d, c.current, f) && (c.current = f, e(f, i.current));
      }, g);
      l.current = new ResizeObserver(E), 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]
  );
}, pe = (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)), Re = (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"), Ye = ({
  children: e,
  visible: n,
  ...s
}) => /* @__PURE__ */ F.jsx(
  ie,
  {
    className: m(H, m.visible(n)),
    empty: !0,
    align: "center",
    ...s,
    children: e
  }
), B = (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, be = ({
  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 = B(i.targetCorner), d = B(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((E) => {
        if (t.xyEquals(E, t.CENTER) || d.x != null && E.x !== d.x || d.y != null && E.y !== d.y) return;
        const f = { targetCorner: u, dialogCorner: E };
        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 = B(i.targetCorner), d = B(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 E = { targetCorner: a, dialogCorner: u };
        !X(E, o) && !c.some(
          (f) => t.xyEquals(f.targetCorner, a) && t.xyEquals(f.dialogCorner, u)
        ) && c.push(E);
      });
    });
  }), 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 = B(s.targetCorner), o = B(s.dialogCorner);
  return t.xyEquals(s.dialogCorner, t.CENTER) ? t.xyMatches(e.targetCorner, g) : t.xyMatches(e.targetCorner, g) && t.xyMatches(e.dialogCorner, o);
}), Ne = ({
  container: e,
  target: n,
  dialog: s,
  initial: g,
  prefer: o,
  disable: c,
  offset: i
}) => {
  const l = be({ initial: g, prefer: o, disable: c }), d = r.construct(e), a = r.construct(n), u = r.construct(s), E = l.reduce(
    (f, T) => {
      const h = Le({
        ...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: E.targetCorner,
    dialogCorner: E.dialogCorner,
    adjustedDialog: E.adjustedDialog
  };
}, Le = ({
  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
  };
}, ke = m.BM("context", "selected"), Ve = m.BE("context", "target"), we = m.B("menu-context"), Me = {
  targetCorner: t.BOTTOM_LEFT,
  dialogCorner: t.BOTTOM_LEFT,
  style: {},
  modalPosition: "base"
}, [Be, We] = se({
  defaultValue: {
    close: () => {
    },
    location: t.BOTTOM_LEFT,
    open: () => {
    },
    toggle: () => {
    },
    variant: "floating",
    visible: !1
  },
  displayName: "Dialog.Context"
}), [_e, Ze] = se({
  displayName: "Dialog.InternalContext",
  providerName: "Dialog.Frame"
}), Pe = [["Escape"]], ve = (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
  }
], Ie = (e, n) => e / n > 0.8 ? "slammed" : e / n > 0.6 ? "shifted" : "base", Se = ({
  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",
  ...E
}) => {
  const [f, T] = ye({
    initial: l,
    value: d,
    onChange: a
  }), h = p(() => T(!1), [T]), V = p(() => T(!0), [T]), W = p(() => T((C) => !C), [T]), v = k(f), _ = w(null), D = w(void 0), Z = w(void 0), y = w(null), [{ targetCorner: R, dialogCorner: I, style: U, modalPosition: K }, J] = Ce({ ...Me, modalPosition: u }), q = p(() => {
    if (_.current == null || y.current == null || !v.current)
      return;
    const C = r.construct(_.current);
    if (r.areaIsZero(C) && o !== "modal") return T(!1);
    let x = r.construct(y.current);
    o === "connected" && (x = r.resize(x, "x", r.width(C)));
    const b = r.construct(0, 0, window.innerWidth, window.innerHeight);
    if (o === "modal") {
      const A = Ie(
        r.height(x),
        r.height(b)
      );
      return J((j) => A === j.modalPosition ? j : { ...j, modalPosition: A });
    }
    let N = oe;
    D.current != null && (N = [D.current, ...oe]);
    const { adjustedDialog: O, ...P } = Ne({
      target: C,
      dialog: x,
      container: b,
      prefer: N,
      initial: n,
      offset: 3
    });
    D.current = P;
    const { targetCorner: S, dialogCorner: $ } = P, M = r.round(O);
    if (ve(o, M, Z.current)) return;
    Z.current = M;
    const L = {};
    L.left = r.left(M), S.y === "top" && $.x === S.x ? L.bottom = r.height(b) - r.bottom(M) : L.top = r.top(M), o === "connected" && (L.width = r.width(M)), typeof c == "number" && (L.maxHeight = c), f && (L.zIndex = i), J((A) => ({ ...A, targetCorner: S, dialogCorner: $, style: L }));
  }, [n, o, T]), ae = ne(q, { enabled: f }), Q = re(y, ae), ce = ne(q, { enabled: f }), le = re(_, ce);
  Re(q);
  const ue = p(
    (C) => {
      if (!v.current || y.current == null || _.current == null)
        return !0;
      if (o !== "modal") {
        const N = y.current.getElementsByClassName(m.visible(!0));
        let O = N != null && N.length > 0;
        return O || (O = _.current.contains(C.target)), document.getElementsByClassName(we).length > 0 && (O = !0), O || C.stopImmediatePropagation(), O;
      }
      if (!(C.target instanceof HTMLElement)) return !0;
      let x = C.target;
      return x.className.includes(H) && (x = x.children[0]), x !== y.current;
    },
    [i, v]
  ), de = p(
    ({ stage: C }) => {
      if (C !== "start" || v.current === !1 || y.current == null)
        return;
      if (o !== "modal") return h();
      const x = y.current, b = Array.from(document.getElementsByClassName(H)), N = b.findIndex((P) => P.contains(x)), O = x.getElementsByClassName(m.visible(!0));
      if (N === b.length - 1 && O.length === 0)
        return h();
    },
    [h, o]
  );
  Oe({ ref: y, exclude: ue, onClickOutside: h }), he({ triggers: Pe, callback: de, loose: !0 });
  const fe = te(
    () => ({
      ref: Q,
      targetCorner: R,
      dialogCorner: I,
      style: U,
      modalPosition: K
    }),
    [Q, R, I, U, K]
  ), ge = te(
    () => ({
      close: h,
      open: V,
      toggle: W,
      visible: f,
      onPointerEnter: s,
      variant: o,
      location: R
    }),
    [h, V, W, f, R]
  );
  return /* @__PURE__ */ F.jsx(Be, { value: ge, children: /* @__PURE__ */ F.jsx(_e, { value: fe, children: /* @__PURE__ */ F.jsx(
    ie,
    {
      ...E,
      ref: le,
      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
    }
  ) }) });
};
Se.displayName = "Dialog.Frame";
export {
  Ye as B,
  we as C,
  Se as F,
  Ne as a,
  Ze as b,
  ke as c,
  Ve as d,
  Oe as e,
  ne as f,
  Re as g,
  B as p,
  We as u
};
