import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Box, Text, useInput } from "ink";
import { useState } from "react";
export function SkillSelector({ skills, onComplete, onSkip, initiallySelected }) {
    // Start with specified skills selected, or all if not specified
    const [selectedSkills, setSelectedSkills] = useState(() => {
        if (initiallySelected) {
            return new Set(initiallySelected.filter((name) => skills.some((s) => s.name === name)));
        }
        return new Set(skills.map((s) => s.name));
    });
    const [highlightedIndex, setHighlightedIndex] = useState(0);
    const [installLocation, setInstallLocation] = useState("project");
    // Items: 0 = "All Skills", 1..n = skills, n+1 = "Install to: project/global"
    const locationOptionIndex = skills.length + 1;
    const totalItems = skills.length + 2; // +1 for "All Skills", +1 for location option
    const allSelected = selectedSkills.size === skills.length;
    useInput((input, key) => {
        if (key.escape) {
            onSkip();
            return;
        }
        if (key.return) {
            const selected = skills.filter((s) => selectedSkills.has(s.name));
            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;
        }
        if (input === " ") {
            if (highlightedIndex === 0) {
                // Toggle all
                if (allSelected) {
                    setSelectedSkills(new Set());
                }
                else {
                    setSelectedSkills(new Set(skills.map((s) => s.name)));
                }
            }
            else if (highlightedIndex === locationOptionIndex) {
                // Toggle install location
                setInstallLocation((prev) => (prev === "project" ? "global" : "project"));
            }
            else {
                // Toggle individual skill
                const skill = skills[highlightedIndex - 1];
                const newSelected = new Set(selectedSkills);
                if (newSelected.has(skill.name)) {
                    newSelected.delete(skill.name);
                }
                else {
                    newSelected.add(skill.name);
                }
                setSelectedSkills(newSelected);
            }
        }
    });
    const renderCheckbox = (checked, highlighted) => {
        const box = checked ? "[x]" : "[ ]";
        return (_jsxs(Text, { color: highlighted ? "#A18CE5" : undefined, children: [highlighted ? ">" : " ", " ", box] }));
    };
    // Allow longer descriptions with word wrapping
    const formatDescription = (desc, maxLen = 80) => {
        if (desc.length <= maxLen)
            return desc;
        // Find a good break point (space) near maxLen
        const breakPoint = desc.lastIndexOf(" ", maxLen);
        if (breakPoint > maxLen * 0.6) {
            return `${desc.substring(0, breakPoint)}...`;
        }
        return `${desc.substring(0, maxLen - 3)}...`;
    };
    return (_jsxs(Box, { flexDirection: "column", paddingX: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: "#A18CE5", bold: true, children: "d3k Skill Installer" }) }), _jsx(Text, { dimColor: true, children: "Skills available from Vercel:" }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Box, { children: [renderCheckbox(allSelected, highlightedIndex === 0), _jsxs(Text, { color: highlightedIndex === 0 ? "#A18CE5" : undefined, bold: true, children: [" ", "All Skills"] })] }), skills.map((skill, index) => {
                        const itemIndex = index + 1;
                        const isHighlighted = highlightedIndex === itemIndex;
                        const isSelected = selectedSkills.has(skill.name);
                        const label = skill.isNew ? "(new)" : "(update)";
                        return (_jsxs(Box, { children: [renderCheckbox(isSelected, isHighlighted), _jsxs(Text, { color: isHighlighted ? "#A18CE5" : undefined, children: [" ", skill.name, " ", _jsx(Text, { color: skill.isNew ? "green" : "yellow", children: label }), _jsxs(Text, { dimColor: true, children: [" - ", formatDescription(skill.description)] })] })] }, skill.name));
                    }), _jsxs(Box, { marginTop: 1, 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(Text, { dimColor: true, children: installLocation === "project" ? " (.claude/skills/)" : " (~/.claude/skills/)" })] })] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Space to toggle, Enter to install, Esc to skip" }) })] }));
}
//# sourceMappingURL=SkillSelector.js.map