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 isDestructive = props?.acceptLabel === "Delete" || props?.acceptLabel === "Remove";
    return (_jsx(Dialog, { open: visible, onOpenChange: handleOpenChange, children: _jsx(DialogContent, { className: "gap-0 p-0 sm:max-w-[480px] max-h-[80vh] flex flex-col", "data-testid": "confirmation-dialog", children: _jsxs("div", { className: "flex flex-col pt-8 overflow-hidden", children: [_jsx("div", { className: "px-8 pb-4 overflow-y-auto max-h-[50vh]", children: _jsxs("div", { className: "flex items-start gap-4", children: [_jsx("div", { className: cn("flex size-10 shrink-0 items-center justify-center rounded-full", isDestructive ? "bg-red-50 text-red-600" : "bg-amber-50 text-amber-600"), children: props?.icon ? (props.icon) : (_jsx(AlertTriangle, { className: "size-5", strokeWidth: 2 })) }), _jsxs("div", { className: "flex flex-col gap-1 min-w-0 flex-1", children: [_jsx(DialogTitle, { className: "text-xl font-bold", "data-testid": "confirmation-dialog-title", children: props?.header || "Confirmation" }), _jsx("div", { className: "text-sm leading-relaxed text-muted-foreground mt-1", children: props?.message })] })] }) }), _jsxs(DialogFooter, { className: "bg-gray-50/50 mt-4 border-t px-8 py-6 sm:flex-wrap", children: [props?.showCancel && (_jsx(Button, { variant: "outline", onClick: handleReject, className: "min-w-[100px] w-full sm:w-auto bg-white whitespace-normal break-words text-center max-w-full", children: "Cancel" })), props?.rejectLabel && (_jsx(Button, { variant: "outline", onClick: handleReject, className: "min-w-[100px] w-full sm:w-auto bg-white whitespace-normal break-words text-center max-w-full", children: props.rejectLabel })), showAccept && (_jsx(Button, { onClick: handleAccept, variant: props?.acceptLabel === "Delete" ? "destructive" : "default", className: "min-w-[100px] w-full sm:w-auto whitespace-normal break-words text-center max-w-full", children: props?.acceptLabel || "Yes" }))] })] }) }) }));
});
export default ConfirmationDialog;
//# sourceMappingURL=ConfirmationDialog.js.map