const fs = require('fs');

try {
    let appFinalJs = fs.readFileSync('app_final.js', 'utf8');

    // Remove references to sales-payment-now-container
    appFinalJs = appFinalJs.replace(/document\.getElementById\('sales-payment-now-container'\)\.style\.display = 'none';/g, '');
    appFinalJs = appFinalJs.replace(/document\.getElementById\('sales-payment-now-container'\)\.style\.display = 'flex';/g, '');
    
    // Remove references to purchases-payment-now-container
    appFinalJs = appFinalJs.replace(/document\.getElementById\('purchases-payment-now-container'\)\.style\.display = 'none';/g, '');
    appFinalJs = appFinalJs.replace(/document\.getElementById\('purchases-payment-now-container'\)\.style\.display = 'flex';/g, '');

    fs.writeFileSync('app_final.js', appFinalJs, 'utf8');

    // Bust cache
    let indexHtml = fs.readFileSync('index.html', 'utf8');
    const timestamp = Date.now();
    indexHtml = indexHtml.replace(/app_final\.js\?v=\d+/g, 'app_final.js?v=' + timestamp);
    fs.writeFileSync('index.html', indexHtml, 'utf8');

    console.log('Fixed openSalesModal and openPurchasesModal crashes.');
} catch (e) {
    console.error('Error patching:', e);
}
