"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.flatmap = flatmap;
exports.getFragmentBackingArray = getFragmentBackingArray;
exports.mapChildren = mapChildren;
exports.mapFragment = mapFragment;
exports.mapSlice = mapSlice;

var _prosemirrorModel = require("prosemirror-model");

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

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

/**
 * A helper to get the underlying array of a fragment.
 */
function getFragmentBackingArray(fragment) {
  return fragment.content;
}

function mapFragment(content, callback) {
  var parent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  var children = [];

  for (var i = 0, size = content.childCount; i < size; i++) {
    var _node = content.child(i);

    var transformed = _node.isLeaf ? callback(_node, parent, i) : callback(_node.copy(mapFragment(_node.content, callback, _node)), parent, i);

    if (transformed) {
      if (transformed instanceof _prosemirrorModel.Fragment) {
        children.push.apply(children, _toConsumableArray(getFragmentBackingArray(transformed)));
      } else if (Array.isArray(transformed)) {
        children.push.apply(children, _toConsumableArray(transformed));
      } else {
        children.push(transformed);
      }
    }
  }

  return _prosemirrorModel.Fragment.fromArray(children);
}

function mapSlice(slice, callback) {
  var fragment = mapFragment(slice.content, callback);
  return new _prosemirrorModel.Slice(fragment, slice.openStart, slice.openEnd);
}

function flatmap(fragment, callback) {
  var fragmentContent = [];

  for (var i = 0; i < fragment.childCount; i++) {
    var child = callback(fragment.child(i), i, fragment);

    if (Array.isArray(child)) {
      fragmentContent.push.apply(fragmentContent, _toConsumableArray(child));
    } else {
      fragmentContent.push(child);
    }
  }

  return _prosemirrorModel.Fragment.fromArray(fragmentContent);
}

function mapChildren(node, callback) {
  var array = [];

  for (var i = 0; i < node.childCount; i++) {
    array.push(callback(node.child(i), i, node instanceof _prosemirrorModel.Fragment ? node : node.content));
  }

  return array;
}