"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const ignoredElements = ["SCRIPT", "STYLE", "TEMPLATE"];
class HtmlDeserializer {
  constructor(createDefaultElement, plugins) {
    this.createDefaultElement = createDefaultElement;
    this.plugins = plugins;
  }
  registerPlugin(plugin, prepend = true) {
    prepend ? this.plugins.unshift(plugin) : this.plugins.push(plugin);
  }
  processNodeListPaste(nodeList, cumulativeTextAttrs) {
    for (const plugin of this.plugins) {
      const result = plugin.processNodeListPaste?.({ nodeList, cumulativeTextAttrs, deserializer: this }) ?? null;
      if (result !== null) {
        return result;
      }
    }
    const processed = [];
    for (const childNode of nodeList) {
      const isWhiteSpace = childNode.nodeType === Node.TEXT_NODE && childNode.textContent?.match(/^\s*$/) !== null;
      const attrs = this.processWithAttributeProcessor(childNode, cumulativeTextAttrs);
      const result = childNode instanceof HTMLElement ? this.processBlockPaste(
        childNode,
        cumulativeTextAttrs
      ) : null;
      if (result !== null) {
        processed.push(...(Array.isArray(result) ? result : [result]).map((element) => ({ element })));
      } else {
        const result2 = this.deserializeTextNode(childNode, cumulativeTextAttrs);
        if (result2 !== null) {
          processed.push(...result2.map((text) => ({ text, isWhiteSpace })));
        } else {
          const deserializedChildren = this.processNodeListPaste(
            Array.from(childNode.childNodes),
            attrs
          );
          processed.push(
            ...deserializedChildren === null ? [] : deserializedChildren.texts !== void 0 ? deserializedChildren.texts.map((text) => ({ text, isWhiteSpace })) : deserializedChildren.elements.map((element) => ({ element }))
          );
        }
      }
    }
    if (processed.length === 0) {
      return null;
    }
    const containsBlock = processed.find(({ element }) => element !== void 0) !== void 0;
    if (containsBlock) {
      return {
        elements: processed.flatMap((item) => {
          if (item.text !== void 0) {
            return item.isWhiteSpace ? [] : [this.createDefaultElement([item.text])];
          } else if (item.element !== void 0) {
            return [item.element];
          }
          {
            return [];
          }
        })
      };
    } else {
      return { texts: processed.map((item) => item.text) };
    }
  }
  deserializeInline(list, cumulativeTextAttrs) {
    return Array.from(list).flatMap((childNode) => {
      const result = this.deserializeTextNode(childNode, cumulativeTextAttrs);
      if (result !== null) {
        return result;
      } else {
        const attrs = this.processWithAttributeProcessor(childNode, cumulativeTextAttrs);
        return this.deserializeInline(childNode.childNodes, attrs);
      }
    });
  }
  deserializeBlocks(list, cumulativeTextAttrs) {
    const result = this.processNodeListPaste(list, cumulativeTextAttrs);
    return result?.texts ?? result?.elements ?? [];
  }
  deserializeTextNode(node, cumulativeTextAttrs) {
    if (node.nodeType === Node.TEXT_NODE) {
      const text = node.textContent ?? "";
      return [{ ...cumulativeTextAttrs, text: text.replace(/[ \t]*(?:\r?\n[ \t]*)+/g, " ") }];
    } else if (node instanceof HTMLElement) {
      const result = this.processInlinePaste(
        node,
        cumulativeTextAttrs
      );
      if (result !== null) {
        return Array.isArray(result) ? result : [result];
      }
    }
    return null;
  }
  processInlinePaste(element, cumulativeTextAttrs) {
    if (ignoredElements.includes(element.tagName)) {
      return [];
    }
    if (element.tagName === "BR") {
      return { ...cumulativeTextAttrs, text: " " };
    }
    const next = (list, cta) => {
      const attrs = this.processWithAttributeProcessor(element, cta);
      return this.deserializeInline(list, attrs);
    };
    for (const plugin of this.plugins) {
      const result = plugin.processInlinePaste?.({ element, next, cumulativeTextAttrs, deserializer: this }) ?? null;
      if (result !== null) {
        return result;
      }
    }
    return null;
  }
  processWithAttributeProcessor(element, cumulativeTextAttrs) {
    if (!(element instanceof HTMLElement)) {
      return {};
    }
    return this.plugins.reduce(
      (cta, plugin) => plugin.processAttributesPaste?.({ element, cumulativeTextAttrs, deserializer: this }) ?? cta,
      cumulativeTextAttrs
    );
  }
  processBlockPaste(element, cumulativeTextAttrs) {
    if (ignoredElements.includes(element.tagName)) {
      return [];
    }
    const next = (list, cta) => this.deserializeBlocks(Array.from(list), { ...cumulativeTextAttrs, ...cta });
    for (const plugin of this.plugins) {
      const result = plugin.processBlockPaste?.({ element, next, cumulativeTextAttrs, deserializer: this }) ?? null;
      if (result !== null) {
        return result;
      }
    }
    return null;
  }
}
exports.HtmlDeserializer = HtmlDeserializer;
//# sourceMappingURL=HtmlDeserializer.cjs.map
