const fs = require('fs');

const filesToPatch = [
    'app_final.js',
    'vouchers_returns.js',
    'subcontracts.js',
    'sales_purchases_module.js'
];

filesToPatch.forEach(file => {
    if (fs.existsSync(file)) {
        let js = fs.readFileSync(file, 'utf8');
        
        // Split into lines to safely replace only when it's not a Date object
        const lines = js.split('\n');
        for (let i = 0; i < lines.length; i++) {
            if (lines[i].includes("toLocaleString('ar-EG')") && !lines[i].includes('Date')) {
                lines[i] = lines[i].replace(/toLocaleString\('ar-EG'\)/g, "toLocaleString('en-US')");
            }
        }
        
        fs.writeFileSync(file, lines.join('\n'), 'utf8');
        console.log(`Patched toLocaleString in ${file}`);
    }
});

// Cache buster
if (fs.existsSync('index.html')) {
    let html = fs.readFileSync('index.html', 'utf8');
    html = html.replace(/v=\d+/g, 'v=' + Date.now());
    fs.writeFileSync('index.html', html, 'utf8');
}
