var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { CodeLanguage } from '../../types';
const presetLanguages = Object.values(CodeLanguage);
/**
 * Processing the code content into different types of nodes.
 */
export class CodeHighlightParser extends VegaSlimmer {
  /**
   * Split the HTML tag to avoid the content render to a html element.
   *
   * @example <div> -> ['<', 'div', '>']
   * @param {string} text - The code content.
   * @returns {string[]} - The code content array.
   */
  splitTag(text) {
    if (!this.hasSpecialCharacter(text) || text.length < 2) {
      return [text];
    }
    else {
      const result = [];
      const textArray = text.split('');
      let nextIndex = 0;
      textArray.forEach((item) => {
        if (item === '<' || item === '>' || item === '/') {
          result.push(item);
          nextIndex = result.length;
        }
        else {
          result[nextIndex] = result[nextIndex] ? result[nextIndex] + item : item;
        }
      });
      return result;
    }
  }
  /**
   * Check the current language is a preset language and the language is not plain text.
   *
   * @param {Nullable<string>} language - The input language.
   * @returns {boolean} - The boolean result.
   */
  isHighlightLanguage(language) {
    return language !== 'plainText' && presetLanguages.includes(language);
  }
  /**
   * Mark the line number class for the first node of each line.
   *
   * @param {CodeNodeType[]} codeNodes - The code nodes array.
   * @returns {CodeNodeType[]} - The code node array with line number tag.
   */
  markLineNumberNodes(codeNodes) {
    if (codeNodes.length === 0) {
      codeNodes.push({ text: '\n', key: '0', className: '' });
    }
    let lineNumber = 0;
    return codeNodes.map((node, index) => {
      let className = node.className;
      if (index === 0 || codeNodes[index - 1].text === '\n') {
        lineNumber++;
        className = `v-code-line-number ${node.className}`;
      }
      return {
        text: node.text,
        key: `${index}`,
        className,
        mark: `${lineNumber}`,
      };
    });
  }
  /**
   * Check the text has special character.
   *
   * @param {string} text - The test text.
   * @returns {boolean} - Return true is the text has <, > , /.
   */
  hasSpecialCharacter(text) {
    return /[<|>|\\/]/.test(text);
  }
}
__decorate([
  MapToComponentField()
], CodeHighlightParser.prototype, "value", void 0);
