'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
 * DeleteApplicationMenuItem — destructive entry for the AppOverflowMenu.
 *
 * Mirrors the standalone DeleteButton's behavior (confirm dialog + toast +
 * redirect) but renders as a DropdownMenuItem so it sits cleanly inside
 * the `⋯` overflow menu instead of as a free-floating icon button in the
 * top bar.
 */
import { useState } from 'react';
import { Trash2 } from 'lucide-react';
import { useRouter } from 'next/navigation';
import { toast } from 'sonner';
import { Button } from '../../ui/button.js';
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '../../ui/dialog.js';
import { DropdownMenuItem } from '../../ui/dropdown-menu.js';
import { deleteApplication } from '../../../app/actions/delete-application.js';
export function DeleteApplicationMenuItem({ applicationId, applicationName, }) {
    const router = useRouter();
    const [confirmOpen, setConfirmOpen] = useState(false);
    return (_jsxs(_Fragment, { children: [_jsxs(DropdownMenuItem, { onSelect: (event) => {
                    // Stop the menu from closing on select so the dialog can take
                    // focus cleanly. Radix re-opens the dialog after the menu
                    // dismiss otherwise, leading to a flicker.
                    event.preventDefault();
                    setConfirmOpen(true);
                }, className: "text-destructive focus:text-destructive focus:bg-destructive/10 cursor-pointer", children: [_jsx(Trash2, { className: "size-3.5" }), _jsx("span", { children: "Delete app\u2026" })] }), _jsx(Dialog, { open: confirmOpen, onOpenChange: setConfirmOpen, children: _jsxs(DialogContent, { className: "max-w-xs", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: "Delete application?" }), _jsxs(DialogDescription, { children: ["This will permanently remove ", _jsx("strong", { children: applicationName }), "."] })] }), _jsxs(DialogFooter, { className: "grid grid-cols-2 gap-2 sm:flex-none", children: [_jsx(DialogClose, { asChild: true, children: _jsx(Button, { variant: "outline", className: "cursor-pointer", children: "Cancel" }) }), _jsx(Button, { variant: "destructive", className: "cursor-pointer", onClick: async () => {
                                        setConfirmOpen(false);
                                        const result = await deleteApplication(applicationId);
                                        if (result.error) {
                                            toast.error('Failed to delete', { description: result.error });
                                        }
                                        else {
                                            toast.success('Application deleted');
                                            router.push('/applications');
                                        }
                                    }, children: "Delete" })] })] }) })] }));
}
