"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TranslationModule = void 0;
const path_1 = require("path");
const logger_js_1 = require("../../utils/logger.js");
const file_ops_js_1 = require("../data-store/file-ops.js");
const logger = (0, logger_js_1.createModuleLogger)('translation');
const GLOSSARY_FILENAME = 'glossary.json';
const TRANSLATION_SYSTEM_PROMPT = `You are a specialized translator for AI image generation prompts.
Your task is to translate Turkish image generation prompts into English.

Rules:
- Translate the Turkish prompt into natural, fluent English suitable for AI image generation models.
- Preserve all technical image generation terms (e.g., lighting terms, camera settings, art styles).
- Keep proper nouns, brand names, and model names unchanged.
- Maintain the structure and order of the prompt elements.
- Do NOT add any explanation or commentary. Output ONLY the translated English prompt.
- If the input contains English words or phrases already, keep them as-is.
- Focus on accuracy for visual description terms: colors, poses, clothing, environments, lighting, camera angles.`;
class TranslationModule {
    dataStore;
    openRouterClient;
    constructor(dataStore, openRouterClient) {
        this.dataStore = dataStore;
        this.openRouterClient = openRouterClient;
    }
    /**
     * Translates a Turkish image generation prompt to English.
     * Applies glossary pre-processing, then uses OpenRouter AI for translation.
     * Falls back to returning the original prompt with enableTranslation hint on failure.
     */
    async translatePrompt(turkishPrompt) {
        const glossary = await this.getGlossary();
        const preprocessed = this.applyGlossary(turkishPrompt, glossary);
        try {
            const messages = [
                { role: 'system', content: TRANSLATION_SYSTEM_PROMPT },
                { role: 'user', content: preprocessed },
            ];
            const response = await this.openRouterClient.chatCompletion(messages, {
                temperature: 0.7,
                max_tokens: 500,
            });
            const translated = response.choices?.[0]?.message?.content?.trim();
            if (!translated) {
                logger.warn('Empty translation response from OpenRouter, using fallback');
                return this.fallbackTranslation(turkishPrompt);
            }
            logger.info({ original: turkishPrompt, translated }, 'Prompt translated successfully');
            return { original: turkishPrompt, translated };
        }
        catch (error) {
            logger.error({ error, prompt: turkishPrompt }, 'Translation failed, using fallback');
            return this.fallbackTranslation(turkishPrompt);
        }
    }
    /**
     * Returns the current glossary (Turkish → English term mappings).
     */
    async getGlossary() {
        const glossaryPath = this.getGlossaryPath();
        const glossary = await (0, file_ops_js_1.readJSON)(glossaryPath);
        return glossary ?? {};
    }
    /**
     * Replaces the entire glossary with the provided terms.
     */
    async updateGlossary(terms) {
        const glossaryPath = this.getGlossaryPath();
        await (0, file_ops_js_1.atomicWriteJSON)(glossaryPath, terms);
        logger.info({ termCount: Object.keys(terms).length }, 'Glossary updated');
    }
    /**
     * Adds a single term to the glossary.
     */
    async addGlossaryTerm(turkish, english) {
        const glossary = await this.getGlossary();
        glossary[turkish] = english;
        await this.updateGlossary(glossary);
        logger.info({ turkish, english }, 'Glossary term added');
    }
    /**
     * Removes a single term from the glossary.
     */
    async removeGlossaryTerm(turkish) {
        const glossary = await this.getGlossary();
        delete glossary[turkish];
        await this.updateGlossary(glossary);
        logger.info({ turkish }, 'Glossary term removed');
    }
    /**
     * Applies glossary term replacements to the text before AI translation.
     * Replaces Turkish terms with their English equivalents.
     * Longer terms are replaced first to avoid partial matches.
     */
    applyGlossary(text, glossary) {
        if (!glossary || Object.keys(glossary).length === 0)
            return text;
        let result = text;
        // Sort by length descending so longer terms are replaced first
        const sortedTerms = Object.keys(glossary).sort((a, b) => b.length - a.length);
        for (const turkish of sortedTerms) {
            const english = glossary[turkish];
            // Case-insensitive replacement
            const regex = new RegExp(escapeRegex(turkish), 'gi');
            result = result.replace(regex, english);
        }
        return result;
    }
    /**
     * Fallback when OpenRouter translation fails.
     * Returns the original prompt as-is — the caller can use KIE API's enableTranslation flag.
     */
    fallbackTranslation(turkishPrompt) {
        logger.warn('Using fallback translation (KIE enableTranslation recommended)');
        return { original: turkishPrompt, translated: turkishPrompt };
    }
    getGlossaryPath() {
        return (0, path_1.resolve)(this.dataStore.dataDir, 'translations', GLOSSARY_FILENAME);
    }
}
exports.TranslationModule = TranslationModule;
function escapeRegex(str) {
    return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
//# sourceMappingURL=index.js.map