#!/usr/bin/env node
import {
  WizardLayout
} from "./chunk-OEJDFGAF.js";
import {
  StepSettings
} from "./chunk-JZHIF3K7.js";
import {
  StepSources
} from "./chunk-HPJP3HFD.js";
import {
  StepStack
} from "./chunk-FMQ3A7W4.js";
import {
  StepAgents
} from "./chunk-YTRFL3MR.js";
import {
  StepBuild
} from "./chunk-VAQJLHUW.js";
import {
  StepConfirm
} from "./chunk-DVW6ASTO.js";
import {
  getStackName
} from "./chunk-NRCKIHND.js";
import {
  cliTheme
} from "./chunk-N5OCAAXY.js";
import {
  useWizardStore
} from "./chunk-SPSGZWTZ.js";
import {
  resolveAlias,
  validateSelection
} from "./chunk-GFDGYQ6M.js";
import {
  warn
} from "./chunk-C4QI54PN.js";
import {
  CLI_COLORS
} from "./chunk-FTD5Z6QD.js";
import {
  init_esm_shims
} from "./chunk-DHET7RCE.js";

// src/cli/components/wizard/wizard.tsx
init_esm_shims();
import { useCallback as useCallback2 } from "react";
import { Box, Text, useApp, useInput, useStdout } from "ink";
import { ThemeProvider } from "@inkjs/ui";

// src/cli/components/hooks/use-wizard-initialization.ts
init_esm_shims();
import { useRef } from "react";
function useWizardInitialization({
  matrix,
  initialStep,
  initialInstallMode,
  initialExpertMode,
  initialDomains,
  initialAgents,
  installedSkillIds
}) {
  const initialized = useRef(false);
  if (!initialized.current) {
    initialized.current = true;
    if (initialStep) {
      if (installedSkillIds?.length) {
        useWizardStore.getState().populateFromSkillIds(installedSkillIds, matrix.skills, matrix.categories);
      }
      useWizardStore.setState({ step: initialStep, approach: "scratch" });
    }
    if (initialInstallMode) {
      useWizardStore.setState({ installMode: initialInstallMode });
    }
    if (initialExpertMode) {
      useWizardStore.setState({ expertMode: initialExpertMode });
    }
    if (initialDomains?.length) {
      useWizardStore.setState({ selectedDomains: initialDomains });
    }
    if (initialAgents?.length) {
      useWizardStore.setState({ selectedAgents: initialAgents });
    }
  }
}

// src/cli/components/hooks/use-build-step-props.ts
init_esm_shims();
import { useCallback } from "react";
function useBuildStepProps({
  store,
  matrix,
  installedSkillIds
}) {
  const currentDomain = store.getCurrentDomain();
  const defaultDomains = ["web"];
  const effectiveDomains = store.selectedDomains.length > 0 ? store.selectedDomains : defaultDomains;
  const allSelections = store.getAllSelectedTechnologies();
  const activeDomain = currentDomain || effectiveDomains[0] || "web";
  const onToggle = useCallback(
    (subcategoryId, techId) => {
      const domain = store.getCurrentDomain() || "web";
      const cat = matrix.categories[subcategoryId];
      store.toggleTechnology(domain, subcategoryId, techId, cat?.exclusive ?? true);
    },
    [store, matrix]
  );
  const onContinue = useCallback(() => {
    if (!store.nextDomain()) {
      store.setStep("sources");
    }
  }, [store]);
  const onBack = useCallback(() => {
    if (!store.prevDomain()) {
      store.goBack();
    }
  }, [store]);
  return {
    matrix,
    domain: activeDomain,
    selectedDomains: effectiveDomains,
    selections: store.domainSelections[activeDomain] || {},
    allSelections,
    showLabels: store.showLabels,
    expertMode: store.expertMode,
    installedSkillIds,
    onToggle,
    onToggleLabels: store.toggleShowLabels,
    onContinue,
    onBack
  };
}

// src/cli/components/wizard/wizard.tsx
import { jsx, jsxs } from "react/jsx-runtime";
var MIN_TERMINAL_WIDTH = 80;
var MIN_TERMINAL_HEIGHT = 15;
var Wizard = ({
  matrix,
  onComplete,
  onCancel,
  version,
  marketplaceLabel,
  logo,
  initialStep,
  initialInstallMode,
  initialExpertMode,
  initialDomains,
  initialAgents,
  installedSkillIds,
  projectDir
}) => {
  const store = useWizardStore();
  const { exit } = useApp();
  const { stdout } = useStdout();
  const terminalWidth = stdout.columns || MIN_TERMINAL_WIDTH;
  const terminalHeight = stdout.rows || MIN_TERMINAL_HEIGHT;
  const isNarrowTerminal = terminalWidth < MIN_TERMINAL_WIDTH;
  const isShortTerminal = terminalHeight < MIN_TERMINAL_HEIGHT;
  useWizardInitialization({
    matrix,
    initialStep,
    initialInstallMode,
    initialExpertMode,
    initialDomains,
    initialAgents,
    installedSkillIds
  });
  const buildStepProps = useBuildStepProps({ store, matrix, installedSkillIds });
  useInput((input, key) => {
    if (store.showSettings) {
      if (input === "g" || input === "G") {
        store.toggleSettings();
      }
      return;
    }
    if (store.showHelp) {
      if (key.escape || input === "?") {
        store.toggleHelp();
      }
      return;
    }
    if (input === "?") {
      store.toggleHelp();
      return;
    }
    if (key.escape) {
      if (store.step !== "build" && store.step !== "confirm" && store.step !== "sources" && store.step !== "agents" && store.step !== "stack") {
        store.goBack();
      }
      return;
    }
    if ((input === "a" || input === "A") && store.step === "build" && store.selectedStackId) {
      store.setStackAction("defaults");
      store.setStep("confirm");
      return;
    }
    if ((input === "g" || input === "G") && store.step === "sources") {
      store.toggleSettings();
      return;
    }
    if (input === "e" || input === "E") {
      store.toggleExpertMode();
      return;
    }
    if (input === "p" || input === "P") {
      store.toggleInstallMode();
    }
  });
  const handleComplete = useCallback2(() => {
    let allSkills;
    if (store.selectedStackId && store.stackAction === "defaults") {
      const stack = matrix.suggestedStacks.find((s) => s.id === store.selectedStackId);
      if (!stack) {
        warn(`Stack not found in matrix: '${store.selectedStackId}'`);
      }
      allSkills = [...stack?.allSkillIds || []];
    } else {
      const techNames = store.getAllSelectedTechnologies();
      allSkills = techNames.map((tech) => {
        const resolved = resolveAlias(tech, matrix);
        if (!matrix.skills[resolved]) {
          warn(
            `Technology '${tech}' could not be resolved to a skill ID - it may be missing from skill_aliases`
          );
        }
        return resolved;
      });
    }
    const methodologySkills = store.getDefaultMethodologySkills();
    for (const skill of methodologySkills) {
      if (!allSkills.includes(skill)) {
        allSkills.push(skill);
      }
    }
    const validation = validateSelection(allSkills, matrix);
    const result = {
      selectedSkills: allSkills,
      selectedAgents: store.selectedAgents,
      selectedStackId: store.selectedStackId,
      domainSelections: store.domainSelections,
      selectedDomains: store.selectedDomains,
      sourceSelections: store.sourceSelections,
      expertMode: store.expertMode,
      installMode: store.installMode,
      cancelled: false,
      validation
    };
    onComplete(result);
    exit();
  }, [store, matrix, onComplete, exit]);
  const handleCancel = useCallback2(() => {
    onCancel();
    exit();
  }, [onCancel, exit]);
  const renderStep = () => {
    switch (store.step) {
      case "stack":
        return /* @__PURE__ */ jsx(StepStack, { matrix, onCancel: handleCancel });
      case "build":
        return /* @__PURE__ */ jsx(StepBuild, { ...buildStepProps });
      case "sources": {
        if (store.showSettings) {
          return /* @__PURE__ */ jsx(
            StepSettings,
            {
              projectDir: projectDir || process.cwd(),
              onClose: () => store.toggleSettings()
            }
          );
        }
        return /* @__PURE__ */ jsx(
          StepSources,
          {
            matrix,
            projectDir,
            onContinue: () => {
              if (!initialAgents?.length) {
                store.preselectAgentsFromDomains();
              }
              store.setStep("agents");
            },
            onBack: store.goBack
          }
        );
      }
      case "agents":
        return /* @__PURE__ */ jsx(StepAgents, { matrix });
      case "confirm": {
        const stackName = getStackName(store.selectedStackId, matrix);
        const technologyCount = store.getTechnologyCount();
        return /* @__PURE__ */ jsx(
          StepConfirm,
          {
            onComplete: handleComplete,
            stackName,
            selectedDomains: store.selectedDomains,
            domainSelections: store.domainSelections,
            technologyCount,
            skillCount: technologyCount,
            agentCount: store.selectedAgents.length,
            installMode: store.installMode,
            onBack: store.goBack
          }
        );
      }
      default:
        return null;
    }
  };
  if (isNarrowTerminal || isShortTerminal) {
    const issue = isNarrowTerminal ? `too narrow (${terminalWidth} columns, need ${MIN_TERMINAL_WIDTH})` : `too short (${terminalHeight} rows, need ${MIN_TERMINAL_HEIGHT})`;
    return /* @__PURE__ */ jsx(ThemeProvider, { theme: cliTheme, children: /* @__PURE__ */ jsx(Box, { flexDirection: "column", padding: 1, children: /* @__PURE__ */ jsxs(Text, { color: CLI_COLORS.WARNING, children: [
      "Terminal ",
      issue,
      ". Please resize your terminal."
    ] }) }) });
  }
  return /* @__PURE__ */ jsx(ThemeProvider, { theme: cliTheme, children: /* @__PURE__ */ jsx(WizardLayout, { version, marketplaceLabel, logo, children: renderStep() }) });
};

export {
  Wizard
};
//# sourceMappingURL=chunk-XTRPYUWK.js.map