const fs = require('fs');
let js = fs.readFileSync('vouchers_returns.js', 'utf8');

js = js.replace(/const treasurySelect = document\.getElementById\('purchases-returns-treasury'\);\n\s*treasurySelect\.innerHTML = '[^']*';\n\s*currentAccounts\.filter\([\s\S]*?\}\);/g, (match) => {
    return `const treasurySelect = document.getElementById('purchases-returns-treasury');\n    if (treasurySelect) {\n        ${match.replace(/const treasurySelect = document\.getElementById\('purchases-returns-treasury'\);\n\s*/, '').replace(/\n/g, '\n        ')}\n    }`;
});

fs.writeFileSync('vouchers_returns.js', js, 'utf8');
console.log("Patched treasurySelect in openPurchasesReturnModal.");
