'use client';
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useMemo, useState } from 'react';
import { GitBranch, Sparkles } from 'lucide-react';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '../../ui/dialog.js';
import { Checkbox } from '../../ui/checkbox.js';
import { Button } from '../../ui/button.js';
export function ManageWorkspaceDialog({ open, onOpenChange, workspace, allNodes, onSave, }) {
    const [selectedRepoIds, setSelectedRepoIds] = useState(new Set());
    const [selectedFeatureIds, setSelectedFeatureIds] = useState(new Set());
    // Reset selection state whenever the dialog opens for a (possibly different) workspace.
    useEffect(() => {
        if (open) {
            setSelectedRepoIds(new Set(workspace.repoIds));
            setSelectedFeatureIds(new Set(workspace.featureIds));
        }
    }, [open, workspace]);
    // Group features under their parent repo by repositoryPath.
    const repoEntries = useMemo(() => {
        const repos = [];
        const byPath = new Map();
        for (const node of allNodes) {
            if (node.type !== 'repositoryNode')
                continue;
            const data = node.data;
            const path = data.repositoryPath ?? '';
            const entry = {
                nodeId: node.id,
                name: data.name ?? path.split('/').filter(Boolean).pop() ?? path,
                features: [],
            };
            repos.push(entry);
            byPath.set(path, entry);
        }
        for (const node of allNodes) {
            if (node.type !== 'featureNode')
                continue;
            const data = node.data;
            const repo = byPath.get(data.repositoryPath ?? '');
            if (repo) {
                repo.features.push({ nodeId: node.id, name: data.name });
            }
        }
        return repos;
    }, [allNodes]);
    const toggleRepo = (id) => {
        setSelectedRepoIds((prev) => {
            const next = new Set(prev);
            if (next.has(id))
                next.delete(id);
            else
                next.add(id);
            return next;
        });
    };
    const toggleFeature = (id) => {
        setSelectedFeatureIds((prev) => {
            const next = new Set(prev);
            if (next.has(id))
                next.delete(id);
            else
                next.add(id);
            return next;
        });
    };
    const handleSave = () => {
        onSave({
            repoIds: Array.from(selectedRepoIds),
            featureIds: Array.from(selectedFeatureIds),
        });
        onOpenChange(false);
    };
    return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsxs(DialogContent, { className: "max-h-[80vh] max-w-lg overflow-hidden", children: [_jsxs(DialogHeader, { children: [_jsxs(DialogTitle, { children: ["Manage workspace: ", workspace.name] }), _jsx(DialogDescription, { children: "Select the repositories and features that should appear on this workspace's canvas." })] }), _jsx("div", { className: "-mx-6 max-h-[50vh] overflow-y-auto px-6", children: repoEntries.length === 0 ? (_jsx("p", { className: "text-muted-foreground py-8 text-center text-sm", children: "No repositories on the canvas yet." })) : (_jsx("ul", { className: "space-y-3 py-2", children: repoEntries.map((repo) => (_jsxs("li", { children: [_jsxs("label", { className: "hover:bg-accent/30 flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5", children: [_jsx(Checkbox, { checked: selectedRepoIds.has(repo.nodeId), onCheckedChange: () => toggleRepo(repo.nodeId) }), _jsx(GitBranch, { className: "text-muted-foreground h-4 w-4" }), _jsx("span", { className: "text-sm font-medium", children: repo.name })] }), repo.features.length > 0 ? (_jsx("ul", { className: "mt-1 ml-7 space-y-0.5", children: repo.features.map((feat) => (_jsx("li", { children: _jsxs("label", { className: "hover:bg-accent/30 flex cursor-pointer items-center gap-2 rounded-md px-2 py-1", children: [_jsx(Checkbox, { checked: selectedFeatureIds.has(feat.nodeId), onCheckedChange: () => toggleFeature(feat.nodeId) }), _jsx(Sparkles, { className: "text-muted-foreground h-3.5 w-3.5" }), _jsx("span", { className: "text-muted-foreground text-xs", children: feat.name })] }) }, feat.nodeId))) })) : null] }, repo.nodeId))) })) }), _jsxs(DialogFooter, { children: [_jsx(Button, { variant: "ghost", onClick: () => onOpenChange(false), children: "Cancel" }), _jsx(Button, { onClick: handleSave, children: "Save" })] })] }) }));
}
