'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { ExternalLink, Loader2, MoreHorizontal, Play, Square, Trash2 } from 'lucide-react';
import { useRouter } from 'next/navigation';
import { useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';
import { Button } from '../../ui/button.js';
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '../../ui/dialog.js';
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from '../../ui/dropdown-menu.js';
import { useDeployAction } from '../../../hooks/use-deploy-action.js';
import { deleteApplication } from '../../../app/actions/delete-application.js';
import { DeploymentState } from '../../../../../../packages/core/src/domain/generated/output.js';
export function ApplicationRowActions({ applicationId, applicationName, repositoryPath, cloudUrl, }) {
    const router = useRouter();
    const queryClient = useQueryClient();
    const [menuOpen, setMenuOpen] = useState(false);
    const [confirmOpen, setConfirmOpen] = useState(false);
    const deploy = useDeployAction({
        targetId: applicationId,
        targetType: 'application',
        repositoryPath,
    });
    const isLocalRunning = deploy.status === DeploymentState.Ready && Boolean(deploy.url);
    const isBooting = deploy.status === DeploymentState.Booting || deploy.deployLoading;
    function handleOpen() {
        setMenuOpen(false);
        router.push(`/application/${applicationId}`);
    }
    function handleStart() {
        setMenuOpen(false);
        void deploy.deploy();
    }
    function handleStop() {
        setMenuOpen(false);
        void deploy.stop();
    }
    function handleOpenLive() {
        setMenuOpen(false);
        const target = deploy.url ?? cloudUrl;
        if (!target)
            return;
        window.open(target, '_blank', 'noopener,noreferrer');
    }
    function handleDeleteRequest() {
        setMenuOpen(false);
        setConfirmOpen(true);
    }
    async function handleDeleteConfirm() {
        setConfirmOpen(false);
        const result = await deleteApplication(applicationId);
        if (result.error) {
            toast.error('Failed to delete', { description: result.error });
        }
        else {
            toast.success('Application deleted');
            void queryClient.invalidateQueries({ queryKey: ['applications'] });
            router.refresh();
        }
    }
    const livePreviewUrl = deploy.url ?? cloudUrl ?? null;
    return (_jsxs(_Fragment, { children: [_jsxs(DropdownMenu, { open: menuOpen, onOpenChange: setMenuOpen, children: [_jsx(DropdownMenuTrigger, { asChild: true, children: _jsx(Button, { variant: "ghost", size: "icon", className: "h-7 w-7", disabled: isBooting || deploy.stopLoading, "aria-label": "Application actions", onClick: (e) => e.stopPropagation(), children: isBooting || deploy.stopLoading ? (_jsx(Loader2, { className: "h-4 w-4 animate-spin" })) : (_jsx(MoreHorizontal, { className: "h-4 w-4" })) }) }), _jsxs(DropdownMenuContent, { align: "end", onClick: (e) => e.stopPropagation(), children: [_jsxs(DropdownMenuItem, { onClick: handleOpen, children: [_jsx(ExternalLink, { className: "mr-2 h-4 w-4" }), "Open"] }), isLocalRunning ? (_jsxs(DropdownMenuItem, { onClick: handleStop, children: [_jsx(Square, { className: "mr-2 h-4 w-4" }), "Stop server"] })) : (_jsxs(DropdownMenuItem, { onClick: handleStart, children: [_jsx(Play, { className: "mr-2 h-4 w-4" }), "Start server"] })), livePreviewUrl ? (_jsxs(DropdownMenuItem, { onClick: handleOpenLive, children: [_jsx(ExternalLink, { className: "mr-2 h-4 w-4" }), "Open live preview"] })) : null, _jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { className: "text-destructive focus:text-destructive", onClick: handleDeleteRequest, children: [_jsx(Trash2, { className: "mr-2 h-4 w-4" }), "Delete"] })] })] }), _jsx(Dialog, { open: confirmOpen, onOpenChange: setConfirmOpen, children: _jsxs(DialogContent, { className: "max-w-xs", onClick: (e) => e.stopPropagation(), 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", children: "Cancel" }) }), _jsx(Button, { variant: "destructive", onClick: handleDeleteConfirm, children: "Delete" })] })] }) })] }));
}
