"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SafePlugin = void 0;
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
var _state = require("@atlaskit/editor-prosemirror/state");
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
// Wraper to avoid any exception during the get pos operation
// See this https://hello.atlassian.net/wiki/spaces/EDITOR/pages/2849713193/ED-19672+Extensions+Regression
// And this https://discuss.prosemirror.net/t/possible-bug-on-viewdesc-posbeforechild/5783
var wrapGetPosExceptions = function wrapGetPosExceptions(spec) {
  var _spec$props;
  if (!(spec !== null && spec !== void 0 && (_spec$props = spec.props) !== null && _spec$props !== void 0 && _spec$props.nodeViews)) {
    return spec;
  }
  var unsafeNodeViews = spec.props.nodeViews;
  var safeNodeViews = new Proxy(unsafeNodeViews, {
    get: function get(target, prop, receiver) {
      var safeNodeView = new Proxy(Reflect.get(target, prop, receiver), {
        apply: function apply(target, thisArg, argumentsList) {
          var _argumentsList = (0, _toArray2.default)(argumentsList),
            node = _argumentsList[0],
            view = _argumentsList[1],
            unsafeGetPos = _argumentsList[2],
            more = _argumentsList.slice(3);
          var safeGetPos = function () {
            try {
              return unsafeGetPos();
            } catch (e) {
              return;
            }
            return;
            // eslint-disable-next-line no-extra-bind
          }.bind(thisArg);
          return Reflect.apply(target, thisArg, [node, view, safeGetPos].concat((0, _toConsumableArray2.default)(more)));
        }
      });
      return safeNodeView;
    }
  });
  spec.props.nodeViews = safeNodeViews;
  return spec;
};
var SafePlugin = exports.SafePlugin = /*#__PURE__*/function (_Plugin) {
  (0, _inherits2.default)(SafePlugin, _Plugin);
  var _super = _createSuper(SafePlugin);
  // This variable isn't (and shouldn't) be used anywhere. Its purpose is
  // to distinguish Plugin from SafePlugin, thus ensuring that an 'unsafe'
  // Plugin cannot be assigned as an item in EditorPlugin → pmPlugins.

  function SafePlugin(spec) {
    (0, _classCallCheck2.default)(this, SafePlugin);
    return _super.call(this, wrapGetPosExceptions(spec));
  }
  return (0, _createClass2.default)(SafePlugin);
}(_state.Plugin);