import type { ComponentRegistry } from "../registry.js";

interface InspectInput { component: string; }
interface InspectResult {
  name: string; description: string; exportPath: string;
  parts: readonly string[]; requiredParts: readonly string[];
  props: Record<string, unknown>; hasStyledVersion: boolean; example: string;
}

function toKebab(str: string): string {
  return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
}

function generateExample(name: string, parts: readonly string[], requiredParts: readonly string[]): string {
  const lines: string[] = [];
  lines.push(`import { ${name} } from "pettyui/${toKebab(name)}";`);
  lines.push("");
  if (requiredParts.includes("Root") || parts.includes("Root")) {
    lines.push(`<${name}>`);
    for (const part of requiredParts) {
      if (part === "Root") continue;
      lines.push(`  <${name}.${part}>...</${name}.${part}>`);
    }
    lines.push(`</${name}>`);
  } else {
    lines.push(`<${name} />`);
  }
  return lines.join("\n");
}

/** Returns full component schema, parts, and usage example. */
export function handleInspect(registry: ComponentRegistry, input: InspectInput): InspectResult | null {
  const comp = registry.getComponent(input.component);
  if (!comp) return null;
  return {
    name: comp.meta.name, description: comp.meta.description, exportPath: comp.exportPath,
    parts: comp.meta.parts, requiredParts: comp.meta.requiredParts,
    props: comp.jsonSchemas, hasStyledVersion: comp.hasStyledVersion,
    example: generateExample(comp.meta.name, comp.meta.parts, comp.meta.requiredParts),
  };
}
