import fs from 'fs';

const OPEN_HDR = /<div\s+class\s*=\s*"[^"]*\bmain-panel\b[^"]*"\s*>/i;
const OPEN_CONTENT = /<div\s+class\s*=\s*"[^"]*\bcontent\b[^"]*"\s*>/i;
const OPEN_PAGE_INNER = /<div\s+class\s*=\s*"[^"]*\bpage-inner\b[^"]*"\s*>/i;
const MARKERS = ["@section('content')", '@section("content")'];

function secIdx(t) {
  let earliest = null;
  for (const m of MARKERS) {
    const i = t.indexOf(m);
    if (i === -1) continue;
    const nl = t.indexOf('\n', i);
    if (nl === -1) continue;
    const nxt = nl + 1;
    earliest = earliest === null ? nxt : Math.min(earliest, nxt);
  }
  return earliest;
}

const raw = fs.readFileSync('resources/views/admin/Withdrawals/mwithdrawals.blade.php', 'utf8');
const sec = secIdx(raw);
const sub = raw.slice(sec);
console.log('sec', sec);
const hdrMatch = sub.match(OPEN_HDR);
if (!hdrMatch) {
  console.log('no hdr');
  process.exit(0);
}
console.log('hdr match', hdrMatch[0]);
const afterShell = sub.slice(hdrMatch.index + hdrMatch[0].length);
const cMatch = afterShell.match(OPEN_CONTENT);
console.log('content match', cMatch?.[0], 'idx', cMatch?.index);
if (!cMatch) process.exit(0);
const afterContent = afterShell.slice(cMatch.index + cMatch[0].length);
const pMatch = afterContent.match(OPEN_PAGE_INNER);
console.log('page-inner', pMatch?.[0]);
