#!/usr/bin/env node
import {
  useMeasuredHeight
} from "./chunk-K77I4XGL.js";
import {
  SelectionCard
} from "./chunk-ZWAL2ZY7.js";
import {
  SourceGrid
} from "./chunk-FXQYEXLS.js";
import {
  ViewTitle
} from "./chunk-B4QYXVPZ.js";
import {
  useWizardStore
} from "./chunk-SPSGZWTZ.js";
import {
  resolveAllSources,
  searchExtraSources
} from "./chunk-GFDGYQ6M.js";
import {
  CLI_COLORS,
  DEFAULT_BRANDING
} from "./chunk-FTD5Z6QD.js";
import {
  init_esm_shims
} from "./chunk-DHET7RCE.js";

// src/cli/components/wizard/step-sources.tsx
init_esm_shims();
import { Box, Text, useInput } from "ink";
import { useCallback, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
var StepSources = ({
  matrix,
  projectDir,
  onContinue,
  onBack
}) => {
  const store = useWizardStore();
  const [view, setView] = useState("choice");
  const [choiceIndex, setChoiceIndex] = useState(0);
  const [isGridSearching, setIsGridSearching] = useState(false);
  const { ref: gridRef, measuredHeight: gridHeight } = useMeasuredHeight();
  const handleGridSelect = useCallback(
    (skillId, sourceId) => {
      store.setSourceSelection(skillId, sourceId);
    },
    [store]
  );
  const handleSearch = useCallback(
    async (alias) => {
      if (!projectDir) return [];
      try {
        const sources = await resolveAllSources(projectDir);
        return await searchExtraSources(alias, sources.extras);
      } catch {
        return [];
      }
    },
    [projectDir]
  );
  const handleBind = useCallback(
    (candidate) => {
      store.bindSkill({
        id: candidate.id,
        sourceUrl: candidate.sourceUrl,
        sourceName: candidate.sourceName,
        boundTo: candidate.alias,
        description: candidate.description
      });
    },
    [store]
  );
  const handleSearchStateChange = useCallback((active) => {
    setIsGridSearching(active);
  }, []);
  useInput((_input, key) => {
    if (view === "choice") {
      if (key.return) {
        if (choiceIndex === 0) {
          onContinue();
        } else {
          store.setCustomizeSources(true);
          setView("customize");
        }
      }
      if (key.escape) {
        onBack();
      }
      if (key.upArrow || key.downArrow) {
        setChoiceIndex((prev) => prev === 0 ? 1 : 0);
      }
    } else if (view === "customize") {
      if (isGridSearching) return;
      if (key.return) {
        onContinue();
      }
      if (key.escape) {
        store.setCustomizeSources(false);
        setView("choice");
      }
    }
  });
  if (view === "customize") {
    const rows2 = store.buildSourceRows(matrix);
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", width: "100%", flexGrow: 1, flexBasis: 0, children: [
      /* @__PURE__ */ jsx(ViewTitle, { children: "Customize skill sources" }),
      /* @__PURE__ */ jsx(Box, { ref: gridRef, flexGrow: 1, flexBasis: 0, children: /* @__PURE__ */ jsx(
        SourceGrid,
        {
          rows: rows2,
          availableHeight: gridHeight,
          onSelect: handleGridSelect,
          onSearch: handleSearch,
          onBind: handleBind,
          onSearchStateChange: handleSearchStateChange
        }
      ) })
    ] });
  }
  const selectedTechnologies = store.getAllSelectedTechnologies();
  const rows = store.buildSourceRows(matrix);
  const isRecommendedSelected = choiceIndex === 0;
  const hasLocalSkills = rows.some(
    (row) => row.options.some((o) => o.installed && o.id === "local")
  );
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingX: 2, children: [
    /* @__PURE__ */ jsxs(Text, { children: [
      "Your stack includes",
      " ",
      /* @__PURE__ */ jsx(Text, { color: CLI_COLORS.PRIMARY, bold: true, children: selectedTechnologies.length }),
      " ",
      "technologies."
    ] }),
    /* @__PURE__ */ jsx(Text, { children: " " }),
    /* @__PURE__ */ jsx(
      SelectionCard,
      {
        label: hasLocalSkills ? "Use installed skill sources" : "Use all recommended skills (verified)",
        description: hasLocalSkills ? "Keep your current local and public skill selections." : [
          `This is the fastest option. All skills are verified and maintained by ${DEFAULT_BRANDING.NAME}`
        ],
        isFocused: isRecommendedSelected,
        marginBottom: 1
      }
    ),
    /* @__PURE__ */ jsx(
      SelectionCard,
      {
        label: "Customize skill sources",
        description: "Choose alternative skills for each technology",
        isFocused: !isRecommendedSelected
      }
    )
  ] });
};

export {
  StepSources
};
//# sourceMappingURL=chunk-HPJP3HFD.js.map