var __rest = (this && this.__rest) || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
    t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
import { generateUUID } from '../../../../utils/misc';
import { cleanObject } from '../../../../utils/object';
import { RTETextNode } from '../../dto/nodes/text-node';
import htmlElementToAnnotationGenerator from '../../slimmers/controllers/helper/element-to-dto-strategy/annotation-handler/html-element-to-annotation-generator';
import { ElementToDTOStrategy } from '../../slimmers/controllers/helper/element-to-dto-strategy/element-to-dto-strategy.abstract';
/**
 * Strategy to convert <span class="token" token-key="...">...</span> to a token node
 */
export class SpanToTokenNodeStrategy extends ElementToDTOStrategy {
  /**
   * @inheritDoc
   */
  canHandle(elementsArray, currentIndex) {
    const element = elementsArray[currentIndex];
    if (element.nodeName === 'SPAN' &&
      element.classList.contains('token') &&
      element.hasAttribute('token-key')) {
      return 1;
    }
    return 0;
  }
  /**
   * @inheritDoc
   */
  handle(elementsArray) {
    const element = elementsArray[0];
    const tokenKey = element.getAttribute('token-key');
    const text = element.textContent;
    const annotations = htmlElementToAnnotationGenerator.generate(RTETextNode, element);
    // Exclude customAttribute and customClass from annotations
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const { customAttribute, customClass } = annotations, restAnnotations = __rest(annotations, ["customAttribute", "customClass"]);
    return {
      id: generateUUID(),
      type: 'token',
      token: tokenKey,
      text,
      annotations: Object.assign(Object.assign({}, restAnnotations), { customAttribute: this.createCustomAttrs(customAttribute) }),
    };
  }
  /**
   * @inheritDoc
   */
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  appendChildBlocks() { }
  /**
   * @inheritDoc
   */
  shouldProceedToElementChildren() {
    return false;
  }
  /**
   * Creates custom attributes record from the given custom attribute object, clean some fixed attributes.
   *
   * @param {Record<string, string>} customAttribute - The custom attribute object
   * @returns {Record<string, string>} The processed custom attributes
   */
  createCustomAttrs(customAttribute) {
    const result = Object.assign(Object.assign({}, customAttribute), { 'contenteditable': null, 'template-token-key': null, 'token-key': null, 'wrapped': customAttribute.wrapped === 'false' ? 'false' : 'true' });
    return cleanObject(result);
  }
}
