"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.splitStyledTextByToken = exports.splitStringByFirstToken = exports.isTokenValid = exports.getTokenIndexes = void 0;

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

var DEFAULT_STYLED_TEXT_TOKEN = {
  start: '[[',
  end: ']]'
};

/**
 *  getTokenIndexes
 *
 *  @param {string} text The string being searched
 *  @param {StyledTextToken} token An object specifying the characters used as begining and the end of the token
 *
 *  @returns {StyledTokenIndexes} an object storing the indexes of the start and end of the token inside of `text`
 */
var getTokenIndexes = function getTokenIndexes(text, token) {
  return {
    start: text.indexOf(token.start),
    end: text.indexOf(token.end)
  };
};
/**
 *  isTokenValid
 *
 *  @param {StyledTokenIndexes} tokenIndexes
 *
 *  @returns {boolean} Whether the start and end tokens exist in the string and are in the correct order
 */


exports.getTokenIndexes = getTokenIndexes;

var isTokenValid = function isTokenValid(_ref) {
  var start = _ref.start,
      end = _ref.end;
  return start !== -1 && end !== -1 && start < end;
};

exports.isTokenValid = isTokenValid;

/**
 *  splitStringByFirstToken
 *  @param {string} text The string being split
 *  @param {StyledTextToken} token An object specifying the begining and the end of the string token
 *  @param {StyledTokenIndexes} tokenIndexes An object specifying the indexes of where `token` starts and ends in `text`
 *
 *  @returns {TokenizedTextSegments} An object including the substrings of `text` before, in between, and after the `token`
 */
var splitStringByFirstToken = function splitStringByFirstToken(text, token, tokenIndexes) {
  return {
    prefix: text.slice(0, tokenIndexes.start),
    styled: text.slice(tokenIndexes.start + token.start.length, tokenIndexes.end),
    suffix: text.slice(tokenIndexes.end + token.end.length)
  };
};

exports.splitStringByFirstToken = splitStringByFirstToken;

/**
 *  splitStyledTextByToken
 *
 *  @param {string} text The tokenized string you want to split by the given token
 *  @param {StyledTextToken} token An object specifying the begining and the end of the string token
 *
 *  @returns {StyledTextSegment[]} An array of objects containing a flag to apply the inline styles or not and the split text segment
 */
var splitStyledTextByToken = function splitStyledTextByToken() {
  var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var token = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_STYLED_TEXT_TOKEN;
  if (!text.length) return [];
  var tokenIndexes = getTokenIndexes(text, token);
  if (!isTokenValid(tokenIndexes)) return [{
    applyInlineStyle: false,
    text: text
  }];

  var _splitStringByFirstTo = splitStringByFirstToken(text, token, tokenIndexes),
      prefix = _splitStringByFirstTo.prefix,
      styled = _splitStringByFirstTo.styled,
      suffix = _splitStringByFirstTo.suffix;

  return [].concat(_toConsumableArray(prefix.length ? [{
    applyInlineStyle: false,
    text: prefix
  }] : []), _toConsumableArray(styled.length ? [{
    applyInlineStyle: true,
    text: styled
  }] : []), _toConsumableArray(splitStyledTextByToken(suffix)));
};

exports.splitStyledTextByToken = splitStyledTextByToken;