import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { NodeSelection, Selection, TextSelection } from '@atlaskit/editor-prosemirror/state';
import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
export function addParagraphAtEnd(tr) {
  var paragraph = tr.doc.type.schema.nodes.paragraph,
    doc = tr.doc;
  if (doc.lastChild && !(doc.lastChild.type === paragraph && doc.lastChild.content.size === 0)) {
    if (paragraph) {
      tr.insert(doc.content.size, paragraph.createAndFill());
    }
  }
  tr.setSelection(TextSelection.create(tr.doc, tr.doc.content.size - 1));
  tr.scrollIntoView();
}
export function createParagraphAtEnd() {
  return function (state, dispatch) {
    var tr = state.tr;
    addParagraphAtEnd(tr);
    if (dispatch) {
      dispatch(tr);
    }
    return true;
  };
}
export var changeImageAlignment = function changeImageAlignment(align) {
  return function (state, dispatch) {
    var _state$selection = state.selection,
      from = _state$selection.from,
      to = _state$selection.to;
    var tr = state.tr;
    state.doc.nodesBetween(from, to, function (node, pos) {
      if (node.type === state.schema.nodes.mediaSingle) {
        tr.setNodeMarkup(pos, undefined, _objectSpread(_objectSpread({}, node.attrs), {}, {
          layout: align === 'center' ? 'center' : "align-".concat(align)
        }));
      }
    });
    if (tr.docChanged && dispatch) {
      dispatch(tr.scrollIntoView());
      return true;
    }
    return false;
  };
};
export var createToggleBlockMarkOnRange = function createToggleBlockMarkOnRange(markType, getAttrs, allowedBlocks) {
  return function (from, to, tr, state) {
    var markApplied = false;
    state.doc.nodesBetween(from, to, function (node, pos, parent) {
      if (!node.type.isBlock) {
        return false;
      }
      if ((!allowedBlocks || (Array.isArray(allowedBlocks) ? allowedBlocks.indexOf(node.type) > -1 : allowedBlocks(state.schema, node, parent))) && parent !== null && parent !== void 0 && parent.type.allowsMarkType(markType)) {
        var oldMarks = node.marks.filter(function (mark) {
          return mark.type === markType;
        });
        var _prevAttrs = oldMarks.length ? oldMarks[0].attrs : undefined;
        var newAttrs = getAttrs(_prevAttrs, node);
        if (newAttrs !== undefined) {
          tr.setNodeMarkup(pos, node.type, node.attrs, node.marks.filter(function (mark) {
            return !markType.excludes(mark.type);
          }).concat(newAttrs === false ? [] : markType.create(newAttrs)));
          markApplied = true;
        }
      }
      return;
    });
    return markApplied;
  };
};
export var createToggleInlineMarkOnRange = function createToggleInlineMarkOnRange(markType, getAttrs) {
  return function (from, to, tr, state) {
    var markApplied = false;
    state.doc.nodesBetween(from, to, function (node, pos, parent) {
      if (parent !== null && parent !== void 0 && parent.type.allowsMarkType(markType)) {
        var oldMarks = node.marks.filter(function (mark) {
          return mark.type === markType;
        });
        var _prevAttrs2 = oldMarks.length ? oldMarks[0].attrs : undefined;
        var newAttrs = getAttrs(_prevAttrs2, node);
        if (newAttrs !== undefined) {
          tr.setNodeMarkup(pos, node.type, node.attrs, node.marks.filter(function (mark) {
            return !markType.excludes(mark.type);
          }).concat(newAttrs === false ? [] : markType.create(newAttrs)));
          tr.setSelection(NodeSelection.create(tr.doc, state.selection.from));
          markApplied = true;
        }
      }
      return;
    });
    return markApplied;
  };
};

/**
 * Toggles block mark based on the return type of `getAttrs`.
 * This is similar to ProseMirror's `getAttrs` from `AttributeSpec`
 * return `false` to remove the mark.
 * return `undefined for no-op.
 * return an `object` to update the mark.
 */
export var toggleBlockMark = function toggleBlockMark(markType, getAttrs, allowedBlocks) {
  return function (state, dispatch) {
    var markApplied = false;
    var tr = state.tr;
    var toggleBlockMarkOnRange = createToggleBlockMarkOnRange(markType, getAttrs, allowedBlocks);
    if (state.selection instanceof CellSelection) {
      state.selection.forEachCell(function (cell, pos) {
        markApplied = toggleBlockMarkOnRange(pos, pos + cell.nodeSize, tr, state);
      });
    } else {
      var _state$selection2 = state.selection,
        from = _state$selection2.from,
        to = _state$selection2.to;
      markApplied = toggleBlockMarkOnRange(from, to, tr, state);
    }
    if (markApplied && tr.docChanged) {
      if (dispatch) {
        dispatch(tr.scrollIntoView());
      }
      return true;
    }
    return false;
  };
};
export var clearEditorContent = function clearEditorContent(state, dispatch) {
  var tr = state.tr;
  tr.replace(0, state.doc.nodeSize - 2);
  tr.setSelection(Selection.atStart(tr.doc));
  if (dispatch) {
    dispatch(tr);
    return true;
  }
  return false;
};

// https://github.com/ProseMirror/prosemirror-commands/blob/master/src/commands.js#L90
// Keep going left up the tree, without going across isolating boundaries, until we
// can go along the tree at that same level
//
// You can think of this as, if you could construct each document like we do in the tests,
// return the position of the first ) backwards from the current selection.
export function findCutBefore($pos) {
  // parent is non-isolating, so we can look across this boundary
  if (!$pos.parent.type.spec.isolating) {
    // search up the tree from the pos's *parent*
    for (var i = $pos.depth - 1; i >= 0; i--) {
      // starting from the inner most node's parent, find out
      // if we're not its first child
      if ($pos.index(i) > 0) {
        return $pos.doc.resolve($pos.before(i + 1));
      }
      if ($pos.node(i).type.spec.isolating) {
        break;
      }
    }
  }
  return null;
}

/**
 * @deprecated
 *
 * This method is no longer needed and can be accessed via the
 * `editor-plugin-block-type` command of `setTextLevel`
 */
export function setHeading(level) {
  return function (state, dispatch) {
    var selection = state.selection,
      schema = state.schema,
      tr = state.tr;
    var ranges = selection instanceof CellSelection ? selection.ranges : [selection];
    ranges.forEach(function (_ref) {
      var $from = _ref.$from,
        $to = _ref.$to;
      tr.setBlockType($from.pos, $to.pos, schema.nodes.heading, {
        level: level
      });
    });
    if (dispatch) {
      dispatch(tr);
    }
    return true;
  };
}
export { insertBlock } from './insert-block';