#!/usr/bin/env node
const fs = require('fs');
const path = require('path');

const ABBR = (process.env.EN_ABBR || 'WEB').toUpperCase();
const ROOT = process.env.OUTPUT_DIR || path.join('data','en_bible', ABBR);

function cleanText(s){
  return s
    // remove \+w/\w wrappers and closing tags
    .replace(/\\\+?w\s+/gi,'')
    .replace(/\|strong="[^"]*"/gi,'')
    .replace(/\\\+?w\*/gi,'')
    // remove footnotes / cross-refs blocks
    .replace(/\\f\s+.*?\\f\*/gis,' ')
    .replace(/\\x\s+.*?\\x\*/gis,' ')
    // remove +wh blocks and similar wrappers
    .replace(/\\\+wh\s+.*?\\\+wh\*/gis,' ')
    // remove inline verse-note blocks like "+ 1:1 ... *"
    .replace(/\+\s*\d+:\d+.*?\*/g,' ')
    // remove stray asterisks left after stripping tags
    .replace(/\*/g,'')
    // remove any other inline tags like \\qs, \\add, etc.
    .replace(/\\[a-z0-9-]+\s*/gi,' ')
    .replace(/\s+/g,' ').trim();
}

function processFile(file){
  const p = path.join(ROOT, file);
  if(!fs.existsSync(p)){
    console.error('Missing', p);
    process.exit(1);
  }
  const j = JSON.parse(fs.readFileSync(p,'utf-8'));
  for(const b of j.books||[]){
    for(const c of b.chapters||[]){
      for(const v of c.verses||[]){
        if(v.text) v.text = cleanText(String(v.text));
      }
    }
  }
  fs.writeFileSync(p, JSON.stringify(j,null,2),'utf-8');
  console.log('Cleaned', p);
}

processFile('old_testament.json');
processFile('new_testament.json');
