#!/usr/bin/env node
import {
  generateMarketplace,
  writeMarketplace
} from "../../chunk-TWDVLTU6.js";
import {
  BaseCommand,
  EXIT_CODES
} from "../../chunk-LNA6M2IE.js";
import {
  compileAllSkillPlugins
} from "../../chunk-GFDGYQ6M.js";
import "../../chunk-T4EXUIBY.js";
import {
  directoryExists,
  ensureDir,
  getErrorMessage,
  writeFile
} from "../../chunk-C4QI54PN.js";
import {
  CLI_BIN_NAME,
  KEBAB_CASE_PATTERN,
  PLUGIN_MANIFEST_DIR,
  SKILLS_DIR_PATH,
  STACKS_FILE_PATH,
  STANDARD_FILES
} from "../../chunk-FTD5Z6QD.js";
import {
  init_esm_shims
} from "../../chunk-DHET7RCE.js";

// src/cli/commands/new/marketplace.ts
init_esm_shims();
import { Args, Flags } from "@oclif/core";
import path from "path";
function validateMarketplaceName(name) {
  if (!name || name.trim() === "") {
    return "Marketplace name is required";
  }
  if (!KEBAB_CASE_PATTERN.test(name)) {
    return "Marketplace name must be kebab-case (lowercase letters, numbers, and hyphens, starting with a letter)";
  }
  return null;
}
function generateStacksYaml(name) {
  return `# Stack definitions for ${name}

stacks:
  - id: dummy-stack
    name: Dummy Stack
    description: Default stack for ${name}
    agents:
      web-developer:
        dummy-category: dummy-skill
    philosophy: Ship fast, iterate faster
`;
}
function generateReadme(name) {
  return `# ${name}

Private marketplace for custom skills and stacks.

## Directory Structure

\`\`\`
${STACKS_FILE_PATH}         # Stack definitions (agent groupings with skill mappings)
${SKILLS_DIR_PATH}/                # Custom skill definitions
\`\`\`

## Creating Skills

\`\`\`bash
${CLI_BIN_NAME} new skill <name> --category <category-name>
\`\`\`

Each skill lives in \`${SKILLS_DIR_PATH}/<skill-name>/\` with:
- \`${STANDARD_FILES.SKILL_MD}\` -- Skill content (what the skill teaches)
- \`${STANDARD_FILES.METADATA_YAML}\` -- Skill metadata (category, author, description, custom: true)

## Using This Marketplace

Point the CLI at this marketplace as a source:

\`\`\`bash
# Local development
${CLI_BIN_NAME} init --source /path/to/${name}

# From a git repository
${CLI_BIN_NAME} init --source github:your-org/${name}
\`\`\`

## How It Works

The CLI auto-discovers skills from the \`${SKILLS_DIR_PATH}/\` directory
and stacks from \`${STACKS_FILE_PATH}\`.
Custom categories are discovered from skill \`${STANDARD_FILES.METADATA_YAML}\` files with \`custom: true\`.
Custom skills appear alongside built-in ones in the wizard. No manual registration needed.
`;
}
var NewMarketplace = class _NewMarketplace extends BaseCommand {
  static summary = "Scaffold a new private marketplace project";
  static description = "Create a new private marketplace directory with the required structure for custom skills and stacks.";
  static examples = [
    {
      description: "Create a new marketplace",
      command: "<%= config.bin %> <%= command.id %> acme-skills"
    },
    {
      description: "Initialize the current directory as a marketplace",
      command: "<%= config.bin %> <%= command.id %> ."
    },
    {
      description: "Create in a specific location",
      command: "<%= config.bin %> <%= command.id %> acme-skills --output ~/projects"
    },
    {
      description: "Overwrite an existing directory",
      command: "<%= config.bin %> <%= command.id %> acme-skills --force"
    }
  ];
  static args = {
    name: Args.string({
      description: 'Name of the marketplace directory (kebab-case), or "." to use the current directory',
      required: true
    })
  };
  static flags = {
    ...BaseCommand.baseFlags,
    force: Flags.boolean({
      char: "f",
      description: "Overwrite existing directory",
      default: false
    }),
    output: Flags.string({
      char: "o",
      description: "Parent directory to create the marketplace in (default: current directory)"
    })
  };
  async run() {
    const { args, flags } = await this.parse(_NewMarketplace);
    this.log("");
    this.log("Create New Marketplace");
    this.log("");
    const parentDir = flags.output ? path.resolve(flags.output) : process.cwd();
    const useCurrentDir = args.name === ".";
    const marketplaceName = useCurrentDir ? path.basename(parentDir) : args.name;
    const marketplaceDir = useCurrentDir ? parentDir : path.join(parentDir, args.name);
    const validationError = validateMarketplaceName(marketplaceName);
    if (validationError) {
      if (useCurrentDir) {
        this.error(
          `Current directory name '${marketplaceName}' is not valid kebab-case. Rename it or pass an explicit name.`,
          { exit: EXIT_CODES.INVALID_ARGS }
        );
      }
      this.error(validationError, { exit: EXIT_CODES.INVALID_ARGS });
    }
    if (!useCurrentDir && await directoryExists(marketplaceDir)) {
      if (!flags.force) {
        this.error(`Directory already exists: ${marketplaceDir}
Use --force to overwrite.`, {
          exit: EXIT_CODES.ERROR
        });
      }
      this.warn(`Overwriting existing directory at ${marketplaceDir}`);
    }
    this.log(`Marketplace: ${marketplaceName}`);
    this.log(`Directory: ${marketplaceDir}`);
    this.log("");
    const skillName = "dummy-skill";
    const skillPath = path.join(SKILLS_DIR_PATH, skillName);
    if (flags["dry-run"]) {
      this.log("[DRY RUN] Would create marketplace structure:");
      this.log(`  ${STACKS_FILE_PATH}`);
      this.log(`  ${skillPath}/${STANDARD_FILES.SKILL_MD}`);
      this.log(`  ${skillPath}/${STANDARD_FILES.METADATA_YAML}`);
      this.log("  README.md");
      return;
    }
    this.log("Creating marketplace structure...");
    try {
      const stacksContent = generateStacksYaml(marketplaceName);
      const stacksPath = path.join(marketplaceDir, STACKS_FILE_PATH);
      await ensureDir(path.dirname(stacksPath));
      await writeFile(stacksPath, stacksContent);
      const skillsDir = path.join(marketplaceDir, SKILLS_DIR_PATH);
      const skillArgs = [skillName, "--output", skillsDir];
      if (flags.force) skillArgs.push("--force");
      await this.config.runCommand("new:skill", skillArgs);
      const readmeContent = generateReadme(marketplaceName);
      const readmePath = path.join(marketplaceDir, "README.md");
      await writeFile(readmePath, readmeContent);
      this.log("");
      this.logSuccess(`Created ${STACKS_FILE_PATH}`);
      this.logSuccess("Created README.md");
      this.log("");
      await this.buildMarketplace(marketplaceDir, marketplaceName);
      this.log("Marketplace created successfully!");
      this.log("");
      this.log("Next steps:");
      if (!useCurrentDir) {
        this.log(`  1. cd ${marketplaceName}`);
      }
      this.log(
        `  ${useCurrentDir ? "1" : "2"}. ${CLI_BIN_NAME} new skill <name> --category <category-name>`
      );
      this.log(`  ${useCurrentDir ? "2" : "3"}. Push to a git repository`);
      this.log(
        `  ${useCurrentDir ? "3" : "4"}. ${CLI_BIN_NAME} init --source github:your-org/${marketplaceName}`
      );
      this.log("");
    } catch (error) {
      this.handleError(error);
    }
  }
  async buildMarketplace(marketplaceDir, marketplaceName) {
    const skillsDir = path.resolve(marketplaceDir, SKILLS_DIR_PATH);
    const pluginsOutputDir = path.resolve(marketplaceDir, "dist/plugins");
    const marketplaceOutputPath = path.resolve(
      marketplaceDir,
      PLUGIN_MANIFEST_DIR,
      "marketplace.json"
    );
    try {
      this.log("Building plugins...");
      const results = await compileAllSkillPlugins(skillsDir, pluginsOutputDir);
      this.logSuccess(`Built ${results.length} skill plugins.`);
      this.log("Generating marketplace.json...");
      const marketplace = await generateMarketplace(pluginsOutputDir, {
        name: marketplaceName,
        ownerName: marketplaceName,
        pluginRoot: "./dist/plugins"
      });
      await writeMarketplace(marketplaceOutputPath, marketplace);
      this.logSuccess(
        `Generated ${PLUGIN_MANIFEST_DIR}/marketplace.json with ${marketplace.plugins.length} plugins.`
      );
    } catch (error) {
      this.warn(`Build step failed: ${getErrorMessage(error)}`);
      this.warn(
        "The scaffold is still valid. Run 'build plugins' and 'build marketplace' manually."
      );
    }
  }
};
export {
  NewMarketplace as default,
  generateReadme,
  generateStacksYaml,
  validateMarketplaceName
};
//# sourceMappingURL=marketplace.js.map