export function fixGarbledUtf8(str: string): string {
  try {
    const bytes = new Uint8Array(str.length);
    for (let i = 0; i < str.length; i++) {
      bytes[i] = str.charCodeAt(i) & 0xFF;
    }
    return new TextDecoder('utf-8').decode(bytes);
  } catch {
    return str;
  }
}

export function normalizeStoryText(rawHtml: string): string {
  const garbled = /Ã|Â|à¤/.test(rawHtml);
  return garbled ? fixGarbledUtf8(rawHtml) : rawHtml;
}