const fs = require('fs');
const path = require('path');
const vm = require('vm');

const htmlPath = path.join(__dirname, 'app.html');
if (!fs.existsSync(htmlPath)) {
    console.error("app.html not found!");
    process.exit(1);
}

const html = fs.readFileSync(htmlPath, 'utf8');
const scriptRegex = /<script\b[^>]*>([\s\S]*?)<\/script>/gi;
let match;
let scriptCount = 0;

while ((match = scriptRegex.exec(html)) !== null) {
    const code = match[1];
    scriptCount++;
    if (code.trim().length === 0) continue;
    
    try {
        new vm.Script(code);
        console.log(`Script block ${scriptCount} compiled successfully.`);
    } catch (e) {
        console.error(`Syntax error in script block ${scriptCount}:`, e.message);
        
        // Find line number in app.html
        const index = match.index + match[0].indexOf(match[1]);
        const before = html.substring(0, index);
        const lineNumber = before.split('\n').length;
        console.error(`Approximate line number in app.html: ${lineNumber}`);
        
        console.log("FIRST 5 LINES:");
        console.log(code.split('\n').slice(0, 10).join('\n'));
        console.log("LAST 5 LINES:");
        console.log(code.split('\n').slice(-10).join('\n'));
    }
}
