import * as fs from 'fs';
import * as path from 'path';
/**
 * 
 * 脚本用来自动引入所有的json文件
 */

function generateImports() {
    const localesPath = path.join(__dirname, 'locales');
    const languages = fs.readdirSync(localesPath);
    let imports = '';
    let translationsMap = 'const translations = {\n';

    languages.forEach(lang => {
        const langPath = path.join(localesPath, lang);
        if (fs.statSync(langPath).isDirectory()) {
            const files = fs.readdirSync(langPath).filter(file => file.endsWith('.json'));

            files.forEach(file => {
                const ns = file.replace('.json', '');
                const varName = `${lang}${ns.charAt(0).toUpperCase() + ns.slice(1)}`;

                imports += `import ${varName} from './locales/${lang}/${file}';\n`;
            });

            translationsMap += `  ${lang}: {\n`;
            files.forEach((file, index) => {
                const ns = file.replace('.json', '');
                const varName = `${lang}${ns.charAt(0).toUpperCase() + ns.slice(1)}`;
                translationsMap += `    ${ns}: ${varName}${index < files.length - 1 ? ',' : ''}\n`;
            });
            translationsMap += '  },\n';
        }
    });

    translationsMap += '};\n\nexport default translations;';

    const output = `// 自动生成的导入文件，请勿手动修改\n\n${imports}\n${translationsMap}`;
    fs.writeFileSync(path.join(__dirname, 'translations-generated.ts'), output);
}

generateImports();