import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useEffect, useState } from "react";
import { Dialog, DialogContent, DialogFooter, } from "../../../components/ui/dialog";
import { Button } from "../../../components/ui/button";
import { Input } from "../../../components/ui/input";
import { StyledDialogTitle } from "../../../components/ui/styled-dialog-title";
import { setupWizardService, } from "../../../setup/services/setupWizardService";
import { AlertCircle, Loader2, Plus } from "lucide-react";
export function CreateAgentProfileDialog({ open, onOpenChange, onCreated, }) {
    const [name, setName] = useState("");
    const [error, setError] = useState(null);
    const [creating, setCreating] = useState(false);
    useEffect(() => {
        if (!open)
            return;
        setName("");
        setError(null);
        setCreating(false);
    }, [open]);
    const handleCreate = useCallback(async () => {
        const trimmedName = name.trim();
        if (!trimmedName) {
            setError("Agent name is required");
            return;
        }
        try {
            setCreating(true);
            setError(null);
            const request = { name: trimmedName };
            const agentId = await setupWizardService.createAgent(request);
            await onCreated(agentId);
            onOpenChange(false);
        }
        catch (e) {
            setError(e instanceof Error ? e.message : "Failed to create agent");
        }
        finally {
            setCreating(false);
        }
    }, [name, onCreated, onOpenChange]);
    return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsxs(DialogContent, { className: "max-w-md gap-0 p-0", children: [_jsx(StyledDialogTitle, { icon: _jsx(Plus, { strokeWidth: 1 }), title: "Add agent profile", subtitle: "Create a new blank agent profile by name." }), _jsxs("div", { className: "space-y-4 px-8 pt-6 pb-2", children: [_jsxs("div", { className: "space-y-2", children: [_jsx("label", { htmlFor: "create-agent-profile-name", className: "text-neutral-grey-100 block text-sm font-medium", children: "Name" }), _jsx(Input, { id: "create-agent-profile-name", value: name, onChange: (e) => setName(e.target.value), placeholder: "My Agent Profile", autoFocus: true, onKeyDown: (e) => {
                                        if (e.key === "Enter" && !creating) {
                                            e.preventDefault();
                                            void handleCreate();
                                        }
                                    } })] }), error ? (_jsxs("div", { className: "border-feedback-red bg-feedback-red-light text-feedback-red flex items-start gap-2 rounded border p-2 text-xs", children: [_jsx(AlertCircle, { className: "mt-0.5 h-3.5 w-3.5 shrink-0", strokeWidth: 1 }), error] })) : null] }), _jsxs(DialogFooter, { className: "border-border-default gap-2 border-t pt-5 sm:gap-3", children: [_jsx(Button, { variant: "outline", onClick: () => onOpenChange(false), disabled: creating, children: "Cancel" }), _jsx(Button, { onClick: () => void handleCreate(), disabled: creating || !name.trim(), children: creating ? (_jsxs(_Fragment, { children: [_jsx(Loader2, { className: "h-3.5 w-3.5 animate-spin", strokeWidth: 1 }), "Creating..."] })) : ("Create") })] })] }) }));
}
export default CreateAgentProfileDialog;
//# sourceMappingURL=CreateAgentProfileDialog.js.map