const fs = require('fs');

let appFinal = fs.readFileSync('d:/برنامج مخازن/app_final.js', 'utf8');

const regex = /if\s*\(role\s*===\s*'موظف مخصص'\)\s*\{([\s\S]*?allowed_projects\.push\(parseInt\(cb\.value\)\)\);)\s*\}/;

const newExtract = `      
      document.querySelectorAll('.perm-module:checked').forEach(cb => allowed_modules.push(cb.value));
      document.querySelectorAll('.perm-warehouse:checked').forEach(cb => allowed_warehouses.push(parseInt(cb.value)));
      document.querySelectorAll('.perm-project:checked').forEach(cb => allowed_projects.push(parseInt(cb.value)));
`;

if (regex.test(appFinal)) {
    appFinal = appFinal.replace(regex, newExtract);
    fs.writeFileSync('d:/برنامج مخازن/app_final.js', appFinal, 'utf8');
    console.log('Fixed app_final.js permissions extraction.');
} else {
    console.log('Could not find the role check in app_final.js.');
}
