"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setParagraphSpacing = setParagraphSpacing;
exports.default = void 0;

var _licitDocAttrsStep = require("@modusoperandi/licit-doc-attrs-step");

var _prosemirrorState = require("prosemirror-state");

var _NodeNames = require("./NodeNames");

var _prosemirrorView = require("prosemirror-view");

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

var _prosemirrorTransform = require("prosemirror-transform");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function setParagraphSpacing(tr, schema, paragraphSpacing, isAfter) {
  const {
    selection,
    doc
  } = tr;

  if (!selection || !doc) {
    return tr;
  }

  if (!(selection instanceof _prosemirrorState.TextSelection) && !(selection instanceof _prosemirrorState.AllSelection)) {
    return tr;
  }

  const {
    from,
    to
  } = selection;
  const paragraph = schema.nodes[_NodeNames.PARAGRAPH];
  const heading = schema.nodes[_NodeNames.HEADING];
  const listItem = schema.nodes[_NodeNames.LIST_ITEM];
  const blockquote = schema.nodes[_NodeNames.BLOCKQUOTE];

  if (!paragraph && !heading && !listItem && !blockquote) {
    return tr;
  }

  const tasks = [];
  const paragraphSpacingValue = paragraphSpacing || null;
  doc.nodesBetween(from, to, (node, pos, parentNode) => {
    const nodeType = node.type;

    if (nodeType === paragraph || nodeType === heading || nodeType === listItem || nodeType === blockquote) {
      const paragraphSpacing = node.attrs.paragraphSpacing || null;

      if (paragraphSpacing !== paragraphSpacingValue) {
        tasks.push({
          node,
          pos,
          nodeType
        });
      }

      return nodeType === listItem ? true : false;
    }

    return true;
  });

  if (!tasks.length) {
    return tr;
  }

  tasks.forEach(job => {
    const {
      node,
      pos,
      nodeType
    } = job;
    let {
      attrs
    } = node;

    if (isAfter) {
      attrs = _objectSpread(_objectSpread({}, attrs), {}, {
        paragraphSpacingAfter: paragraphSpacingValue ? paragraphSpacingValue : null
      });
    } else {
      attrs = _objectSpread(_objectSpread({}, attrs), {}, {
        paragraphSpacingBefore: paragraphSpacingValue ? paragraphSpacingValue : null
      });
    }

    tr = tr.setNodeMarkup(pos, nodeType, attrs, node.marks);
  });
  return tr;
}

class ParagraphSpacingCommand extends _licitDocAttrsStep.UICommand {
  constructor(paragraphSpacing, isAfter) {
    super();

    _defineProperty(this, "_paragraphSpacing", void 0);

    _defineProperty(this, "_isAfter", void 0);

    _defineProperty(this, "execute", (state, dispatch, view) => {
      const {
        schema,
        selection
      } = state;
      const tr = setParagraphSpacing(state.tr.setSelection(selection), schema, this._paragraphSpacing, this._isAfter);

      if (tr.docChanged) {
        dispatch && dispatch(tr);
        return true;
      } else {
        return false;
      }
    });

    this._paragraphSpacing = paragraphSpacing;
    this._isAfter = isAfter;
  }

}

var _default = ParagraphSpacingCommand;
exports.default = _default;