#!/usr/bin/env node
import {
  SelectionCard
} from "./chunk-X5EG4EFP.js";
import {
  useSectionScroll
} from "./chunk-BNQ5O6LE.js";
import {
  useWizardStore
} from "./chunk-WF6RM73R.js";
import {
  DEFAULT_SCRATCH_DOMAINS
} from "./chunk-6OWHQ7HM.js";
import {
  init_esm_shims
} from "./chunk-DHET7RCE.js";

// src/cli/components/wizard/stack-selection.tsx
init_esm_shims();
import { Box, useInput } from "ink";
import { useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
var INITIAL_FOCUSED_INDEX = 0;
var SCRATCH_LABEL = "Start from scratch";
var SCRATCH_DESCRIPTION = "Select domains and skills manually";
var EXTRA_ITEMS_COUNT = 1;
var StackSelection = ({
  matrix,
  availableHeight = 0,
  onCancel
}) => {
  const { selectStack, setApproach, setStackAction, populateFromSkillIds, toggleDomain } = useWizardStore();
  const [focusedIndex, setFocusedIndex] = useState(INITIAL_FOCUSED_INDEX);
  const stacks = matrix.suggestedStacks;
  const stackCount = stacks.length;
  const scratchIndex = stackCount;
  const totalItems = stackCount + EXTRA_ITEMS_COUNT;
  const { setSectionRef, scrollEnabled, scrollTopPx } = useSectionScroll({
    sectionCount: totalItems,
    focusedIndex,
    availableHeight
  });
  useInput((input, key) => {
    if (key.escape) {
      if (onCancel) {
        onCancel();
      }
      return;
    }
    if (key.return) {
      if (focusedIndex === scratchIndex) {
        selectStack(null);
        setApproach("scratch");
        for (const domain of DEFAULT_SCRATCH_DOMAINS) {
          toggleDomain(domain);
        }
        return;
      }
      const focusedStack = stacks[focusedIndex];
      if (focusedStack) {
        selectStack(focusedStack.id);
        setStackAction("customize");
        populateFromSkillIds(focusedStack.allSkillIds, matrix.skills, matrix.categories);
        setApproach("stack");
      }
      return;
    }
    if (key.upArrow || input === "k") {
      setFocusedIndex((prev) => Math.max(0, prev - 1));
      return;
    }
    if (key.downArrow || input === "j") {
      setFocusedIndex((prev) => Math.min(totalItems - 1, prev + 1));
    }
  });
  const noShrink = scrollEnabled ? { flexShrink: 0 } : {};
  const sectionElements = stacks.map((stack, index) => /* @__PURE__ */ jsx(Box, { ref: (el) => setSectionRef(index, el), width: "100%", ...noShrink, children: /* @__PURE__ */ jsx(
    SelectionCard,
    {
      label: stack.name,
      description: stack.description,
      isFocused: index === focusedIndex,
      marginBottom: 1
    }
  ) }, stack.id));
  const scratchElement = /* @__PURE__ */ jsx(Box, { ref: (el) => setSectionRef(scratchIndex, el), width: "100%", ...noShrink, children: /* @__PURE__ */ jsx(
    SelectionCard,
    {
      label: SCRATCH_LABEL,
      description: SCRATCH_DESCRIPTION,
      isFocused: focusedIndex === scratchIndex
    }
  ) });
  return /* @__PURE__ */ jsx(
    Box,
    {
      flexDirection: "column",
      width: "100%",
      ...scrollEnabled ? { height: availableHeight, overflow: "hidden" } : { flexGrow: 1 },
      children: /* @__PURE__ */ jsxs(Box, { flexDirection: "column", marginTop: scrollTopPx > 0 ? -scrollTopPx : 0, ...noShrink, children: [
        sectionElements,
        scratchElement
      ] })
    }
  );
};

export {
  StackSelection
};
//# sourceMappingURL=chunk-BMJZBLP7.js.map