"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TidyAgent = void 0;
const AICore_1 = require("../../core/ai/AICore");
const chalk_1 = __importDefault(require("chalk"));
class TidyAgent {
    constructor(ai) {
        this.name = "TidyAgent";
        this.description = "Suggests semantic names for design tokens and constants based on code context.";
        this.personality = "A meticulous Design System Architect who values consistency, clarity, and semantic meaning.";
        this.llmProvider = "github-models";
        this.expertise = ["Design Systems", "UI/UX Architecture", "Tailwind CSS", "Refactoring"];
        this.ai = ai;
    }
    getAICore() {
        if (!this.ai) {
            try {
                this.ai = AICore_1.AICore.getInstance();
            }
            catch {
                this.ai = AICore_1.AICore.getInstance({
                    workingDirectory: process.cwd(),
                    fallbackEnabled: true,
                });
            }
        }
        return this.ai;
    }
    async run(input) {
        const aiCore = this.getAICore();
        const prd = input.context?.prd || "Standardizing the codebase for better maintainability.";
        const tailwindBlock = input.type === "tailwind_class" && input.allowedReplacements?.length
            ? `
TASK (TAILWIND CLASS):
The literal is a Tailwind utility class that could map to more than one semantic theme class.
Pick EXACTLY ONE replacement from this allow-list (copy the string verbatim): ${JSON.stringify(input.allowedReplacements)}
"suggestedName" MUST be one of those strings exactly (e.g. "bg-background").
`
            : `
TASK:
1. Suggest a semantic, uppercase, snake_case name for this literal (e.g. BRAND_PRIMARY, ERROR_TEXT, MAX_RETRIES).
2. Ensure the name reflects its *purpose* in the code context provided.
3. Provide a brief rationale.
`;
        const prompt = `
YOU ARE A DESIGN SYSTEM ARCHITECT.
A developer found a hardcoded literal in the codebase and wants to turn it into a semantic token or constant.

LITERAL: "${input.literal}"
TYPE: ${input.type}
CODE CONTEXT:
\`\`\`
${input.contextCode}
\`\`\`

PROJECT CONTEXT: ${prd}
${tailwindBlock}

SCHEMA:
{
  "suggestedName": "string",
  "rationale": "string"
}
`;
        try {
            const result = await aiCore.generateStructuredText(prompt, "JSON block matching the provided schema");
            if (input.type === "tailwind_class" &&
                input.allowedReplacements?.length &&
                !input.allowedReplacements.includes(result.suggestedName)) {
                return {
                    suggestedName: input.allowedReplacements[0] ?? result.suggestedName,
                    rationale: `${result.rationale} (corrected to allowed replacement)`,
                };
            }
            return result;
        }
        catch (error) {
            console.error(chalk_1.default.red(`[TidyAgent] Error: ${error.message}`));
            return {
                suggestedName: input.type === "tailwind_class" && input.allowedReplacements?.[0]
                    ? input.allowedReplacements[0]
                    : `PENDING_${input.type.toUpperCase()}`,
                rationale: `Error during generation: ${error.message}`,
            };
        }
    }
    async validate(input) {
        if (!input.literal || !input.contextCode)
            return false;
        if (input.type === "tailwind_class" && (!input.allowedReplacements || input.allowedReplacements.length === 0)) {
            return false;
        }
        return true;
    }
}
exports.TidyAgent = TidyAgent;
//# sourceMappingURL=TidyAgent.js.map