import DOMPurify from "isomorphic-dompurify";
import { FOLLOW_UP_BODY_SANITIZE_CONFIG } from "@/modules/survey/follow-ups/lib/sanitize-follow-up-body";
import { SURVEY_IMPORT_MAX_TEXT_CHARS, SURVEY_IMPORT_MAX_TEXT_TAGS } from "../limits";
import type { TSurveyImportIssueCode } from "../types";
import { createSlicer } from "./event-loop";
import { hasMarkup } from "./markup";
import type { TSurveyImportPipelineIssue } from "./report";

/**
 * Text sanitizing for every import lane (ENG-3607): what a file says is HTML from outside, whatever
 * the file.
 *
 * - Headlines and the ending message keep rich text, through the follow-up email allowlist
 *   (`FOLLOW_UP_BODY_SANITIZE_CONFIG`, unchanged). Accepted trade-offs: https links survive, `class`
 *   survives, underline is dropped, and `<div>` lines run together.
 * - Every other text becomes plain text, then is parsed once more: `textContent` decodes
 *   `&lt;img onerror…&gt;` into markup, and survey-ui's `Label` renders any string that parses as
 *   HTML as HTML. A text that parses into elements gets its `<` replaced, so it shows as typed.
 *
 * DOMPurify is the parser throughout; nothing is stripped with a regex. What it removed is read from
 * the default export's `DOMPurify.removed` right after each call — the named `removed` export is a
 * Proxy that breaks array methods.
 *
 * `FORCE_BODY` is set on every call: without it the HTML parser moves a leading `<script>` or
 * `<style>` into the head, where DOMPurify neither sees nor reports it. It changes parsing, not the
 * allowlist.
 */

/**
 * How a text is sanitized. `rich` keeps the follow-up email allowlist (headlines and the ending
 * message, ENG-3607); `plain` keeps the text only (choices, answers, names).
 */
export type TSurveyImportTextFormat = "rich" | "plain";

const RICH_CONFIG = {
  ...FOLLOW_UP_BODY_SANITIZE_CONFIG,
  FORCE_BODY: true,
  RETURN_DOM_FRAGMENT: true as const,
};
const PLAIN_CONFIG = { ALLOWED_TAGS: [] as string[], FORCE_BODY: true, RETURN_DOM_FRAGMENT: true as const };

/** Stands in for `<` in plain text that would otherwise parse as markup. */
const NEUTRAL_LESS_THAN = "\uFF1C";

/** The allowed tags that stand on their own line; every other allowed tag is inline. */
const BLOCK_TAGS = new Set(["p", "ul", "ol", "li"]);

/** Removed elements that say nothing about the text: the parser's wrappers and plain structure. */
const STRUCTURAL_TAGS = new Set(["body", "html", "head", "remove", "div", "p", "br", "span"]);
const MEDIA_TAGS = new Set([
  "img",
  "image",
  "picture",
  "svg",
  "video",
  "audio",
  "source",
  "track",
  "canvas",
  "map",
  "area",
]);
const ACTIVE_TAGS = new Set([
  "script",
  "iframe",
  "frame",
  "frameset",
  "object",
  "embed",
  "applet",
  "noscript",
  "link",
  "meta",
  "base",
  "form",
  "input",
  "button",
  "textarea",
  "select",
  "template",
  "math",
]);
const STYLE_ATTRIBUTES = new Set(["style", "color", "bgcolor", "face", "size", "align", "class"]);
const ACTIVE_URL = /^\s*(?:javascript|vbscript|data):/i;

type TDroppedCode = Extract<
  TSurveyImportIssueCode,
  "image_dropped" | "script_dropped" | "formatting_dropped"
>;

interface TSanitizedText {
  text: string;
  /** The text without markup, for the prompt. Equal to `text` for plain text. */
  plain: string;
  dropped: Set<TDroppedCode>;
  escaped: boolean;
  tooLong: boolean;
}

const elementName = (node: unknown): string =>
  typeof node === "object" && node !== null && "nodeName" in node && typeof node.nodeName === "string"
    ? node.nodeName.toLowerCase()
    : "";

const hasStyleAttribute = (node: unknown): boolean =>
  typeof node === "object" &&
  node !== null &&
  "hasAttribute" in node &&
  typeof node.hasAttribute === "function" &&
  Boolean(node.hasAttribute("style"));

/** The report code for an element DOMPurify removed, or `null` for structure (`body`, `div`, …). */
function droppedElementCode(element: unknown): TDroppedCode | null {
  const tag = elementName(element);
  if (MEDIA_TAGS.has(tag)) return "image_dropped";
  if (ACTIVE_TAGS.has(tag)) return "script_dropped";
  if (tag === "style" || hasStyleAttribute(element)) return "formatting_dropped";
  return tag !== "" && !STRUCTURAL_TAGS.has(tag) ? "formatting_dropped" : null;
}

/** The report code for an attribute DOMPurify removed, or `null` for one not worth a line. */
function droppedAttributeCode(attribute: { name: string; value: string }): TDroppedCode | null {
  const name = attribute.name.toLowerCase();
  if (name.startsWith("on") || (name === "href" && ACTIVE_URL.test(attribute.value))) return "script_dropped";
  return STYLE_ATTRIBUTES.has(name) ? "formatting_dropped" : null;
}

/** What the last DOMPurify call removed, as report codes. */
function readDropped(into: Set<TDroppedCode>): void {
  for (const entry of DOMPurify.removed) {
    let code: TDroppedCode | null = null;
    if ("element" in entry) code = droppedElementCode(entry.element);
    else if (entry.attribute) code = droppedAttributeCode(entry.attribute);
    if (code) into.add(code);
  }
}

const collapseWhitespace = (text: string): string => text.replaceAll(/\s+/g, " ").trim();

/** The `<` in a text, counted up to just past `limit`. */
const countLessThan = (text: string, limit: number): number => {
  let count = 0;
  for (let index = text.indexOf("<"); index !== -1 && count <= limit; index = text.indexOf("<", index + 1)) {
    count += 1;
  }
  return count;
};

/**
 * A `<` written as a character reference: `&lt;`, `&#60;`, `&#x3c;`, with or without the semicolon, any
 * case and any zero padding (`&nvlt;` decodes to `<` too). `textContent` decodes each into a `<` that the
 * next parse reads as a tag. Counting a few that are not `<` (`&LT;` is) only errs towards the cap.
 */
const ENCODED_LESS_THAN = /&(?:nv)?lt|&#0*60(?!\d)|&#x0*3c(?![\da-f])/gi;

/**
 * The tags a text can open in any parse: its own `<`, plus every `<` it holds as a character reference,
 * which decoding turns into tags one parse later. Counted up to just past `limit`.
 */
const countTags = (text: string, limit: number): number => {
  let count = countLessThan(text, limit);
  if (count > limit || !text.includes("&")) return count;
  for (const _match of text.matchAll(ENCODED_LESS_THAN)) {
    count += 1;
    if (count > limit) break;
  }
  return count;
};

/**
 * Whether plain text parses into HTML elements. `textContent` decodes entities, so `&lt;b&gt;` in the
 * file is `<b>` here — markup again, one parse later.
 *
 * A text with more `<` than a text may hold tags is markup without being parsed: a parse costs about
 * linear time in its tags, and thousands of nested ones (`&lt;i>` repeated, decoded) block the event
 * loop for seconds before DOMPurify throws a `RangeError`. `sanitizeText` refuses such a text first by
 * counting its encoded `<`; this holds for any text that reaches here by another decode.
 */
export function containsMarkup(text: string): boolean {
  if (!text.includes("<")) return false;
  if (countLessThan(text, SURVEY_IMPORT_MAX_TEXT_TAGS) > SURVEY_IMPORT_MAX_TEXT_TAGS) return true;
  DOMPurify.sanitize(text, PLAIN_CONFIG);
  return DOMPurify.removed.some(
    (entry) => "element" in entry && !["body", "html", "head", "remove"].includes(elementName(entry.element))
  );
}

/** Plain text that renders as typed: `<` replaced where the text would parse as markup. */
export function neutralize(text: string): { text: string; escaped: boolean } {
  return containsMarkup(text)
    ? { text: text.replaceAll("<", NEUTRAL_LESS_THAN), escaped: true }
    : { text, escaped: false };
}

function sanitizePlain(raw: string, dropped: Set<TDroppedCode>): { text: string; escaped: boolean } {
  if (!hasMarkup(raw)) return { text: collapseWhitespace(raw), escaped: false };

  const fragment = DOMPurify.sanitize(raw, PLAIN_CONFIG);
  readDropped(dropped);
  return neutralize(collapseWhitespace(fragment.textContent ?? ""));
}

function sanitizeRich(
  raw: string,
  dropped: Set<TDroppedCode>
): { text: string; plain: string; escaped: boolean } {
  if (!hasMarkup(raw)) {
    const text = collapseWhitespace(raw);
    return { text, plain: text, escaped: false };
  }

  const fragment = DOMPurify.sanitize(raw, RICH_CONFIG);
  readDropped(dropped);
  const plain = collapseWhitespace(fragment.textContent ?? "");

  if (fragment.querySelector("*") === null) {
    // No markup left. Stored as the decoded text: kept as HTML, `&amp;` would show literally, because
    // `Label` only renders a string as HTML when it parses into elements.
    const neutral = neutralize(plain);
    return { text: neutral.text, plain: neutral.text, escaped: neutral.escaped };
  }

  wrapInlineRuns(fragment);
  const holder = fragment.ownerDocument.createElement("div");
  holder.append(fragment);
  return { text: holder.innerHTML.trim(), plain, escaped: false };
}

/**
 * Wraps each run of top-level inline content in a `<p>`, as the editor stores text. `Label` lays an
 * HTML headline's top-level nodes out as a column, so `Read <a>our terms</a> or <a>this</a>.` left
 * bare would show each piece on its own line.
 */
function wrapInlineRuns(fragment: DocumentFragment): void {
  let paragraph: HTMLParagraphElement | null = null;
  for (const node of Array.from(fragment.childNodes)) {
    if (node.nodeType === node.ELEMENT_NODE && BLOCK_TAGS.has(node.nodeName.toLowerCase())) {
      paragraph = null;
      continue;
    }
    // Whitespace between blocks is layout, not a line of its own.
    if (!paragraph && node.nodeType === node.TEXT_NODE && !node.textContent?.trim()) continue;
    if (!paragraph) {
      paragraph = fragment.ownerDocument.createElement("p");
      node.before(paragraph);
    }
    paragraph.append(node);
  }
}

/** Sanitize one text. Synchronous; callers that sanitize many texts yield between them. */
export function sanitizeText(raw: string, format: TSurveyImportTextFormat): TSanitizedText {
  const dropped = new Set<TDroppedCode>();
  if (
    raw.length > SURVEY_IMPORT_MAX_TEXT_CHARS ||
    countTags(raw, SURVEY_IMPORT_MAX_TEXT_TAGS) > SURVEY_IMPORT_MAX_TEXT_TAGS
  ) {
    return { text: "", plain: "", dropped, escaped: false, tooLong: true };
  }

  if (format === "rich") {
    const result = sanitizeRich(raw, dropped);
    return { ...result, dropped, tooLong: false };
  }

  const result = sanitizePlain(raw, dropped);
  return { text: result.text, plain: result.text, dropped, escaped: result.escaped, tooLong: false };
}

/**
 * A text read from the file, before it is sanitized, keyed by a key the lane's reader assigned —
 * never by an id from the file.
 */
export interface TSurveyImportText {
  format: TSurveyImportTextFormat;
  /** The source question it belongs to, for report lines. */
  questionRef: string | null;
  /** Raw text by normalized language code. The default language is always present (maybe empty). */
  byLanguage: Map<string, string>;
}

/** What `sanitizeImportTexts` reads: a lane's texts, and how to name what they belong to. */
export interface TSanitizeImportTextsSource {
  texts: ReadonlyMap<string, TSurveyImportText>;
  defaultLanguage: string;
  /** The key of the survey's end message, whose report lines name the ending rather than a question. */
  endingKey: string | null;
  /** The label the file gives a source question (`questionTag` on its report lines), if any. */
  questionLabel: (questionRef: string) => string | undefined;
}

export interface TSanitizedTexts {
  /** The text to store, by text key and language. */
  byKey: Map<string, Map<string, string>>;
  /** The default language's text without markup, by text key, for the prompt. */
  plainDefault: Map<string, string>;
  issues: TSurveyImportPipelineIssue[];
}

/**
 * The report lines sanitizing writes: formatting once per survey, everything else once per question
 * and code.
 */
class SanitizeReporter {
  readonly issues: TSurveyImportPipelineIssue[] = [];
  private readonly reported = new Set<string>();

  constructor(private readonly questionLabel: TSanitizeImportTextsSource["questionLabel"]) {}

  /** `isEnding`: the text is the end message, whose lines name the ending rather than a question. */
  add(result: TSanitizedText, questionRef: string | null, isEnding = false): void {
    for (const code of result.dropped) this.report(code, questionRef, isEnding);
    if (result.escaped) this.report("markup_escaped", questionRef, isEnding);
    if (result.tooLong) this.report("text_too_long", questionRef, isEnding);
  }

  private report(code: TSurveyImportIssueCode, questionRef: string | null, isEnding: boolean): void {
    const perSurvey = code === "formatting_dropped";
    // What the line is about, so each question, and the ending, gets one line per code.
    let subject = questionRef ?? "";
    if (questionRef === null && isEnding) subject = "ending";
    const id = `${code}\u0000${perSurvey ? "" : subject}`;
    if (this.reported.has(id)) return;
    this.reported.add(id);
    const label = questionRef === null ? undefined : this.questionLabel(questionRef);
    this.issues.push({
      code,
      severity: perSurvey ? "info" : "warning",
      ...(label && !perSurvey ? { questionTag: label } : {}),
      ...(questionRef !== null && !perSurvey ? { questionRef } : {}),
      ...(isEnding && !perSurvey ? { params: { subject: "ending" } } : {}),
    });
  }
}

/**
 * Sanitize every text of the survey. Yields to the event loop every `SURVEY_IMPORT_SLICE_MS` (by
 * elapsed time, not by count: one long text can cost more than a hundred short ones) and stops when
 * `signal` aborts.
 */
export async function sanitizeImportTexts(
  source: TSanitizeImportTextsSource,
  signal: AbortSignal
): Promise<TSanitizedTexts> {
  const byKey = new Map<string, Map<string, string>>();
  const plainDefault = new Map<string, string>();
  const reporter = new SanitizeReporter(source.questionLabel);

  const slice = createSlicer(signal);
  for (const [key, entry] of source.texts) {
    const sanitized = new Map<string, string>();
    for (const [language, raw] of entry.byLanguage) {
      // Yields by elapsed time, by design: the point of the loop is to give the event loop back.
      await slice(); // NOSONAR(typescript:S9382) -- a deliberate yield every slice

      const result = sanitizeText(raw, entry.format);
      sanitized.set(language, result.text);
      if (language === source.defaultLanguage) plainDefault.set(key, result.plain);
      reporter.add(result, entry.questionRef, key === source.endingKey);
    }
    byKey.set(key, sanitized);
  }

  return { byKey, plainDefault, issues: reporter.issues };
}

/** Plain text for a name from the file (the survey's, a question label used as a headline). */
export function sanitizeName(raw: string): string {
  return sanitizeText(raw, "plain").text;
}
