"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.stripHtmlTags = exports.removeUnwantedCharacters = exports.processMarkup = exports.getAdjustedLength = exports.createSlateMarkup = void 0;

var _escape = _interopRequireDefault(require("lodash/escape"));

// do not remove \t from \times, \triangle, \tan, \theta or \therefore
var tSymbols = 'imes|riangle|an|heta|herefore'; // do not remove \n from \nthroot, \nparallel, \ncong, \napprox, \neq, \ne or \nsim

var nSymbols = 'throot|parallel|cong|approx|eq|e|sim'; // match all \t and \n that are not part of math symbols that starts with \t or \n

var matchTabAndNewLine = new RegExp("(\\t(?!".concat(tSymbols, "))|(\\n(?!").concat(nSymbols, "))|(\\\\t(?!").concat(tSymbols, "))|(\\\\n(?!").concat(nSymbols, "))"), 'g');

var removeUnwantedCharacters = function removeUnwantedCharacters(markup) {
  return markup.replace(matchTabAndNewLine, '').replace(/\\"/g, '"').replace(/\\\//g, '/');
};

exports.removeUnwantedCharacters = removeUnwantedCharacters;

var createElementFromHTML = function createElementFromHTML() {
  var htmlString = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();
  return div;
};

var processMarkup = function processMarkup(markup) {
  var newMarkup = removeUnwantedCharacters(markup || '');
  var slateMarkup = createElementFromHTML(newMarkup || '');
  var index = 0;
  slateMarkup.querySelectorAll('[data-type="explicit_constructed_response"]').forEach(function (s) {
    s.replaceWith("{{".concat(index++, "}}"));
  });
  return slateMarkup.innerHTML;
};

exports.processMarkup = processMarkup;
var REGEX = /\{\{(\d+)\}\}/g;

var createSlateMarkup = function createSlateMarkup(markup, choices) {
  if (!markup) {
    return '';
  }

  var newMarkup = removeUnwantedCharacters(markup);
  return newMarkup.replace(REGEX, function (match, g) {
    var label = choices[g][0].label || '';
    return "<span data-type=\"explicit_constructed_response\" data-index=\"".concat(g, "\" data-value=\"").concat((0, _escape["default"])(label), "\"></span>");
  });
}; // also used in controller/src/index.js


exports.createSlateMarkup = createSlateMarkup;

var getAdjustedLength = function getAdjustedLength(length) {
  if (length <= 2) {
    return length + 2;
  }

  if (length <= 4) {
    return length + 3;
  }

  if (length <= 6) {
    return length + 4;
  }

  return length + 5;
};

exports.getAdjustedLength = getAdjustedLength;

var stripHtmlTags = function stripHtmlTags(markup) {
  if (typeof markup !== 'string') {
    return '';
  }

  return markup.replace(/<\/?[^>]+(>|$)/g, "");
};

exports.stripHtmlTags = stripHtmlTags;
//# sourceMappingURL=markupUtils.js.map