// Script to validate all code 355 JavaScript commands in CommonEvents.json
const fs = require('fs');

console.log('Loading CommonEvents.json...');
const data = JSON.parse(fs.readFileSync('./data/CommonEvents.json', 'utf8'));

let errorCount = 0;
let totalScripts = 0;

console.log('Validating script calls...\n');

data.forEach((event, eventIndex) => {
    if (!event || !event.list) return;
    
    event.list.forEach((command, cmdIndex) => {
        if (command.code === 355 && command.parameters && command.parameters[0]) {
            totalScripts++;
            const script = command.parameters[0];
            
            try {
                // Try to parse the script as a Function to check syntax
                new Function(script);
            } catch (e) {
                errorCount++;
                console.log(`ERROR in Event ${event.id} "${event.name}" (Command index ${cmdIndex}):`);
                console.log(`Script: ${script.substring(0, 100)}...`);
                console.log(`Error: ${e.message}\n`);
            }
        }
    });
});

console.log(`\n=== VALIDATION COMPLETE ===`);
console.log(`Total scripts checked: ${totalScripts}`);
console.log(`Errors found: ${errorCount}`);

if (errorCount === 0) {
    console.log('\nAll scripts are syntactically valid!');
} else {
    console.log('\nPlease fix the errors above.');
}
