'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var prosemirrorModel = require('prosemirror-model');
var prosemirrorState = require('prosemirror-state');
var prosemirrorUtils = require('prosemirror-utils');
var utils_dist_uiduEditorCoreUtils = require('../../dist/index-b4e68eb9.cjs.dev.js');
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var adfUtils = require('@uidu/adf-utils');
require('@uidu/editor-common');
require('@uidu/editor-json-transformer');
require('prosemirror-commands');
require('prosemirror-transform');
require('@babel/runtime/helpers/inherits');
require('@babel/runtime/helpers/createSuper');
require('prosemirror-view');
require('@uidu/editor-common/selection');
require('@uidu/mentions');
require('@uidu/editor-common/utils');
require('@babel/runtime/helpers/typeof');
require('@babel/runtime/helpers/assertThisInitialized');

function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }

var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);

function removeMarks(node) {
  var newNode = _objectSpread({}, node);
  delete newNode.marks;
  return newNode;
}
function sanitizeNode(json) {
  var sanitizedJSON = adfUtils.traverse(json, {
    text: function text(node) {
      if (!node || !Array.isArray(node.marks)) {
        return node;
      }
      return _objectSpread(_objectSpread({}, node), {}, {
        marks: node.marks.filter(function (mark) {
          return mark.type !== 'typeAheadQuery';
        })
      });
    },
    status: function status(node) {
      if (node.attrs && !!node.attrs.text) {
        return removeMarks(node);
      }
      return false; // empty status
    },

    emoji: removeMarks,
    mention: removeMarks,
    date: removeMarks,
    hardBreak: removeMarks,
    inlineCard: removeMarks
  });
  return sanitizedJSON;
}

var EditorActions = /*#__PURE__*/function () {
  function EditorActions() {
    _classCallCheck(this, EditorActions);
    _defineProperty(this, "editorView", void 0);
    _defineProperty(this, "contentTransformer", void 0);
    _defineProperty(this, "contentEncode", void 0);
    _defineProperty(this, "eventDispatcher", void 0);
    _defineProperty(this, "listeners", []);
  }
  _createClass(EditorActions, [{
    key: "_privateGetEditorView",
    value:
    // This method needs to be public for context based helper components.
    function _privateGetEditorView() {
      return this.editorView;
    }
  }, {
    key: "_privateGetEventDispatcher",
    value: function _privateGetEventDispatcher() {
      return this.eventDispatcher;
    }

    // This method needs to be public for EditorContext component.
  }, {
    key: "_privateRegisterEditor",
    value: function _privateRegisterEditor(editorView, eventDispatcher, contentTransformer) {
      this.contentTransformer = contentTransformer;
      this.eventDispatcher = eventDispatcher;
      if (!this.editorView && editorView) {
        this.editorView = editorView;
        this.listeners.forEach(function (cb) {
          return cb(editorView, eventDispatcher);
        });
      } else if (this.editorView !== editorView) {
        throw new Error("Editor has already been registered! It's not allowed to re-register editor with the new Editor instance.");
      }
      if (this.contentTransformer) {
        this.contentEncode = this.contentTransformer.encode.bind(this.contentTransformer);
      }
    }

    // This method needs to be public for EditorContext component.
  }, {
    key: "_privateUnregisterEditor",
    value: function _privateUnregisterEditor() {
      this.editorView = undefined;
      this.contentTransformer = undefined;
      this.contentEncode = undefined;
      this.eventDispatcher = undefined;
    }
  }, {
    key: "_privateSubscribe",
    value: function _privateSubscribe(cb) {
      // If editor is registered and somebody is trying to add a listener,
      // just call it first.
      if (this.editorView && this.eventDispatcher) {
        cb(this.editorView, this.eventDispatcher);
      }
      this.listeners.push(cb);
    }
  }, {
    key: "_privateUnsubscribe",
    value: function _privateUnsubscribe(cb) {
      this.listeners = this.listeners.filter(function (c) {
        return c !== cb;
      });
    }
  }, {
    key: "focus",
    value: function focus() {
      if (!this.editorView || this.editorView.hasFocus()) {
        return false;
      }
      this.editorView.focus();
      this.editorView.dispatch(this.editorView.state.tr.scrollIntoView());
      return true;
    }
  }, {
    key: "blur",
    value: function blur() {
      if (!this.editorView || !this.editorView.hasFocus()) {
        return false;
      }
      this.editorView.dom.blur();
      return true;
    }
  }, {
    key: "clear",
    value: function clear() {
      if (!this.editorView) {
        return false;
      }
      var editorView = this.editorView;
      var state = editorView.state;
      var tr = editorView.state.tr.setSelection(prosemirrorState.TextSelection.create(state.doc, 0, state.doc.nodeSize - 2)).deleteSelection();
      editorView.dispatch(tr);
      return true;
    }
  }, {
    key: "getValue",
    value: function () {
      var _getValue = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
        var _this = this;
        var doc;
        return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return utils_dist_uiduEditorCoreUtils.getEditorValueWithMedia(this.editorView);
            case 2:
              doc = _context.sent;
              if (doc) {
                _context.next = 5;
                break;
              }
              return _context.abrupt("return");
            case 5:
              return _context.abrupt("return", utils_dist_uiduEditorCoreUtils.compose(function (doc) {
                return _this.contentEncode ? _this.contentEncode(prosemirrorModel.Node.fromJSON(_this.editorView.state.schema, doc)) : doc;
              }, sanitizeNode, utils_dist_uiduEditorCoreUtils.toJSON)(doc));
            case 6:
            case "end":
              return _context.stop();
          }
        }, _callee, this);
      }));
      function getValue() {
        return _getValue.apply(this, arguments);
      }
      return getValue;
    }()
  }, {
    key: "replaceDocument",
    value: function replaceDocument(rawValue) {
      var shouldScrollToBottom = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
      var shouldAddToHistory = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
      if (!this.editorView || rawValue === undefined || rawValue === null) {
        return false;
      }
      var state = this.editorView.state;
      var schema = state.schema;
      var content = utils_dist_uiduEditorCoreUtils.processRawValue(schema, rawValue, undefined, undefined, this.contentTransformer);
      if (!content) {
        return false;
      }

      // In case of replacing a whole document, we only need a content of a top level node e.g. document.
      var tr = state.tr.replaceWith(0, state.doc.nodeSize - 2, content.content);
      if (!shouldScrollToBottom && !tr.selectionSet) {
        // Restore selection at start of document instead of the end.
        tr.setSelection(prosemirrorState.Selection.atStart(tr.doc));
      }
      if (shouldScrollToBottom) {
        tr = tr.scrollIntoView();
      }
      if (!shouldAddToHistory) {
        tr.setMeta('addToHistory', false);
      }
      this.editorView.dispatch(tr);
      return true;
    }
  }, {
    key: "replaceSelection",
    value: function replaceSelection(rawValue, tryToReplace) {
      if (!this.editorView) {
        return false;
      }
      var state = this.editorView.state;
      if (!rawValue) {
        var tr = state.tr.deleteSelection().scrollIntoView();
        this.editorView.dispatch(tr);
        return true;
      }
      var schema = state.schema;
      var content = utils_dist_uiduEditorCoreUtils.processRawValue(schema, rawValue);
      if (!content) {
        return false;
      }

      // try to find a place in the document where to insert a node if its not allowed at the cursor position by schema
      this.editorView.dispatch(prosemirrorUtils.safeInsert(content, undefined, tryToReplace)(state.tr).scrollIntoView());
      return true;
    }
  }, {
    key: "appendText",
    value: function appendText(text) {
      if (!this.editorView || !text) {
        return false;
      }
      var state = this.editorView.state;
      var lastChild = state.doc.lastChild;
      if (lastChild && lastChild.type !== state.schema.nodes.paragraph) {
        return false;
      }
      var tr = state.tr.insertText(text).scrollIntoView();
      this.editorView.dispatch(tr);
      return true;
    }
  }], [{
    key: "from",
    value: function from(view, eventDispatcher, transformer) {
      var editorActions = new EditorActions();
      editorActions._privateRegisterEditor(view, eventDispatcher, transformer);
      return editorActions;
    }
  }]);
  return EditorActions;
}();

exports["default"] = EditorActions;
