import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useMemo } from "react";
import { toast } from "sonner";
import { Button } from "../../components/ui/button";
import { Dialog, DialogContent, DialogFooter, } from "../../components/ui/dialog";
import { StyledDialogTitle } from "../../components/ui/styled-dialog-title";
import { Label } from "../../components/ui/label";
import { Select } from "../../components/ui/select";
import { Link as LinkIcon } from "lucide-react";
import { addDependency } from "../services/taskService";
export function AddDependencyDialog(props) {
    const { open, onOpenChange, task, allTasks, onCreated } = props;
    const [targetTaskId, setTargetTaskId] = useState("");
    const [type, setType] = useState("BlockedBy");
    const [submitting, setSubmitting] = useState(false);
    const options = useMemo(() => {
        return allTasks
            .filter((t) => t.taskId !== task.taskId)
            .map((t) => ({ value: t.taskId, label: t.title }));
    }, [allTasks, task]);
    const submit = async () => {
        if (!targetTaskId) {
            toast.error("Please select a target task");
            return;
        }
        setSubmitting(true);
        try {
            const result = await addDependency({
                taskId: task.taskId,
                dependsOnTaskId: targetTaskId,
                dependencyType: type,
            });
            if (result.type !== "success") {
                toast.error(result.summary || "Failed to add dependency");
                return;
            }
            toast.success("Relationship added");
            onOpenChange(false);
            await onCreated();
            // reset
            setTargetTaskId("");
            setType("BlockedBy");
        }
        finally {
            setSubmitting(false);
        }
    };
    return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsxs(DialogContent, { className: "max-w-md", children: [_jsx(StyledDialogTitle, { icon: _jsx(LinkIcon, { strokeWidth: 1 }), title: "Add Relationship", subtitle: `Link "${task.title}" to another task` }), _jsxs("div", { className: "grid gap-6 p-6", children: [_jsxs("div", { className: "grid gap-2", children: [_jsx(Label, { htmlFor: "targetTask", children: "Target Task" }), _jsx(Select, { id: "targetTask", value: targetTaskId, onValueChange: setTargetTaskId, options: options, placeholder: "Select a task..." })] }), _jsxs("div", { className: "grid gap-2", children: [_jsx(Label, { htmlFor: "dependencyType", children: "Relationship Type" }), _jsx(Select, { id: "dependencyType", value: type, onValueChange: (v) => setType(v), options: [
                                        {
                                            value: "BlockedBy",
                                            label: "Blocked By (Current task waits for target)",
                                        },
                                        { value: "RelatedTo", label: "Related To (General link)" },
                                    ] })] })] }), _jsxs(DialogFooter, { className: "p-4", children: [_jsx(Button, { variant: "outline", onClick: () => onOpenChange(false), disabled: submitting, children: "Cancel" }), _jsx(Button, { onClick: submit, disabled: submitting || !targetTaskId, children: submitting ? "Adding..." : "Add Relationship" })] })] }) }));
}
//# sourceMappingURL=AddDependencyDialog.js.map