const fs = require('fs');
const content = fs.readFileSync('dist/assets/index-D9sWWWv_.js', 'utf8');

// Find where Xe is assigned. In react, it is often:
// const Xe = ...
// or let Xe = ...
// or just Xe = ...
// Let's look at the surrounding code of Du and Laporan Penjualan.
// Let's search backwards from index 987484. We can look for keywords like "filter" or "map" or "reduce" or "Date".
const target = 987484;
const slice = content.substring(target - 20000, target);
console.log("Analyzing 20000 characters before target...");

// Find all matches of something like: = ... .filter( or = ... .reduce(
// let's do some regex matching in the slice to find calculations of stats
const regexes = [
  /const\s+[a-zA-Z0-9_$]+\s*=\s*k\s*\.\s*filter/g,
  /[a-zA-Z0-9_$]+\s*=\s*k\s*\.\s*filter/g,
  /\.filter/g,
  /\.reduce/g
];

regexes.forEach(re => {
  let match;
  console.log(`\n--- Matches for ${re.toString()} ---`);
  while ((match = re.exec(slice)) !== null) {
    const idx = match.index;
    console.log(`Match at index ${idx}: ${slice.substring(idx - 30, idx + 100)}`);
  }
});
