import _createClass from "@babel/runtime/helpers/createClass";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _toArray from "@babel/runtime/helpers/toArray";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import { Plugin } from '@atlaskit/editor-prosemirror/state';
// 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 = _toArray(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(_toConsumableArray(more)));
        }
      });
      return safeNodeView;
    }
  });
  spec.props.nodeViews = safeNodeViews;
  return spec;
};
export var SafePlugin = /*#__PURE__*/function (_Plugin) {
  _inherits(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) {
    _classCallCheck(this, SafePlugin);
    return _super.call(this, wrapGetPosExceptions(spec));
  }
  return _createClass(SafePlugin);
}(Plugin);