#!/usr/bin/env node
import {
  getAgentDefinitions
} from "../../chunk-YHCYKUA3.js";
import {
  compileStackPlugin,
  loadStacks,
  printStackCompilationSummary
} from "../../chunk-TZXYBG3R.js";
import "../../chunk-OCEFD7V6.js";
import "../../chunk-T4EXUIBY.js";
import {
  BaseCommand,
  EXIT_CODES
} from "../../chunk-VR3CDXDT.js";
import {
  setVerbose
} from "../../chunk-LWXRUR6B.js";
import {
  PROJECT_ROOT,
  STACKS_FILE_PATH
} from "../../chunk-6OWHQ7HM.js";
import "../../chunk-EC3UJRKZ.js";
import {
  init_esm_shims
} from "../../chunk-DHET7RCE.js";

// src/cli/commands/build/stack.tsx
init_esm_shims();
import { Flags } from "@oclif/core";
import { Select } from "@inkjs/ui";
import { render, Box, Text } from "ink";
import path from "path";
import { jsx, jsxs } from "react/jsx-runtime";
var DEFAULT_OUTPUT_DIR = "dist/stacks";
var StackSelector = ({ availableStacks, onSelect }) => {
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
    /* @__PURE__ */ jsx(Text, { children: "Select a stack to compile:" }),
    /* @__PURE__ */ jsx(
      Select,
      {
        options: availableStacks.map((name) => ({
          value: name,
          label: name
        })),
        onChange: (value) => {
          onSelect(value);
        }
      }
    )
  ] });
};
var BuildStack = class _BuildStack extends BaseCommand {
  static summary = "Build a stack into a standalone plugin";
  static description = "Build a stack into a standalone plugin (requires skills repo). If no stack is specified, you will be prompted to select one.";
  static examples = [
    "<%= config.bin %> <%= command.id %>",
    "<%= config.bin %> <%= command.id %> --stack frontend-stack",
    "<%= config.bin %> <%= command.id %> --stack frontend-stack --output-dir ./plugins",
    "<%= config.bin %> <%= command.id %> --agent-source /path/to/agents --refresh"
  ];
  static flags = {
    ...BaseCommand.baseFlags,
    stack: Flags.string({
      description: "Stack ID to compile (directory name in src/stacks/)"
    }),
    "output-dir": Flags.string({
      char: "o",
      description: "Output directory",
      default: DEFAULT_OUTPUT_DIR
    }),
    "agent-source": Flags.string({
      description: "Agent partials source (default: local CLI)"
    }),
    refresh: Flags.boolean({
      description: "Force refresh remote agent source",
      default: false
    }),
    verbose: Flags.boolean({
      char: "v",
      description: "Enable verbose logging",
      default: false
    })
  };
  async run() {
    const { flags } = await this.parse(_BuildStack);
    setVerbose(flags.verbose);
    const projectRoot = process.cwd();
    const outputDir = path.resolve(projectRoot, flags["output-dir"]);
    let stackId = flags.stack;
    if (!stackId) {
      const stacks = await loadStacks(projectRoot);
      const availableStacks = stacks.map((s) => s.id).sort();
      if (availableStacks.length === 0) {
        this.error(`No stacks found in ${STACKS_FILE_PATH}`, {
          exit: EXIT_CODES.ERROR
        });
      }
      stackId = await new Promise((resolve, reject) => {
        const { waitUntilExit } = render(
          /* @__PURE__ */ jsx(
            StackSelector,
            {
              availableStacks,
              onSelect: (selected) => {
                resolve(selected);
              }
            }
          )
        );
        waitUntilExit().catch(reject);
      }).catch(() => {
        this.log("Cancelled");
        this.exit(EXIT_CODES.CANCELLED);
      });
    }
    this.log("");
    this.log(`Compiling stack plugin: ${stackId}`);
    this.log(`  Output directory: ${outputDir}`);
    this.log("");
    let agentSourcePath;
    try {
      this.log(flags["agent-source"] ? "Fetching agent partials..." : "Loading agent partials...");
      const agentDefs = await getAgentDefinitions(flags["agent-source"], {
        forceRefresh: flags.refresh
      });
      agentSourcePath = agentDefs.sourcePath;
      this.log(
        flags["agent-source"] ? `Agent partials fetched from: ${flags["agent-source"]}` : `Agent partials loaded from: ${PROJECT_ROOT}`
      );
    } catch (error) {
      this.log("Failed to load agent partials");
      this.handleError(error);
    }
    try {
      this.log(`Compiling stack "${stackId}"...`);
      const result = await compileStackPlugin({
        stackId,
        outputDir,
        projectRoot,
        agentSourcePath
      });
      this.log(`Compiled stack plugin: ${result.stackName}`);
      printStackCompilationSummary(result);
      this.log("");
      this.logSuccess("Stack plugin compilation complete!");
    } catch (error) {
      this.log("Compilation failed");
      this.handleError(error);
    }
  }
};
export {
  BuildStack as default
};
//# sourceMappingURL=stack.js.map