#!/usr/bin/env node
import {
  StepSettings
} from "./chunk-52M2XF3W.js";
import {
  StepSources
} from "./chunk-SZRK3VOR.js";
import {
  StepStack
} from "./chunk-MKCHLXMY.js";
import {
  WizardLayout
} from "./chunk-EMIUPGPL.js";
import {
  StepAgents
} from "./chunk-SEJF7CGJ.js";
import {
  StepBuild
} from "./chunk-TC3NHO34.js";
import {
  StepConfirm
} from "./chunk-KIWFEBKH.js";
import {
  getStackName
} from "./chunk-BKJHAJQW.js";
import {
  useWizardStore
} from "./chunk-WF6RM73R.js";
import {
  cliTheme
} from "./chunk-XUDTFI4M.js";
import {
  resolveAlias,
  validateSelection
} from "./chunk-TZXYBG3R.js";
import {
  warn
} from "./chunk-LWXRUR6B.js";
import {
  CLI_COLORS
} from "./chunk-6OWHQ7HM.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,
  initialDomains,
  initialAgents,
  installedSkillIds,
  installedSkillConfigs,
  lockedSkillIds,
  lockedAgentNames
}) {
  const initialized = useRef(false);
  if (!initialized.current) {
    initialized.current = true;
    if (initialStep) {
      if (installedSkillIds?.length) {
        useWizardStore.getState().populateFromSkillIds(
          installedSkillIds,
          matrix.skills,
          matrix.categories,
          installedSkillConfigs
        );
      }
      useWizardStore.setState({ step: initialStep, approach: "scratch" });
    }
    if (initialDomains?.length) {
      useWizardStore.setState({ selectedDomains: initialDomains });
    }
    if (initialAgents?.length) {
      useWizardStore.setState({ selectedAgents: initialAgents });
    }
    if (lockedSkillIds?.length || lockedAgentNames?.length) {
      useWizardStore.setState({
        ...lockedSkillIds?.length && { lockedSkillIds },
        ...lockedAgentNames?.length && { lockedAgentNames }
      });
    }
  }
}

// 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(
    (categoryId, techId) => {
      const domain = store.getCurrentDomain() || "web";
      const cat = matrix.categories[categoryId];
      store.toggleTechnology(domain, categoryId, 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,
    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,
  initialDomains,
  initialAgents,
  installedSkillIds,
  installedSkillConfigs,
  lockedSkillIds,
  lockedAgentNames,
  projectDir,
  startupMessages
}) => {
  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,
    initialDomains,
    initialAgents,
    installedSkillIds,
    installedSkillConfigs,
    lockedSkillIds,
    lockedAgentNames
  });
  const buildStepProps = useBuildStepProps({ store, matrix, installedSkillIds });
  useInput((input, key) => {
    if (store.showSettings) {
      if (input === "s" || input === "S") {
        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 === "s" || input === "S") && store.step === "build") {
      const focused = store.focusedSkillId;
      if (focused) {
        store.toggleSkillScope(focused);
      }
      return;
    }
    if ((input === "s" || input === "S") && store.step === "agents") {
      const focused = store.focusedAgentId;
      if (focused) {
        store.toggleAgentScope(focused);
      }
      return;
    }
    if ((input === "s" || input === "S") && store.step === "sources") {
      store.toggleSettings();
      return;
    }
  });
  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) => resolveAlias(tech, matrix));
    }
    const methodologySkills = store.getDefaultMethodologySkills();
    for (const skill of methodologySkills) {
      if (!allSkills.includes(skill)) {
        allSkills.push(skill);
      }
    }
    const skillConfigs = allSkills.map((id) => {
      const existing = store.skillConfigs.find((sc) => sc.id === id);
      return existing ?? { id, scope: "project", source: "local" };
    });
    const validation = validateSelection(allSkills, matrix);
    const result = {
      skills: skillConfigs,
      selectedAgents: store.selectedAgents,
      agentConfigs: store.agentConfigs,
      selectedStackId: store.selectedStackId,
      domainSelections: store.domainSelections,
      selectedDomains: store.selectedDomains,
      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 selectedSkills = store.getAllSelectedTechnologies();
        return /* @__PURE__ */ jsx(
          StepConfirm,
          {
            onComplete: handleComplete,
            stackName,
            selectedDomains: store.selectedDomains,
            domainSelections: store.domainSelections,
            technologyCount: selectedSkills.length,
            skillCount: selectedSkills.length,
            agentCount: store.selectedAgents.length,
            skillConfigs: store.skillConfigs,
            agentConfigs: store.agentConfigs,
            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,
      startupMessages,
      children: renderStep()
    }
  ) });
};

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