/*! ratio-ui · design system for knowledge sharing · https://github.com/losol/ratio-ui · (c) 2026 Losol AS · MPL-2.0 */
import { Button } from "../../core/Button/Button2.js";
import "../../core/Button/index.js";
import { Dialog } from "./Dialog.js";
import { Input } from "../../forms/Input/Input.js";
import { useEffect, useId, useRef, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/layout/Dialog/AlertDialog.tsx
var variantPrimaryStyle = {
	confirmation: "primary",
	information: "primary",
	warning: "primary",
	destructive: "danger",
	error: "danger"
};
var AlertDialog = ({ isOpen, onClose, title, children, variant = "confirmation", size = "sm", testId, primaryActionLabel, secondaryActionLabel, cancelLabel, isPrimaryActionDisabled, isSecondaryActionDisabled, autoFocusButton, confirmText, confirmLabel, onPrimaryAction, onSecondaryAction, onCancel }) => {
	const primaryRef = useRef(null);
	const secondaryRef = useRef(null);
	const cancelRef = useRef(null);
	const confirmInputRef = useRef(null);
	const confirmInputId = useId();
	const confirmLabelId = useId();
	const trimmedConfirmText = confirmText?.trim();
	const gateActive = trimmedConfirmText !== void 0 && trimmedConfirmText !== "";
	const [confirmInput, setConfirmInput] = useState("");
	const confirmMismatch = gateActive && confirmInput.trim() !== trimmedConfirmText;
	const primaryDisabled = isPrimaryActionDisabled || confirmMismatch;
	const hasConfirmLabel = Boolean(confirmLabel);
	useEffect(() => {
		if (isOpen) setConfirmInput("");
	}, [isOpen]);
	const desiredFocus = autoFocusButton ?? (variant === "destructive" || variant === "error" ? "cancel" : "primary");
	const isFocusable = {
		primary: !primaryDisabled,
		secondary: Boolean(secondaryActionLabel) && !isSecondaryActionDisabled,
		cancel: Boolean(cancelLabel)
	};
	const focusTarget = [
		desiredFocus,
		"cancel",
		"primary",
		"secondary"
	].find((t) => isFocusable[t]);
	useEffect(() => {
		if (!isOpen) return;
		if (gateActive) {
			confirmInputRef.current?.focus();
			return;
		}
		if (!focusTarget) return;
		({
			primary: primaryRef,
			secondary: secondaryRef,
			cancel: cancelRef
		})[focusTarget].current?.focus();
	}, [
		isOpen,
		focusTarget,
		gateActive
	]);
	const handleCancel = () => {
		onCancel?.();
		onClose();
	};
	const handlePrimary = () => {
		if (primaryDisabled) return;
		onPrimaryAction();
		onClose();
	};
	const handleSecondary = () => {
		onSecondaryAction?.();
		onClose();
	};
	const dialogProps = {
		isOpen,
		onClose: handleCancel,
		size,
		testId,
		role: "alertdialog"
	};
	const fallbackConfirmLabel = gateActive ? `Type "${trimmedConfirmText}" to confirm` : void 0;
	return /* @__PURE__ */ jsxs(Dialog, {
		...dialogProps,
		children: [
			/* @__PURE__ */ jsx(Dialog.Heading, { children: title }),
			/* @__PURE__ */ jsxs(Dialog.Content, { children: [children, gateActive && /* @__PURE__ */ jsxs("div", {
				className: "mt-4 flex flex-col gap-2",
				children: [hasConfirmLabel && /* @__PURE__ */ jsx("label", {
					id: confirmLabelId,
					htmlFor: confirmInputId,
					className: "text-sm text-(--text)",
					children: confirmLabel
				}), /* @__PURE__ */ jsx(Input, {
					ref: confirmInputRef,
					id: confirmInputId,
					value: confirmInput,
					onChange: (e) => setConfirmInput(e.target.value),
					onKeyDown: (e) => {
						if (e.key === "Enter" && !confirmMismatch && !isPrimaryActionDisabled) {
							e.preventDefault();
							handlePrimary();
						}
					},
					placeholder: trimmedConfirmText,
					autoComplete: "off",
					autoCapitalize: "off",
					autoCorrect: "off",
					spellCheck: false,
					"aria-labelledby": hasConfirmLabel ? confirmLabelId : void 0,
					"aria-label": hasConfirmLabel ? void 0 : fallbackConfirmLabel
				})]
			})] }),
			/* @__PURE__ */ jsxs(Dialog.Footer, { children: [
				cancelLabel && /* @__PURE__ */ jsx(Button, {
					ref: cancelRef,
					variant: "secondary",
					onClick: handleCancel,
					children: cancelLabel
				}),
				secondaryActionLabel && /* @__PURE__ */ jsx(Button, {
					ref: secondaryRef,
					variant: "secondary",
					disabled: isSecondaryActionDisabled,
					onClick: handleSecondary,
					children: secondaryActionLabel
				}),
				/* @__PURE__ */ jsx(Button, {
					ref: primaryRef,
					variant: variantPrimaryStyle[variant],
					disabled: primaryDisabled,
					onClick: handlePrimary,
					children: primaryActionLabel
				})
			] })
		]
	});
};
//#endregion
export { AlertDialog };

//# sourceMappingURL=AlertDialog.js.map