import t, { useId as x, useMemo as P, Fragment as G, useEffect as ae, useContext as le, createContext as ne, useRef as L, useReducer as ie, createRef as se } from "react";
import { useEscape as ue } from "../../hooks/use-escape.js";
import { useEvent as C } from "../../hooks/use-event.js";
import { useInertOthers as de } from "../../hooks/use-inert-others.js";
import { useIsTouchDevice as pe } from "../../hooks/use-is-touch-device.js";
import { useOnDisappear as ce } from "../../hooks/use-on-disappear.js";
import { useOutsideClick as me } from "../../hooks/use-outside-click.js";
import { useOwnerDocument as fe } from "../../hooks/use-owner.js";
import { MainTreeProvider as U, useMainTreeNode as ge, useRootContainers as ve } from "../../hooks/use-root-containers.js";
import { useScrollLock as he } from "../../hooks/use-scroll-lock.js";
import { useServerHandoffComplete as Ee } from "../../hooks/use-server-handoff-complete.js";
import { useSyncRefs as N } from "../../hooks/use-sync-refs.js";
import { CloseProvider as $e } from "../../internal/close-provider.js";
import { useOpenClosed as V, State as b, ResetOpenClosedProvider as De } from "../../internal/open-closed.js";
import { ForcePortalRoot as j } from "../../internal/portal-force-root.js";
import { match as Ce } from "../../utils/match.js";
import { forwardRefWithAs as T, useRender as y, RenderFeatures as z } from "../../utils/render.js";
import { Description as Pe, useDescriptions as Te } from "../description/description.js";
import { FocusTrapFeatures as D, FocusTrap as we } from "../focus-trap/focus-trap.js";
import { useNestedPortals as Re, Portal as be, PortalGroup as ye } from "../portal/portal.js";
import { TransitionChild as W, Transition as Oe } from "../transition/transition.js";
var Ie = ((e) => (e[e.Open = 0] = "Open", e[e.Closed = 1] = "Closed", e))(Ie || {}), Fe = ((e) => (e[e.SetTitleId = 0] = "SetTitleId", e))(Fe || {});
let Se = { 0(e, o) {
  return e.titleId === o.id ? e : { ...e, titleId: o.id };
} }, Y = ne(null);
Y.displayName = "DialogContext";
function O(e) {
  let o = le(Y);
  if (o === null) {
    let n = new Error(`<${e} /> is missing a parent <Dialog /> component.`);
    throw Error.captureStackTrace && Error.captureStackTrace(n, O), n;
  }
  return o;
}
function ke(e, o) {
  return Ce(o.type, Se, e, o);
}
let A = T(function(e, o) {
  let n = x(), { id: i = `headlessui-dialog-${n}`, open: r, onClose: u, initialFocus: s, role: a = "dialog", autoFocus: c = !0, __demoMode: d = !1, unmount: m = !1, ...I } = e, w = L(!1);
  a = (function() {
    return a === "dialog" || a === "alertdialog" ? a : (w.current || (w.current = !0, console.warn(`Invalid role [${a}] passed to <Dialog />. Only \`dialog\` and and \`alertdialog\` are supported. Using \`dialog\` instead.`)), "dialog");
  })();
  let g = V();
  r === void 0 && g !== null && (r = (g & b.Open) === b.Open);
  let p = L(null), F = N(p, o), E = fe(p), f = r ? 0 : 1, [R, q] = ie(ke, { titleId: null, descriptionId: null, panelRef: se() }), v = C(() => u(!1)), M = C((l) => q({ type: 0, id: l })), h = Ee() ? f === 0 : !1, [K, Q] = Re(), X = { get current() {
    var l;
    return (l = R.panelRef.current) != null ? l : p.current;
  } }, S = ge(), { resolveContainers: k } = ve({ mainTreeNode: S, portals: K, defaultContainers: [X] }), B = g !== null ? (g & b.Closing) === b.Closing : !1;
  de(d || B ? !1 : h, { allowed: C(() => {
    var l, _;
    return [(_ = (l = p.current) == null ? void 0 : l.closest("[data-headlessui-portal]")) != null ? _ : null];
  }), disallowed: C(() => {
    var l;
    return [(l = S == null ? void 0 : S.closest("body > *:not(#headlessui-portal-root)")) != null ? l : null];
  }) }), me(h, k, (l) => {
    l.preventDefault(), v();
  }), ue(h, E == null ? void 0 : E.defaultView, (l) => {
    l.preventDefault(), l.stopPropagation(), document.activeElement && "blur" in document.activeElement && typeof document.activeElement.blur == "function" && document.activeElement.blur(), v();
  }), he(d || B ? !1 : h, E, k), ce(h, p, v);
  let [Z, J] = Te(), ee = P(() => [{ dialogState: f, close: v, setTitleId: M, unmount: m }, R], [f, R, v, M, m]), H = P(() => ({ open: f === 0 }), [f]), oe = { ref: F, id: i, role: a, tabIndex: -1, "aria-modal": d ? void 0 : f === 0 ? !0 : void 0, "aria-labelledby": R.titleId, "aria-describedby": Z, unmount: m }, te = !pe(), $ = D.None;
  h && !d && ($ |= D.RestoreFocus, $ |= D.TabLock, c && ($ |= D.AutoFocus), te && ($ |= D.InitialFocus));
  let re = y();
  return t.createElement(De, null, t.createElement(j, { force: !0 }, t.createElement(be, null, t.createElement(Y.Provider, { value: ee }, t.createElement(ye, { target: p }, t.createElement(j, { force: !1 }, t.createElement(J, { slot: H }, t.createElement(Q, null, t.createElement(we, { initialFocus: s, initialFocusFallback: p, containers: k, features: $ }, t.createElement($e, { value: v }, re({ ourProps: oe, theirProps: I, slot: H, defaultTag: xe, features: Ne, visible: f === 0, name: "Dialog" })))))))))));
}), xe = "div", Ne = z.RenderStrategy | z.Static;
function Ye(e, o) {
  let { transition: n = !1, open: i, ...r } = e, u = V(), s = e.hasOwnProperty("open") || u !== null, a = e.hasOwnProperty("onClose");
  if (!s && !a) throw new Error("You have to provide an `open` and an `onClose` prop to the `Dialog` component.");
  if (!s) throw new Error("You provided an `onClose` prop to the `Dialog`, but forgot an `open` prop.");
  if (!a) throw new Error("You provided an `open` prop to the `Dialog`, but forgot an `onClose` prop.");
  if (!u && typeof e.open != "boolean") throw new Error(`You provided an \`open\` prop to the \`Dialog\`, but the value is not a boolean. Received: ${e.open}`);
  if (typeof e.onClose != "function") throw new Error(`You provided an \`onClose\` prop to the \`Dialog\`, but the value is not a function. Received: ${e.onClose}`);
  return (i !== void 0 || n) && !r.static ? t.createElement(U, null, t.createElement(Oe, { show: i, transition: n, unmount: r.unmount }, t.createElement(A, { ref: o, ...r }))) : t.createElement(U, null, t.createElement(A, { ref: o, open: i, ...r }));
}
let Me = "div";
function Be(e, o) {
  let n = x(), { id: i = `headlessui-dialog-panel-${n}`, transition: r = !1, ...u } = e, [{ dialogState: s, unmount: a }, c] = O("Dialog.Panel"), d = N(o, c.panelRef), m = P(() => ({ open: s === 0 }), [s]), I = C((E) => {
    E.stopPropagation();
  }), w = { ref: d, id: i, onClick: I }, g = r ? W : G, p = r ? { unmount: a } : {}, F = y();
  return t.createElement(g, { ...p }, F({ ourProps: w, theirProps: u, slot: m, defaultTag: Me, name: "Dialog.Panel" }));
}
let He = "div";
function _e(e, o) {
  let { transition: n = !1, ...i } = e, [{ dialogState: r, unmount: u }] = O("Dialog.Backdrop"), s = P(() => ({ open: r === 0 }), [r]), a = { ref: o, "aria-hidden": !0 }, c = n ? W : G, d = n ? { unmount: u } : {}, m = y();
  return t.createElement(c, { ...d }, m({ ourProps: a, theirProps: i, slot: s, defaultTag: He, name: "Dialog.Backdrop" }));
}
let Le = "h2";
function Ue(e, o) {
  let n = x(), { id: i = `headlessui-dialog-title-${n}`, ...r } = e, [{ dialogState: u, setTitleId: s }] = O("Dialog.Title"), a = N(o);
  ae(() => (s(i), () => s(null)), [i, s]);
  let c = P(() => ({ open: u === 0 }), [u]), d = { ref: a, id: i };
  return y()({ ourProps: d, theirProps: r, slot: c, defaultTag: Le, name: "Dialog.Title" });
}
let je = T(Ye), ze = T(Be);
T(_e);
let Ae = T(Ue), mo = Object.assign(je, { Panel: ze, Title: Ae, Description: Pe });
export {
  mo as Dialog,
  ze as DialogPanel,
  Ae as DialogTitle
};
