"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.convertTextToTextEditorValue = convertTextToTextEditorValue;
exports.getTextEditorValueLength = getTextEditorValueLength;
exports.hasGreaterTextEditorLinesThan = hasGreaterTextEditorLinesThan;
exports.isEmptyTextEditorValue = isEmptyTextEditorValue;
exports.isLongerTextEditorValueThan = isLongerTextEditorValueThan;
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
var _textEditorConstants = require("./text-editor-constants");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
 * @param {Array} value - the type of value is defined with TextEditorValue
 * @returns {boolean}
 */
function isEmptyTextEditorValue(value) {
  return (0, _isEqual.default)(value, _textEditorConstants.TEXT_EDITOR_DEFAULT_VALUE);
}

/**
 * @param {Array} value - the type of value is defined with TextEditorValue
 * @param {number} maxLength
 * @returns {boolean}
 */
function isLongerTextEditorValueThan(value, maxLength = 120) {
  let length = 0;
  for (const record of value) {
    // if we reached maxLength - no need to continue
    if (length > maxLength) {
      break;
    }
    // if this is leaf node that has text - sum it up
    if (record.hasOwnProperty('text') && !!record.text) {
      length += record?.text?.length ?? 0;
    } else if (record.hasOwnProperty('children') && !!record.children) {
      // if this is a node that has children - calculate length of children
      length += getTextEditorValueLength(record.children);
    }
  }
  return length > maxLength;
}
function getTextEditorValueLength(value) {
  let length = 0;
  for (const record of value) {
    // if this is leaf node that has text - sum it up
    if (record.hasOwnProperty('text') && !!record.text) {
      length += record?.text?.length ?? 0;
    } else if (record.hasOwnProperty('children') && !!record.children) {
      // if this is a node that has children - iterate through them via recursion
      length += getTextEditorValueLength(record.children);
    }
  }
  return length;
}

/**
 * @param {Array} value - the type of value is defined with TextEditorValue
 * @param {number} maxTextLines
 * @returns {boolean}
 */
function hasGreaterTextEditorLinesThan(value, maxTextLines = 1) {
  return value.length > maxTextLines;
}

/**
 * @param {string} text
 * @returns {Array.<object>} the type of value is defined with TextEditorValue
 */
function convertTextToTextEditorValue(text) {
  return [{
    type: _textEditorConstants.BLOCK_TYPES.REGULAR_TEXT,
    children: [{
      text
    }]
  }];
}