'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * CreateAgentDialog
 *
 * Lets the user spin up a brand-new custom agent type (alongside the
 * built-in `feature-agent` and `supervisor-agent`). The user picks the
 * stable type id, a display name, a description, and optionally seeds the
 * first prompt slot. After save the dialog navigates to the editor for
 * the new agent so the user can author more slots and the graph.
 */
import { useState, useTransition } from 'react';
import { useTranslation } from 'react-i18next';
import { useRouter } from 'next/navigation';
import { Plus } from 'lucide-react';
import { Alert, AlertDescription } from '../ui/alert.js';
import { Button } from '../ui/button.js';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '../ui/dialog.js';
import { Input } from '../ui/input.js';
import { Label } from '../ui/label.js';
import { Textarea } from '../ui/textarea.js';
import { createCustomAgent } from '../../app/actions/custom-agents.js';
const TYPE_PATTERN = /^[a-z][a-z0-9-]{1,63}$/;
export function CreateAgentDialog({ onCreateOverride, initialOpen, onNavigateOverride, }) {
    const { t } = useTranslation('web');
    const router = useRouter();
    const [open, setOpen] = useState(Boolean(initialOpen));
    const [agentType, setAgentType] = useState('');
    const [name, setName] = useState('');
    const [description, setDescription] = useState('');
    const [promptId, setPromptId] = useState('system');
    const [body, setBody] = useState('');
    const [error, setError] = useState(null);
    const [isPending, startTransition] = useTransition();
    function reset() {
        setAgentType('');
        setName('');
        setDescription('');
        setPromptId('system');
        setBody('');
        setError(null);
    }
    const typeValid = TYPE_PATTERN.test(agentType);
    const ready = typeValid && name.trim().length > 0 && description.trim().length > 0 && !isPending;
    function handleSubmit(e) {
        e.preventDefault();
        if (!ready)
            return;
        setError(null);
        startTransition(async () => {
            const fn = onCreateOverride ?? createCustomAgent;
            const result = await fn({
                agentType,
                name,
                description,
                ...(promptId.trim() && body.length > 0
                    ? { initialPromptId: promptId, initialPromptBody: body }
                    : {}),
            });
            if (!result.ok) {
                setError(result.error ?? t('agentEditor.failedToCreateAgent'));
                return;
            }
            const next = result.agentType ?? agentType;
            reset();
            setOpen(false);
            const path = `/agents/${next}`;
            if (onNavigateOverride) {
                onNavigateOverride(path);
            }
            else {
                router.push(path);
            }
        });
    }
    return (_jsxs(Dialog, { open: open, onOpenChange: (next) => {
            setOpen(next);
            if (!next)
                reset();
        }, children: [_jsx(DialogTrigger, { asChild: true, children: _jsxs(Button, { "data-testid": "create-agent-trigger", children: [_jsx(Plus, { className: "size-4" }), t('agentEditor.newAgent')] }) }), _jsxs(DialogContent, { className: "max-w-xl", "data-testid": "create-agent-dialog", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: t('agentEditor.createCustomAgent') }), _jsx(DialogDescription, { children: t('agentEditor.createCustomAgentDescription') })] }), _jsxs("form", { className: "flex flex-col gap-4", onSubmit: handleSubmit, children: [_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(Label, { htmlFor: "create-agent-type", children: t('agentEditor.stableTypeId') }), _jsx(Input, { id: "create-agent-type", "data-testid": "create-agent-type", placeholder: t('agentEditor.stableTypeIdPlaceholder'), value: agentType, onChange: (e) => setAgentType(e.target.value.toLowerCase()), disabled: isPending, autoComplete: "off", "aria-invalid": agentType.length > 0 && !typeValid }), _jsxs("p", { className: "text-muted-foreground text-xs", children: [t('agentEditor.stableTypeIdHelpPrefix'), ' ', _jsx("code", { className: "bg-muted rounded px-1", children: "/^[a-z][a-z0-9-]+$/" }), t('agentEditor.stableTypeIdHelpSuffix')] })] }), _jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(Label, { htmlFor: "create-agent-name", children: t('agentEditor.displayName') }), _jsx(Input, { id: "create-agent-name", "data-testid": "create-agent-name", placeholder: t('agentEditor.displayNamePlaceholder'), value: name, onChange: (e) => setName(e.target.value), disabled: isPending })] }), _jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(Label, { htmlFor: "create-agent-description", children: t('agentEditor.description') }), _jsx(Input, { id: "create-agent-description", "data-testid": "create-agent-description", placeholder: t('agentEditor.descriptionPlaceholder'), value: description, onChange: (e) => setDescription(e.target.value), disabled: isPending })] }), _jsxs("div", { className: "flex flex-col gap-2 rounded-md border border-dashed p-3", children: [_jsx("p", { className: "text-sm font-medium", children: t('agentEditor.seedFirstPrompt') }), _jsx(Label, { htmlFor: "create-agent-prompt-id", children: t('agentEditor.promptSlotId') }), _jsx(Input, { id: "create-agent-prompt-id", "data-testid": "create-agent-prompt-id", value: promptId, onChange: (e) => setPromptId(e.target.value), disabled: isPending, placeholder: t('agentEditor.promptSlotIdPlaceholder') }), _jsx(Label, { htmlFor: "create-agent-prompt-body", children: t('agentEditor.promptBody') }), _jsx(Textarea, { id: "create-agent-prompt-body", "data-testid": "create-agent-prompt-body", value: body, onChange: (e) => setBody(e.target.value), rows: 5, className: "font-mono text-xs", disabled: isPending, placeholder: t('agentEditor.promptBodyPlaceholder') })] }), error ? (_jsx(Alert, { variant: "destructive", "data-testid": "create-agent-error", children: _jsx(AlertDescription, { children: error }) })) : null, _jsx(DialogFooter, { children: _jsx(Button, { type: "submit", disabled: !ready, "data-testid": "create-agent-submit", children: isPending ? t('agentEditor.creating') : t('agentEditor.createAgent') }) })] })] })] }));
}
