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

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

js = js.replace(/const treasurySelect = document\.getElementById\('sales-returns-treasury'\);\s*if \(treasurySelect\) \{/g, "const treasurySelect = document.getElementById('sales-returns-treasury');\n    if (treasurySelect) {");

fs.writeFileSync('vouchers_returns.js', js, 'utf8');
console.log("Patched treasury selects in open modal functions.");
