#!/usr/bin/env node
import {
  ensureDir,
  getErrorMessage,
  glob,
  pluginManifestSchema,
  readFileSafe,
  verbose,
  warn,
  writeFile
} from "./chunk-C4QI54PN.js";
import {
  MAX_PLUGIN_FILE_SIZE
} from "./chunk-FTD5Z6QD.js";
import {
  init_esm_shims
} from "./chunk-DHET7RCE.js";

// src/cli/lib/marketplace-generator.ts
init_esm_shims();
import path from "path";
import { sortBy } from "remeda";
var PLUGIN_MANIFEST_PATH = ".claude-plugin/plugin.json";
var MARKETPLACE_SCHEMA_URL = "https://anthropic.com/claude-code/marketplace.schema.json";
var CATEGORY_PATTERNS = [
  { pattern: /^web-/, category: "web" },
  { pattern: /^api-/, category: "api" },
  { pattern: /^cli-/, category: "cli" },
  { pattern: /^meta-/, category: "methodology" },
  { pattern: /^infra-/, category: "infra" },
  { pattern: /^mobile-/, category: "mobile" },
  { pattern: /^security-/, category: "security" }
];
function inferCategoryFromPluginName(pluginName) {
  for (const { pattern, category } of CATEGORY_PATTERNS) {
    if (pattern.test(pluginName)) {
      return category;
    }
  }
  return void 0;
}
async function readPluginManifest(pluginDir) {
  const manifestPath = path.join(pluginDir, PLUGIN_MANIFEST_PATH);
  try {
    const content = await readFileSafe(manifestPath, MAX_PLUGIN_FILE_SIZE);
    return pluginManifestSchema.parse(JSON.parse(content));
  } catch (error) {
    verbose(`Failed to read plugin manifest at '${manifestPath}': ${getErrorMessage(error)}`);
    return null;
  }
}
function convertManifestToMarketplacePlugin(manifest, pluginRoot, pluginDirName) {
  const category = inferCategoryFromPluginName(manifest.name);
  const plugin = {
    name: manifest.name,
    source: `./${pluginRoot}/${pluginDirName}`,
    description: manifest.description,
    version: manifest.version,
    author: manifest.author,
    keywords: manifest.keywords
  };
  if (category) {
    plugin.category = category;
  }
  return plugin;
}
async function generateMarketplace(pluginsDir, options) {
  verbose(`Scanning plugins directory: ${pluginsDir}`);
  const manifestFiles = await glob(`**/${PLUGIN_MANIFEST_PATH}`, pluginsDir);
  verbose(`Found ${manifestFiles.length} plugin manifests`);
  const plugins = [];
  for (const manifestFile of manifestFiles) {
    const pluginDirName = manifestFile.split("/")[0];
    const pluginDir = path.join(pluginsDir, pluginDirName);
    const manifest = await readPluginManifest(pluginDir);
    if (!manifest) {
      warn(`Could not read plugin manifest: '${manifestFile}'`);
      continue;
    }
    const plugin = convertManifestToMarketplacePlugin(
      manifest,
      options.pluginRoot.replace(/^\.\//, ""),
      pluginDirName
    );
    plugins.push(plugin);
    verbose(`  [OK] ${plugin.name}`);
  }
  const sortedPlugins = sortBy(plugins, (p) => p.name);
  const marketplace = {
    $schema: MARKETPLACE_SCHEMA_URL,
    name: options.name,
    version: options.version ?? "1.0.0",
    owner: {
      name: options.ownerName
    },
    metadata: {
      pluginRoot: options.pluginRoot
    },
    plugins: sortedPlugins
  };
  if (options.description) {
    marketplace.description = options.description;
  }
  if (options.ownerEmail) {
    marketplace.owner.email = options.ownerEmail;
  }
  return marketplace;
}
async function writeMarketplace(outputPath, marketplace) {
  await ensureDir(path.dirname(outputPath));
  const content = `${JSON.stringify(marketplace, null, 2)}
`;
  await writeFile(outputPath, content);
}
function getMarketplaceStats(marketplace) {
  const byCategory = {};
  for (const plugin of marketplace.plugins) {
    const category = plugin.category ?? "uncategorized";
    byCategory[category] = (byCategory[category] ?? 0) + 1;
  }
  return {
    total: marketplace.plugins.length,
    byCategory
  };
}

export {
  generateMarketplace,
  writeMarketplace,
  getMarketplaceStats
};
//# sourceMappingURL=chunk-TWDVLTU6.js.map