const fs = require('fs');
let code = fs.readFileSync('d:/Easy ERP/save_fix.js', 'utf8');

// Replace window.currentAccounts with allAccounts
code = code.replace(/window\.currentAccounts \?/g, "allAccounts ?");
code = code.replace(/window\.currentAccounts/g, "allAccounts");

// Replace window.currentUsers with allUsers
code = code.replace(/window\.currentUsers/g, "allUsers");

// Inject the await window.DB.getAll
const target = "let custody_account_id = document.getElementById('user-custody')";
const injection = `
      const allAccounts = await window.DB.getAll('accounts');
      const allUsers = await window.DB.getAll('users');
      let custody_account_id = document.getElementById('user-custody')`;
code = code.replace(target, injection);

fs.writeFileSync('d:/Easy ERP/save_fix.js', code, 'utf8');
console.log("Patched save_fix.js");
