const fs = require('fs');

const files = require('./files_to_translate.json');
const translationMap = {};

const logRegex = /(?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*((?:[\'\"\`])(?:.*?)[\u4e00-\u9fa5]+(?:.*?)(?:[\'\"\`]))/g;
// We also need to catch template literals that have variables, e.g. `User ${userId} created`
const logRegexTemplate = /(?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*(\`(?:.*?)\`)/gs;

files.forEach(file => {
    const content = fs.readFileSync(file, 'utf8');
    let match;
    // Match single/double quotes with Chinese
    const simpleStringRegex = /(?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*(['"])(.*?[\u4e00-\u9fa5]+.*?)\1/g;
    while ((match = simpleStringRegex.exec(content)) !== null) {
        translationMap[match[2]] = ""; // The inner string
    }

    // Match template literals with Chinese
    const templateRegex = /(?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*\`([\s\S]*?)\`/g;
    while ((match = templateRegex.exec(content)) !== null) {
        if (/[\u4e00-\u9fa5]/.test(match[1])) {
            translationMap[match[1]] = "";
        }
    }
});

fs.writeFileSync('translation_map.json', JSON.stringify(translationMap, null, 2));
console.log('Extracted ' + Object.keys(translationMap).length + ' distinct strings.');
