import { parseTja } from './parse';
import { factorize } from './factorize';
import { readdirSync, readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { join, dirname } from 'path';
import iconv from 'iconv-lite';

const [,, workingDir, dataDir] = process.argv;

const featurePath = join(workingDir, 'factors.json');

if (existsSync(featurePath)) {
    console.log('factors.json already exists. Skipping extraction.');
    process.exit(0);
}

// 작업 디렉토리 존재 확인 및 생성
if (!existsSync(workingDir)) {
    mkdirSync(workingDir, { recursive: true });
}

const tjaDir = join(dataDir, 'tja');
const results: any[] = [];

for (const file of readdirSync(tjaDir)) {
    if (!file.endsWith('.tja')) continue;
    
    const songno = file.replace(/\D/g, '');
    const buffer = readFileSync(join(tjaDir, file));
    
    let content = iconv.decode(buffer, 'shift-jis', { stripBOM: true });
    content = content.replace(/\uFFFD/g, '').replace(/[\u{0080}-\u{FFFF}]/gu, '').replace(/\r\n/g, '\n');
    let parsed = parseTja(content);

    if (!parsed) {
        content = iconv.decode(buffer, 'utf-8', { stripBOM: true });
        content = content.replace(/\uFFFD/g, '').replace(/[\u{0080}-\u{FFFF}]/gu, '').replace(/\r\n/g, '\n');
        parsed = parseTja(content);
    }
    
    if (!parsed) continue;

    for (const diff of ['oni', 'edit'] as const) {
        if (!parsed[diff]) continue;
        const factors = factorize(parsed[diff]!);
        results.push({ songno, diff: diff === 'oni' ? 'oni' : 'ura', ...factors });
    }
}
writeFileSync(featurePath, JSON.stringify(results, null, 2));
console.log(`Features extracted to ${featurePath}`);
