import * as fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { MULTI_EXPORT_PACKAGES } from "./constants.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function toKebabCase(str) {
  return str.replaceAll(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
}
function toPascalCase(str) {
  return str.split("-").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
}
function findExistingPath(resourceName, possiblePaths, throwIfNotFound = true) {
  for (const resourcePath of possiblePaths) {
    try {
      if (fs.existsSync(resourcePath)) {
        return resourcePath;
      }
    } catch {
    }
  }
  if (throwIfNotFound) {
    throw new Error(
      `${resourceName} not found in any of: ${possiblePaths.join(", ")}`
    );
  }
  return null;
}
function loadManifest(manifestName) {
  const manifestPath = findExistingPath(
    `Required manifest file '${manifestName}'`,
    [
      // In production (when installed via npm), manifests are in the same directory as this file
      path.join(__dirname, manifestName),
      // During development/testing, manifests are in dist/cli
      path.join(__dirname, "../../dist/cli", manifestName)
    ]
  );
  return JSON.parse(fs.readFileSync(manifestPath, "utf8"));
}
function loadDesignEngineer() {
  const promptPath = findExistingPath("Design engineer prompt file", [
    // In production (when installed via npm), the prompt should be bundled with the package
    path.join(__dirname, "../../src/prompts/DESIGN_ENGINEER.md"),
    // During development/testing
    path.join(__dirname, "../prompts/DESIGN_ENGINEER.md")
  ]);
  return fs.readFileSync(promptPath, "utf8");
}
function discoverAllComponents() {
  const listManifest = loadManifest("components-list.json");
  return listManifest;
}
function isGroupedComponent(name) {
  return MULTI_EXPORT_PACKAGES.includes(name);
}
function loadAllComponentInfo(options = {}) {
  const manifestFile = options.verbose ? "components-info-verbose.json" : "components-info.json";
  return loadManifest(manifestFile);
}
function getComponentInfo(componentName, options = {}) {
  const manifestFile = options.verbose ? "components-info-verbose.json" : "components-info.json";
  const infoManifest = loadManifest(manifestFile);
  if (!infoManifest[componentName]) {
    return null;
  }
  return infoManifest[componentName];
}
export {
  discoverAllComponents,
  findExistingPath,
  getComponentInfo,
  isGroupedComponent,
  loadAllComponentInfo,
  loadDesignEngineer,
  toKebabCase,
  toPascalCase
};
