#!/usr/bin/env node
import {
  FEATURE_FLAGS
} from "./chunk-HGTC76BX.js";
import {
  useMeasuredHeight
} from "./chunk-K77I4XGL.js";
import {
  SourceGrid
} from "./chunk-O6BA7Q2B.js";
import {
  SelectionCard
} from "./chunk-X5EG4EFP.js";
import {
  ViewTitle
} from "./chunk-MGNYPVOJ.js";
import {
  useWizardStore
} from "./chunk-WF6RM73R.js";
import {
  resolveAllSources,
  searchExtraSources
} from "./chunk-TZXYBG3R.js";
import {
  CLI_COLORS,
  DEFAULT_BRANDING
} from "./chunk-6OWHQ7HM.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 (input === "l" || input === "L") {
        store.setAllSourcesLocal();
      }
      if (input === "p" || input === "P") {
        store.setAllSourcesPlugin(matrix);
      }
      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: FEATURE_FLAGS.SOURCE_SEARCH ? handleSearch : void 0,
          onBind: FEATURE_FLAGS.SOURCE_SEARCH ? handleBind : void 0,
          onSearchStateChange: FEATURE_FLAGS.SOURCE_SEARCH ? handleSearchStateChange : void 0
        }
      ) }),
      /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { dimColor: true, children: "L set all local P set all plugin ENTER continue ESC back" }) })
    ] });
  }
  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-SZRK3VOR.js.map