import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../components/ui/dialog";
import { Button } from "../components/ui/button";
import { Input } from "../components/ui/input";
import { Loader2 } from "lucide-react";
/**
 * Dialog for renaming an agent chat from the sidebar overflow menu.
 */
export function RenameAgentDialog({ open, initialName, onOpenChange, onRename, }) {
    const [name, setName] = useState(initialName);
    const [saving, setSaving] = useState(false);
    useEffect(() => {
        if (open) {
            setName(initialName);
            setSaving(false);
        }
    }, [open, initialName]);
    const trimmedName = name.trim();
    const canSave = trimmedName.length > 0 && !saving;
    const handleSave = async () => {
        if (!canSave)
            return;
        setSaving(true);
        try {
            await onRename(trimmedName);
            onOpenChange(false);
        }
        finally {
            setSaving(false);
        }
    };
    return (_jsx(Dialog, { open: open, onOpenChange: (next) => !saving && onOpenChange(next), children: _jsxs(DialogContent, { className: "sm:max-w-md", "data-testid": "rename-agent-dialog", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: "Rename chat" }), _jsx(DialogDescription, { children: "Give this chat a name that helps you find it later." })] }), _jsx(Input, { value: name, onChange: (e) => setName(e.target.value), maxLength: 200, autoFocus: true, onKeyDown: (e) => {
                        if (e.key === "Enter") {
                            e.preventDefault();
                            handleSave();
                        }
                    }, "data-testid": "rename-agent-input" }), _jsxs(DialogFooter, { children: [_jsx(Button, { type: "button", variant: "outline", onClick: () => onOpenChange(false), disabled: saving, children: "Cancel" }), _jsxs(Button, { type: "button", onClick: handleSave, disabled: !canSave, "data-testid": "rename-agent-save", children: [saving && _jsx(Loader2, { className: "size-4 animate-spin" }), "Save"] })] })] }) }));
}
//# sourceMappingURL=RenameAgentDialog.js.map