"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getMentionsCount = getMentionsCount;
exports.isMentionElement = isMentionElement;
exports.resetNodes = resetNodes;
var _slate = require("slate");
var _constants = require("./constants");
function isMentionElement(type) {
  return type === _constants.CUSTOM_ELEMENT_TYPES.mention;
}
function getMentionsCount(nodes) {
  return nodes.reduce((total, {
    children
  }) => {
    return total + children.reduce((subTotal, {
      type
    }) => {
      return isMentionElement(type) ? subTotal + 1 : subTotal;
    }, 0);
  }, 0);
}

/** This function synchronises slate editor chidren with value prop.
 * Setting value directly (editor.children = value) breaks UI when keyboard is used
 * https://docs.slatejs.org/walkthroughs/06-saving-to-a-database
 */
function resetNodes(editor, nodes) {
  const children = [...editor.children];
  children.forEach(node => editor.apply({
    type: 'remove_node',
    path: [0],
    node
  }));
  nodes.forEach((node, i) => editor.apply({
    type: 'insert_node',
    path: [i],
    node
  }));
  const point = _slate.Editor.end(editor, []);
  _slate.Transforms.select(editor, point);
}