import { cleanObject } from '../../../../../../utils/object';
import { RTETextNode } from '../../../../dto/nodes/text-node';
import { ElementToTextNodeStrategy } from './element-to-text-node-strategy';
/**
 * Class to handle plain text nodes and convert them to RTE text node DTOs
 */
export class PlainTextToTextNodeStrategy extends ElementToTextNodeStrategy {
  /**
   * @inheritDoc
   */
  canHandle(elementsArray, currentIndex) {
    const element = elementsArray[currentIndex];
    return element.nodeType === Node.TEXT_NODE ? 1 : 0;
  }
  /**
   * @inheritDoc
   */
  handle(elementsArray, options, parentTextAnnotations) {
    const parentAnnotations = Object.assign({}, parentTextAnnotations);
    const disableAutoMatchFormat = !!(options && options.autoMatchFormat === false);
    const child = elementsArray[0];
    const parentNodeName = child.parentNode.nodeName.toLowerCase();
    const plainText = disableAutoMatchFormat && !RTETextNode.supportsHtmlTag(parentNodeName);
    const textNode = this.generateTextNode(String(child.textContent), disableAutoMatchFormat
      ? cleanObject(Object.assign(Object.assign({}, (parentNodeName === 'span'
        ? parentAnnotations
        : {
          // text node could be wrapped in <a> tag, so we need to keep the parent link annotation
          link: parentAnnotations.link,
          code: parentNodeName === 'code' ? parentAnnotations.code : undefined,
        })), { inlineHtml: parentAnnotations.inlineHtml, plainText: plainText || undefined }))
      : parentAnnotations);
    return Object.assign({}, textNode);
  }
}
