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

const directories = ['d:/workspace/AuraK/web', 'd:/workspace/AuraK/server/src'];
const excludeDirs = ['node_modules', '.git', 'dist', '.next', 'dist-check'];
const extensions = ['.ts', '.tsx', '.js', '.jsx'];

const cjkPattern = /[\u4e00-\u9fff\u3040-\u309f\u30a0-\u30ff]+/;
const cjkLines = {};

function walkSync(currentDirPath, callback) {
    fs.readdirSync(currentDirPath).forEach((name) => {
        const filePath = path.join(currentDirPath, name);
        const stat = fs.statSync(filePath);
        if (stat.isFile()) {
            callback(filePath, stat);
        } else if (stat.isDirectory() && !excludeDirs.includes(name)) {
            walkSync(filePath, callback);
        }
    });
}

directories.forEach(d => {
    walkSync(d, (filePath) => {
        if (extensions.some(ext => filePath.endsWith(ext))) {
            try {
                const content = fs.readFileSync(filePath, 'utf-8');
                const lines = content.split('\n');
                lines.forEach((line, i) => {
                    if (cjkPattern.test(line)) {
                        if (!cjkLines[filePath]) {
                            cjkLines[filePath] = [];
                        }
                        cjkLines[filePath].push({ line: i + 1, text: line.trim() });
                    }
                });
            } catch (e) {
                console.error(`Error reading ${filePath}: `, e);
            }
        }
    });
});

fs.writeFileSync('cjk_extract.json', JSON.stringify(cjkLines, null, 2), 'utf-8');
console.log('Extracted to cjk_extract.json');
