import { b as Q, d as T, f as _, g as x, h as q, l as A, p as d, r as $, v as u } from "./vendor-preact-DThS2ZQ_.js";
import { n as cn } from "./loadingIcon-B0ysgiNq.js";
import { o as CloseIcon } from "./icons-CJJbIpBq.js";
import { a as useModalPortalTarget, i as useModalFocusCapture, n as useModalBodyScrollLock, o as useModalTabCycle, r as useModalEscapeDismiss, t as MODAL_TITLE_ID } from "./modal.hooks-ZrzmHAUa.js";
//#region src/shared/modal/modal.tsx
var ModalContext = Q(null);
function useModalContext() {
	const ctx = x(ModalContext);
	if (!ctx) throw new Error("Modal compound components must be used within Modal.Root");
	return ctx;
}
function ModalRoot({ open, children, onOpenChange, dismissible = true, portalContainer, class: className, hasBackdrop = true, manageInitialFocus = true, returnFocusRef }) {
	const portalTarget = useModalPortalTarget(open, portalContainer ?? void 0);
	const [hasDescription, setHasDescription] = d(false);
	const dialogRef = A(null);
	const requestClose = q(() => {
		if (!dismissible) return;
		onOpenChange?.(false);
	}, [dismissible, onOpenChange]);
	const contextValue = T(() => ({
		setHasDescription,
		dismissible,
		requestClose
	}), [dismissible, requestClose]);
	const dialogActive = open && Boolean(portalTarget);
	useModalBodyScrollLock(open);
	useModalEscapeDismiss(open, dismissible, requestClose);
	useModalFocusCapture(dialogActive, dialogRef, manageInitialFocus, returnFocusRef);
	useModalTabCycle(dialogActive, dialogRef);
	if (!portalTarget || !open) return null;
	return $(/* @__PURE__ */ u(ModalContext.Provider, {
		value: contextValue,
		children: /* @__PURE__ */ u("div", {
			class: cn("IncodeModalRoot", className),
			"data-incode-modal": "",
			children: [hasBackdrop && /* @__PURE__ */ u("div", {
				class: "IncodeModalBackdrop",
				"aria-hidden": "true",
				onMouseDown: () => {
					requestClose();
				}
			}), /* @__PURE__ */ u("div", {
				ref: dialogRef,
				class: "IncodeModalDialog",
				role: "dialog",
				"aria-modal": "true",
				"aria-labelledby": MODAL_TITLE_ID,
				"aria-describedby": hasDescription ? "incode-modal-description" : void 0,
				tabIndex: -1,
				onMouseDown: (event) => {
					event.stopPropagation();
				},
				children
			})]
		})
	}), portalTarget);
}
function ModalContent({ children, class: className }) {
	return /* @__PURE__ */ u("div", {
		class: cn("IncodeModalContent", className),
		children
	});
}
function ModalClose({ "aria-label": ariaLabel = "Close", title: titleAttr = "Close", class: className, onClick }) {
	const { dismissible, requestClose } = useModalContext();
	if (!dismissible) return null;
	const handleClose = async () => {
		if (onClick) await onClick();
		requestClose();
	};
	return /* @__PURE__ */ u("button", {
		type: "button",
		class: cn("IncodeModalClose", className),
		"aria-label": ariaLabel,
		title: titleAttr,
		onClick: handleClose,
		children: /* @__PURE__ */ u(CloseIcon, {
			height: 32,
			width: 32,
			fillColor: "var(--icon-neutral-300)"
		})
	});
}
function ModalIcon({ children, class: className }) {
	return /* @__PURE__ */ u("div", {
		class: cn("IncodeModalIcon", className),
		children
	});
}
function ModalTitleSection({ children, align = "center" }) {
	const className = {
		center: "IncodeModalTitleSectionCenter",
		left: "IncodeModalTitleSectionLeft"
	}[align];
	return /* @__PURE__ */ u("div", {
		class: className,
		children
	});
}
function ModalTitle({ children, ref, class: className }) {
	return /* @__PURE__ */ u("h2", {
		id: MODAL_TITLE_ID,
		ref,
		tabIndex: -1,
		className: cn("IncodeModalTitle", className),
		children
	});
}
function ModalSubtitle({ children, class: className }) {
	const { setHasDescription } = useModalContext();
	_(() => {
		setHasDescription(true);
		return () => setHasDescription(false);
	}, [setHasDescription]);
	return /* @__PURE__ */ u("p", {
		id: "incode-modal-description",
		className: cn("IncodeModalSubtitle", className),
		children
	});
}
function ModalActions({ children, class: className, orientation = "vertical" }) {
	return /* @__PURE__ */ u("div", {
		class: cn(orientation === "vertical" ? "IncodeModalActionsVertical" : "IncodeModalActionsHorizontal", className),
		role: "group",
		children
	});
}
var Modal = {
	Root: ModalRoot,
	Content: ModalContent,
	Close: ModalClose,
	Icon: ModalIcon,
	TitleSection: ModalTitleSection,
	Title: ModalTitle,
	Subtitle: ModalSubtitle,
	Actions: ModalActions
};
//#endregion
export { Modal as t };
