const fs = require('fs');
const path = 'd:/Sites/Work/RadioCast/src/main.js';
const s = fs.readFileSync(path,'utf8');
const lines = s.split(/\r?\n/);
let balance = 0;
let maxBalance = 0;
let maxLine = -1;
for (let i=0;i<lines.length;i++){
  const line = lines[i];
  for (const ch of line){
    if (ch==='{' ) balance++;
    else if (ch==='}') balance--;
  }
  if (balance>maxBalance){ maxBalance = balance; maxLine = i+1; }
}
console.log('final balance:', balance, 'maxBalance:', maxBalance, 'maxLine:', maxLine);
console.log('last 40 lines:');
for (let i=Math.max(0, lines.length-40); i<lines.length;i++) console.log((i+1)+': '+lines[i]);

// Print context around maxLine
if (maxLine>0){
  console.log('\nContext around max imbalance at line', maxLine);
  for (let i=Math.max(1, maxLine-5); i<=Math.min(lines.length, maxLine+5); i++) console.log(i+': '+lines[i-1]);
}
