import { loadDesignEngineer } from "./cli/component-discovery.js";
class CodeCleaner {
  /**
   * Remove markdown fences, normalize spacing, and clean LLM artifacts
   *
   * This combines the best practices from both screen-composer and generate-screen
   * implementations to provide comprehensive code cleaning.
   */
  clean(code) {
    if (typeof code !== "string") return code;
    let cleaned = code.trim();
    cleaned = cleaned.replaceAll(
      /```[a-zA-Z0-9-]*\s*\n?([\s\S]*?)\n?```/g,
      "$1"
    );
    cleaned = cleaned.replaceAll("```", "");
    cleaned = cleaned.replaceAll(
      /`var\(--([^`]*?)\$\{([^}]+)\}([^`]*?)\)`/g,
      (_m, pre, expr, post) => `"var(--${pre}" + ${expr} + "${post})"`
    );
    cleaned = cleaned.replaceAll(/\n{3,}/g, "\n\n");
    const firstImportIdx = cleaned.search(/^\s*import\s+/m);
    if (firstImportIdx > 0) {
      cleaned = cleaned.slice(firstImportIdx);
    }
    cleaned = cleaned.split("\n").filter((line) => !line.includes("```")).join("\n").trim();
    return cleaned;
  }
  /**
   * Extract specific sections from generated code
   */
  extractImports(code) {
    const importMatch = code.match(/import[\s\S]*?from ['"][@\w/-]+['"];?/gm);
    return importMatch ? importMatch.join("\n") : "";
  }
  /**
   * Extract type definitions from generated code
   */
  extractTypes(code) {
    const typeMatch = code.match(
      /(?:interface|type)\s+\w+[\s\S]*?(?=\n(?:interface|type|export|const))/gm
    );
    return typeMatch ? typeMatch.join("\n\n") : void 0;
  }
}
class PromptBuilder {
  designEngineerPrompt;
  constructor() {
    const fullTemplate = loadDesignEngineer();
    const separatorIndex = fullTemplate.indexOf("\n---\n");
    this.designEngineerPrompt = separatorIndex !== -1 ? fullTemplate.substring(0, separatorIndex).trim() : fullTemplate;
  }
  /**
   * Get the design engineer prompt (design guidelines only)
   */
  getDesignEngineerPrompt() {
    return this.designEngineerPrompt;
  }
  /**
   * Format component information for LLM context
   */
  formatComponentDocs(componentInfo) {
    return componentInfo.map((c) => {
      if ("type" in c && c.type === "group") {
        const componentsArray = Array.isArray(c.components) ? c.components : Object.entries(c.components).map(([id, comp]) => ({
          id,
          ...comp
        }));
        const comps = componentsArray.map((comp) => {
          const lines = [`  - ${comp.name}`, `    Import: ${comp.import}`];
          if (comp.description)
            lines.push(`    Description: ${comp.description}`);
          return lines.join("\n");
        }).join("\n\n");
        return [
          `${c.name} (${c.category})`,
          c.description ? `Description: ${c.description}` : "",
          "Components:",
          comps
        ].filter(Boolean).join("\n");
      } else {
        const info = c;
        const lines = [
          `${info.name} (${info.category})`,
          `Import: ${info.import}`
        ];
        if (info.description) lines.push(`Description: ${info.description}`);
        if (info.props) {
          const entries = Object.entries(info.props);
          const required = entries.filter(([, p]) => p.required).map(
            ([name, p]) => `${name}: ${p.type ?? "unknown"}`
          );
          const optional = entries.filter(([, p]) => !p.required).map(([name]) => name);
          if (required.length)
            lines.push(`Required Props: ${required.join(", ")}`);
          if (optional.length) {
            const head = optional.slice(0, 10).join(", ");
            const tail = optional.length > 10 ? ", ..." : "";
            lines.push(`Optional Props: ${head}${tail}`);
          }
        }
        if (info.example) lines.push(`Example: ${info.example}`);
        return lines.join("\n");
      }
    }).join("\n\n---\n\n");
  }
  /**
   * Build a component import map showing correct import paths and props
   */
  buildComponentImportMap(componentInfo) {
    return componentInfo.map((c) => {
      if ("type" in c && c.type === "group") {
        const componentsArray = Array.isArray(c.components) ? c.components : Object.entries(c.components).map(([id, comp]) => ({
          id,
          ...comp
        }));
        return componentsArray.map((comp) => `${comp.name}: ${comp.import}`).join("\n");
      } else {
        const componentInfo2 = c;
        const props = componentInfo2.props ? Object.keys(componentInfo2.props).filter((p) => !p.includes("...")).join(", ") : "standard HTML props";
        return `${componentInfo2.name}: ${componentInfo2.import} // Key props: ${props}`;
      }
    }).join("\n");
  }
}
export {
  CodeCleaner as C,
  PromptBuilder as P
};
