/**
 * HTML → plain text cleaning for SEC filings.
 * Used by both paragraph extraction (fast-reparse) and DAPT corpus preparation.
 */

/** Strip HTML tags, decode entities, fix word-boundary artifacts from SEC EDGAR HTML. */
export function stripHtml(html: string): string {
  return html
    .replace(/<script[\s\S]*?<\/script>/gi, "")
    .replace(/<style[\s\S]*?<\/style>/gi, "")
    .replace(/<noscript[\s\S]*?<\/noscript>/gi, "")
    // Collapse adjacent inline element boundaries to prevent word splitting
    .replace(/<\/(span|a|b|i|u|em|strong|font)>(\s*)<(?:span|a|b|i|u|em|strong|font)[^>]*>/gi, (_m, _tag, ws) => ws.length > 0 ? " " : "")
    .replace(/<\/ix:[a-z]+>(\s*)<ix:[a-z]+[^>]*>/gi, (_m, ws) => ws.length > 0 ? " " : "")
    .replace(/<\/(p|div|tr|li|h[1-6]|td|th)>/gi, "\n")
    .replace(/<(br|hr)\s*\/?>/gi, "\n")
    .replace(/<[^>]+>/g, " ")
    .replace(/&nbsp;|&#160;|&#xa0;/gi, " ")
    .replace(/&amp;/g, "&")
    .replace(/&lt;/g, "<")
    .replace(/&gt;/g, ">")
    .replace(/&quot;|&ldquo;|&rdquo;|&#8220;|&#8221;|&#147;|&#148;/g, '"')
    .replace(/&#39;|&apos;|&rsquo;|&lsquo;|&#8216;|&#8217;|&#146;/g, "'")
    .replace(/&mdash;|&#8212;|&#151;/g, "—")
    .replace(/&ndash;|&#8211;|&#150;/g, "–")
    .replace(/&bull;|&#8226;|&#149;/g, "•")
    .replace(/&minus;|&#8722;/g, "-")
    .replace(/&sect;|&#167;/g, "§")
    .replace(/&#153;/g, "™")
    .replace(/&#x([0-9a-fA-F]+);/gi, (_, hex) => String.fromCodePoint(parseInt(hex, 16)))
    .replace(/&#\d+;/g, " ")
    .replace(/&\w+;/g, " ")
    .replace(/[^\S\n]+/g, " ")
    .replace(/([a-z])\.([A-Z])/g, "$1. $2")
    .replace(/([a-z]),([A-Z])/g, "$1, $2")
    .replace(/([a-z]);([A-Z])/g, "$1; $2")
    .replace(/•([A-Za-z])/g, "• $1")
    .replace(/\b([a-z])\.([A-Z])/g, "$1. $2")
    // Greek question mark (U+037E) → semicolon
    .replace(/\u037e/g, ";")
    // Fix inline element joins that created camelCase with common English words
    .replace(/([a-z])(The|Our|We|This|These|That|Its|His|Her|In|As|For|And|Or|If|An|It|To|By|On|At|No|Of|All|Any|Has|Was|Is|Are|Not|May|Can|Will|Such|Also|But|Each|New|So|Up|With|From)\b/g, "$1 $2")
    // Fix colon-joins: word:Word → word: Word (exclude URLs)
    .replace(/([a-z]):([A-Z])/g, "$1: $2")
    // Fix ISO standard joins: ISO/IEC27001 → ISO/IEC 27001, ISO27001 → ISO 27001
    .replace(/\b(ISO(?:\/IEC)?)(\d)/g, "$1 $2")
    .replace(/(Standardization)(\d)/g, "$1 $2")
    // Fix PDF extraction artifact: space before punctuation ("Director ," → "Director,")
    .replace(/ ([,;:.!?)])/g, "$1");
}
