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