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: "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 pt-8", children: [_jsx("div", { className: "max-h-[50vh] overflow-y-auto px-8 pb-4", 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-feedback-red-light text-feedback-red"
                                        : "bg-feedback-orange-light text-feedback-orange"), children: props?.icon ? (props.icon) : (_jsx(AlertTriangle, { className: "size-5", strokeWidth: 2 })) }), _jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-1", children: [_jsx(DialogTitle, { className: "text-xl font-bold", "data-testid": "confirmation-dialog-title", children: props?.header || "Confirmation" }), _jsx("div", { className: "text-muted-foreground mt-1 text-sm leading-relaxed", children: props?.message })] })] }) }), _jsxs(DialogFooter, { className: "bg-neutral-grey-5/50 mt-4 border-t px-8 py-6 sm:flex-wrap", children: [props?.showCancel && (_jsx(Button, { variant: "outline", onClick: handleReject, className: "w-full max-w-full min-w-[100px] bg-white text-center break-words whitespace-normal sm:w-auto", children: "Cancel" })), props?.rejectLabel && (_jsx(Button, { variant: "outline", onClick: handleReject, className: "w-full max-w-full min-w-[100px] bg-white text-center break-words whitespace-normal sm:w-auto", children: props.rejectLabel })), showAccept && (_jsx(Button, { onClick: handleAccept, variant: props?.acceptLabel === "Delete" ? "destructive" : "default", className: "w-full max-w-full min-w-[100px] text-center break-words whitespace-normal sm:w-auto", children: props?.acceptLabel || "Yes" }))] })] }) }) }));
});
export default ConfirmationDialog;
//# sourceMappingURL=ConfirmationDialog.js.map