'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useEffect, useState } from 'react';
import { Loader2 } from 'lucide-react';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '../../ui/dialog.js';
import { Input } from '../../ui/input.js';
import { Button } from '../../ui/button.js';
import { createProjectFolder } from '../../../app/actions/create-project-folder.js';
export function NewProjectDialog({ open, onOpenChange, onCreated }) {
    const [name, setName] = useState('');
    const [busy, setBusy] = useState(false);
    const [error, setError] = useState(null);
    // Reset state every time the dialog opens.
    useEffect(() => {
        if (open) {
            setName('');
            setBusy(false);
            setError(null);
        }
    }, [open]);
    const handleSubmit = async (e) => {
        e.preventDefault();
        const trimmed = name.trim();
        if (!trimmed || busy)
            return;
        setBusy(true);
        setError(null);
        const result = await createProjectFolder(trimmed);
        if (!result.ok || !result.path) {
            setError(result.error ?? 'Failed to create project.');
            setBusy(false);
            return;
        }
        // Hand the new path off to the empty state's existing add-repo flow.
        onCreated(result.path);
        onOpenChange(false);
    };
    return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsx(DialogContent, { className: "max-w-sm", children: _jsxs("form", { onSubmit: handleSubmit, children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: "New project" }), _jsx(DialogDescription, { children: "Create an empty project folder inside your Shep home directory. Git will be initialised automatically when the project is added." })] }), _jsxs("div", { className: "py-4", children: [_jsx(Input, { autoFocus: true, value: name, onChange: (e) => {
                                    setName(e.target.value);
                                    if (error)
                                        setError(null);
                                }, placeholder: "my-prototype", disabled: busy, "data-testid": "new-project-name-input" }), error ? (_jsx("p", { className: "text-destructive mt-2 text-xs", role: "alert", children: error })) : null] }), _jsxs(DialogFooter, { children: [_jsx(Button, { type: "button", variant: "ghost", onClick: () => onOpenChange(false), disabled: busy, children: "Cancel" }), _jsx(Button, { type: "submit", disabled: !name.trim() || busy, children: busy ? (_jsxs(_Fragment, { children: [_jsx(Loader2, { className: "mr-2 h-4 w-4 animate-spin" }), "Creating\u2026"] })) : ('Create project') })] })] }) }) }));
}
