'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '../../ui/dialog.js';
import { Button } from '../../ui/button.js';
import { Input } from '../../ui/input.js';
import { Label } from '../../ui/label.js';
import { Textarea } from '../../ui/textarea.js';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '../../ui/select.js';
import { createWorkItem } from '../../../app/actions/create-work-item.js';
export function CreateWorkItemDialog({ open, onOpenChange, projectId, states, onCreated, parentId, }) {
    const [title, setTitle] = useState('');
    const [description, setDescription] = useState('');
    const [stateId, setStateId] = useState('');
    const [priority, setPriority] = useState('None');
    const [error, setError] = useState(null);
    const [submitting, setSubmitting] = useState(false);
    const handleSubmit = async () => {
        setError(null);
        setSubmitting(true);
        try {
            const result = await createWorkItem({
                projectId,
                title,
                description: description || undefined,
                stateId: stateId || undefined,
                priority: priority || undefined,
                parentId: parentId ?? undefined,
            });
            if (result.error) {
                setError(result.error);
            }
            else if (result.workItem) {
                onCreated(result.workItem);
                setTitle('');
                setDescription('');
                setStateId('');
                setPriority('None');
            }
        }
        finally {
            setSubmitting(false);
        }
    };
    return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsxs(DialogContent, { "data-testid": "create-work-item-dialog", children: [_jsx(DialogHeader, { children: _jsx(DialogTitle, { children: parentId ? 'Create Sub-item' : 'Create Work Item' }) }), _jsxs("div", { className: "space-y-4 py-2", children: [_jsxs("div", { className: "space-y-2", children: [_jsx(Label, { htmlFor: "work-item-title", children: "Title" }), _jsx(Input, { id: "work-item-title", value: title, onChange: (e) => setTitle(e.target.value), placeholder: "What needs to be done?", "data-testid": "work-item-title-input" })] }), _jsxs("div", { className: "space-y-2", children: [_jsx(Label, { htmlFor: "work-item-desc", children: "Description (optional)" }), _jsx(Textarea, { id: "work-item-desc", value: description, onChange: (e) => setDescription(e.target.value), placeholder: "Add more details...", rows: 3, "data-testid": "work-item-desc-input" })] }), _jsxs("div", { className: "grid grid-cols-2 gap-4", children: [_jsxs("div", { className: "space-y-2", children: [_jsx(Label, { children: "State" }), _jsxs(Select, { value: stateId, onValueChange: setStateId, children: [_jsx(SelectTrigger, { "data-testid": "work-item-state-select", children: _jsx(SelectValue, { placeholder: "Default" }) }), _jsx(SelectContent, { children: states.map((s) => (_jsx(SelectItem, { value: s.id, children: s.name }, s.id))) })] })] }), _jsxs("div", { className: "space-y-2", children: [_jsx(Label, { children: "Priority" }), _jsxs(Select, { value: priority, onValueChange: setPriority, children: [_jsx(SelectTrigger, { "data-testid": "work-item-priority-select", children: _jsx(SelectValue, { placeholder: "None" }) }), _jsxs(SelectContent, { children: [_jsx(SelectItem, { value: "None", children: "None" }), _jsx(SelectItem, { value: "Low", children: "Low" }), _jsx(SelectItem, { value: "Medium", children: "Medium" }), _jsx(SelectItem, { value: "High", children: "High" }), _jsx(SelectItem, { value: "Urgent", children: "Urgent" })] })] })] })] }), error ? (_jsx("p", { className: "text-destructive text-xs", "data-testid": "create-work-item-error", children: error })) : null] }), _jsxs(DialogFooter, { children: [_jsx(Button, { variant: "outline", onClick: () => onOpenChange(false), disabled: submitting, children: "Cancel" }), _jsx(Button, { onClick: handleSubmit, disabled: !title.trim() || submitting, "data-testid": "create-work-item-submit", children: submitting ? 'Creating...' : 'Create' })] })] }) }));
}
