import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useRef, forwardRef, useImperativeHandle } from "react";
import { Dialog, DialogContent, DialogTitle, DialogFooter, } from "../components/ui/dialog";
import { Button } from "../components/ui/button";
import { AlertTriangle } from "lucide-react";
import { cn } from "../lib/utils";
const ConfirmationDialog = forwardRef((_props, ref) => {
    const [visible, setVisible] = useState(false);
    const resolvedRef = useRef(false);
    const [props, setProps] = useState();
    const showAccept = props?.showAccept === undefined ? true : props.showAccept;
    useImperativeHandle(ref, () => ({
        confirm: (props) => {
            resolvedRef.current = false;
            setProps(props);
            setVisible(true);
        },
    }));
    const handleAccept = () => {
        if (resolvedRef.current)
            return;
        resolvedRef.current = true;
        props?.accept();
        setVisible(false);
    };
    const handleReject = () => {
        if (resolvedRef.current)
            return;
        resolvedRef.current = true;
        if (props?.reject)
            props.reject();
        setVisible(false);
    };
    const handleOpenChange = (open) => {
        if (open) {
            setVisible(true);
            return;
        }
        if (!resolvedRef.current) {
            resolvedRef.current = true;
            props?.reject?.();
        }
        setVisible(false);
    };
    const acceptLabel = props?.acceptLabel || "";
    const actionsLayout = props?.actionsLayout ?? "default";
    const actionButtonClassName = cn("w-full max-w-full min-w-[100px] text-center break-words whitespace-normal sm:w-auto", actionsLayout === "single-row" && "sm:shrink-0");
    const isDestructive = /^(Delete|Remove)\b/i.test(acceptLabel) ||
        /^Yes,\s*(delete|remove)\b/i.test(acceptLabel);
    return (_jsx(Dialog, { open: visible, onOpenChange: handleOpenChange, children: _jsx(DialogContent, { className: "flex max-h-[80vh] flex-col gap-0 p-0 sm:max-w-[480px]", "data-testid": "confirmation-dialog", children: _jsxs("div", { className: "flex flex-col overflow-hidden", children: [_jsxs("div", { className: "border-border-default flex items-center gap-4 border-b px-6 py-4 pr-14", children: [_jsx("div", { className: cn("flex shrink-0 items-center justify-center", isDestructive
                                    ? "text-feedback-red-strong"
                                    : "text-feedback-orange"), children: props?.icon ? (props.icon) : (_jsx(AlertTriangle, { className: "size-5", strokeWidth: 2 })) }), _jsx(DialogTitle, { className: "min-w-0 text-xl font-medium", "data-testid": "confirmation-dialog-title", children: props?.header || "Confirmation" })] }), _jsx("div", { className: "max-h-[50vh] overflow-y-auto px-6 py-4", children: _jsx("div", { className: "text-muted-foreground text-sm leading-relaxed", children: props?.message }) }), _jsxs(DialogFooter, { className: cn("border-border-default mt-0 gap-4 border-t px-3 py-3 sm:gap-4", actionsLayout === "single-row"
                            ? "sm:flex-nowrap"
                            : "sm:flex-wrap"), children: [props?.showCancel && (_jsx(Button, { variant: "ghost", onClick: handleReject, className: actionButtonClassName, children: "Cancel" })), props?.rejectLabel && (_jsx(Button, { variant: props.rejectButtonVariant ?? "ghost", onClick: handleReject, className: actionButtonClassName, children: props.rejectLabel })), showAccept && (_jsx(Button, { onClick: handleAccept, variant: isDestructive ? "destructive" : "default", className: actionButtonClassName, children: props?.acceptLabel || "Yes" }))] })] }) }) }));
});
export default ConfirmationDialog;
//# sourceMappingURL=ConfirmationDialog.js.map