import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Box, Text, useInput } from "ink";
import { useState } from "react";
export function PackageSelector({ packages, onComplete, onSkip }) {
    // Start with uninstalled packages selected
    const [selectedPackages, setSelectedPackages] = useState(() => new Set(packages.filter((p) => !p.installed).map((p) => p.repo)));
    const [highlightedIndex, setHighlightedIndex] = useState(0);
    const [installLocation, setInstallLocation] = useState("project");
    // Items: packages + location option
    const locationOptionIndex = packages.length;
    const totalItems = packages.length + 1;
    useInput((input, key) => {
        if (key.escape) {
            onSkip();
            return;
        }
        if (key.return) {
            const selected = packages.filter((p) => selectedPackages.has(p.repo));
            onComplete(selected, installLocation);
            return;
        }
        if (key.upArrow) {
            setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : totalItems - 1));
            return;
        }
        if (key.downArrow) {
            setHighlightedIndex((prev) => (prev < totalItems - 1 ? prev + 1 : 0));
            return;
        }
        // Left/right arrows toggle location when on the location option
        if (highlightedIndex === locationOptionIndex && (key.leftArrow || key.rightArrow)) {
            setInstallLocation((prev) => (prev === "project" ? "global" : "project"));
            return;
        }
        if (input === " ") {
            if (highlightedIndex === locationOptionIndex) {
                // Toggle install location
                setInstallLocation((prev) => (prev === "project" ? "global" : "project"));
            }
            else {
                // Toggle individual package (only if not already installed)
                const pkg = packages[highlightedIndex];
                if (!pkg.installed) {
                    const newSelected = new Set(selectedPackages);
                    if (newSelected.has(pkg.repo)) {
                        newSelected.delete(pkg.repo);
                    }
                    else {
                        newSelected.add(pkg.repo);
                    }
                    setSelectedPackages(newSelected);
                }
            }
        }
    });
    const renderCheckbox = (checked, highlighted) => {
        const box = checked ? "[x]" : "[ ]";
        return (_jsxs(Text, { color: highlighted ? "#A18CE5" : undefined, children: [highlighted ? ">" : " ", " ", box] }));
    };
    // Get install path based on location
    const getInstallPath = () => {
        if (installLocation === "global") {
            return "~/.agents/skills";
        }
        return ".agents/skills";
    };
    return (_jsxs(Box, { flexDirection: "column", paddingX: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: "#A18CE5", bold: true, children: "d3k Skill Packages" }) }), _jsx(Text, { color: "#AAAAAA", children: "Install skill packages for your project:" }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [packages.map((pkg, index) => {
                        const isHighlighted = highlightedIndex === index;
                        const isSelected = selectedPackages.has(pkg.repo);
                        const isInstalled = pkg.installed;
                        return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { children: [isInstalled ? _jsxs(Text, { color: "#888888", children: ["  ", "[\u2713]"] }) : renderCheckbox(isSelected, isHighlighted), _jsxs(Text, { color: isInstalled ? "gray" : isHighlighted ? "#A18CE5" : undefined, children: [" ", pkg.repo, isInstalled && _jsx(Text, { color: "green", children: " (installed)" })] })] }), _jsx(Box, { flexDirection: "column", marginLeft: 6, children: pkg.skillFolders.map((skill) => (_jsx(Text, { color: "#999999", children: skill }, skill))) })] }, pkg.repo));
                    }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Box, { children: [_jsxs(Text, { color: highlightedIndex === locationOptionIndex ? "#A18CE5" : undefined, children: [highlightedIndex === locationOptionIndex ? ">" : " ", " Install to:", " "] }), _jsx(Text, { color: installLocation === "project" ? "cyan" : "gray", bold: installLocation === "project", children: "[project]" }), _jsx(Text, { children: " " }), _jsx(Text, { color: installLocation === "global" ? "cyan" : "gray", bold: installLocation === "global", children: "[global]" })] }), _jsx(Box, { marginLeft: 4, children: _jsx(Text, { color: "#888888", children: getInstallPath() }) })] })] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "#888888", children: "Space/arrows to toggle, Enter to install, Esc to skip" }) })] }));
}
//# sourceMappingURL=PackageSelector.js.map