const fs = require('fs');
const path = require('path');

const stringsToTranslate = fs.readFileSync('true_code.txt', 'utf8').split('\n').filter(l => l.trim().length > 0 && !l.trim().startsWith('*'));

// Exclude i18n.service.ts Chinese/Japanese prompt templates
const filtered = stringsToTranslate.filter(s => {
    if (s.includes('你是一个文档分析师')) return false;
    if (s.includes('あなたはドキュメントアナライザーです')) return false;
    if (s.includes('基于以下知识库内容回答用户问题')) return false;
    if (s.includes('以下のナレッジベースの内容に基づいてユーザーの質問に答えてください')) return false;
    if (s.includes('请用Chinese回答')) return false;
    if (s.includes('Japaneseで回答してください')) return false;
    if (s.includes('用户问题：{question}')) return false;
    if (s.includes('历史对话：')) return false;
    if (s.includes('知识库内容：')) return false;
    if (s.includes('作为智能助手')) return false;
    if (s.includes('片段：')) return false;
    if (s.includes('スニペット：')) return false;
    return true;
});

async function translateText(text) {
    try {
        const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=${encodeURIComponent(text)}`;
        const res = await fetch(url);
        const data = await res.json();
        return data[0].map(x => x[0]).join('');
    } catch (e) {
        return null;
    }
}

async function main() {
    console.log(`Starting translation for ${filtered.length} strings...`);
    const dict = {};
    for (let i = 0; i < filtered.length; i++) {
        const s = filtered[i];
        const translated = await translateText(s);
        if (translated) {
            dict[s] = translated;
            if (i % 10 === 0) console.log(`Translated ${i + 1}/${filtered.length}`);
        } else {
            console.log(`Failed to translate: ${s}`);
        }
        await new Promise(r => setTimeout(r, 200)); // Sleep to avoid rate limits
    }
    fs.writeFileSync('auto_dict.json', JSON.stringify(dict, null, 2));
    console.log('Successfully generated auto_dict.json');
}

main();
