const fs = require('fs');

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

    if (!js.includes('window.openSalesReturnsModal = window.openSalesReturnModal;')) {
        js += "\n\n// Aliases for HTML buttons\nwindow.openSalesReturnsModal = window.openSalesReturnModal;\nwindow.openPurchasesReturnsModal = window.openPurchasesReturnModal;\n";
        fs.writeFileSync('vouchers_returns.js', js, 'utf8');
        console.log("Added aliases to vouchers_returns.js");
        
        let html = fs.readFileSync('index.html', 'utf8');
        html = html.replace(/vouchers_returns\.js\?v=\d+/g, 'vouchers_returns.js?v=' + Date.now());
        fs.writeFileSync('index.html', html, 'utf8');
        console.log("Cache busted index.html");
    } else {
        console.log("Aliases already exist.");
    }
} catch (e) {
    console.error(e);
}
