"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ExtensionNode = void 0;
exports.default = ExtensionNodeView;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireDefault(require("react"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _reactNodeView = _interopRequireDefault(require("../react-node-view"));
var _Extension = require("./Extension");
var _ExtensionNodeWrapper = require("./ExtensionNodeWrapper");
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _superPropGet(t, o, e, r) { var p = (0, _get2.default)((0, _getPrototypeOf2.default)(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; }
// getInlineNodeViewProducer is a new api to use instead of ReactNodeView
// when creating inline node views, however, it is difficult to test the impact
// on selections when migrating inlineExtension to use the new api.
// The ReactNodeView api will be visited in the second phase of the selections
// project whilst investigating block nodes. We will revisit the Extension node view there too.
var ExtensionNode = exports.ExtensionNode = /*#__PURE__*/function (_ReactNodeView) {
  function ExtensionNode() {
    (0, _classCallCheck2.default)(this, ExtensionNode);
    return _callSuper(this, ExtensionNode, arguments);
  }
  (0, _inherits2.default)(ExtensionNode, _ReactNodeView);
  return (0, _createClass2.default)(ExtensionNode, [{
    key: "ignoreMutation",
    value: function ignoreMutation(mutation) {
      // Extensions can perform async operations that will change the DOM.
      // To avoid having their tree rebuilt, we need to ignore the mutation
      // for atom based extensions if its not a layout, we need to give
      // children a chance to recalc
      return this.node.type.isAtom || mutation.type !== 'selection' && mutation.attributeName !== 'data-layout';
    }

    // Reserve height by setting a minimum height for the extension node view element
  }, {
    key: "createDomRef",
    value: function createDomRef() {
      if (!(0, _platformFeatureFlags.fg)('confluence_connect_macro_preset_height')) {
        return _superPropGet(ExtensionNode, "createDomRef", this, 3)([]);
      }
      if (!this.node.isInline) {
        var _this$reactComponentP, _this$reactComponentP2;
        var _htmlElement = document.createElement('div');
        var extensionHeight = (_this$reactComponentP = this.reactComponentProps) === null || _this$reactComponentP === void 0 || (_this$reactComponentP = _this$reactComponentP.extensionNodeViewOptions) === null || _this$reactComponentP === void 0 || (_this$reactComponentP2 = _this$reactComponentP.getExtensionHeight) === null || _this$reactComponentP2 === void 0 ? void 0 : _this$reactComponentP2.call(_this$reactComponentP, this.node);
        if (extensionHeight) {
          _htmlElement.style.setProperty('min-height', "".concat(extensionHeight, "px"));
        }
        return _htmlElement;
      }
      var htmlElement = document.createElement('span');
      return htmlElement;
    }

    /**
     * When interacting with input elements inside an extension's body, the events
     * bubble up to the editor and get handled by it. This almost always gets in the way
     * of being able to actually interact with said input in the extension, i.e.
     * typing inside a text field (in an extension body) will print the text in the editor
     * content area instead. This change prevents the editor view from trying to handle these events,
     * when the target of the event is an input element, so the extension can.
     */
  }, {
    key: "stopEvent",
    value: function stopEvent(event) {
      if ((0, _platformFeatureFlags.fg)('forge-ui-extensionnodeview-stop-event-for-textarea')) {
        return event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement;
      }
      return event.target instanceof HTMLInputElement;
    }
  }, {
    key: "getContentDOM",
    value: function getContentDOM() {
      if (this.node.isInline) {
        return;
      }
      var dom = document.createElement('div');
      dom.className = "".concat(this.node.type.name, "-content-dom-wrapper");
      return {
        dom: dom
      };
    }
  }, {
    key: "render",
    value: function render(props, forwardRef) {
      var _props$extensionNodeV;
      return /*#__PURE__*/_react.default.createElement(_ExtensionNodeWrapper.ExtensionNodeWrapper, {
        nodeType: this.node.type.name,
        macroInteractionDesignFeatureFlags: props.macroInteractionDesignFeatureFlags
      }, /*#__PURE__*/_react.default.createElement(_Extension.Extension, {
        editorView: this.view,
        node: this.node,
        eventDispatcher: this.eventDispatcher
        // The getPos arg is always a function when used with nodes
        // the version of the types we use has a union with the type
        // for marks.
        // This has been fixed in later versions of the definitly typed
        // types (and also in prosmirror-views inbuilt types).
        // https://github.com/DefinitelyTyped/DefinitelyTyped/pull/57384
        ,
        getPos: this.getPos,
        providerFactory: props.providerFactory,
        handleContentDOMRef: forwardRef,
        extensionHandlers: props.extensionHandlers,
        editorAppearance: (_props$extensionNodeV = props.extensionNodeViewOptions) === null || _props$extensionNodeV === void 0 ? void 0 : _props$extensionNodeV.appearance,
        pluginInjectionApi: props.pluginInjectionApi,
        macroInteractionDesignFeatureFlags: props.macroInteractionDesignFeatureFlags,
        showLivePagesBodiedMacrosRendererView: props.showLivePagesBodiedMacrosRendererView,
        showUpdatedLivePages1PBodiedExtensionUI: props.showUpdatedLivePages1PBodiedExtensionUI,
        rendererExtensionHandlers: props.rendererExtensionHandlers
      }));
    }
  }]);
}(_reactNodeView.default);
function ExtensionNodeView(portalProviderAPI, eventDispatcher, providerFactory, extensionHandlers, extensionNodeViewOptions, pluginInjectionApi, macroInteractionDesignFeatureFlags, showLivePagesBodiedMacrosRendererView, showUpdatedLivePages1PBodiedExtensionUI, rendererExtensionHandlers) {
  return function (node, view, getPos) {
    return new ExtensionNode(node, view, getPos, portalProviderAPI, eventDispatcher, {
      providerFactory: providerFactory,
      extensionHandlers: extensionHandlers,
      extensionNodeViewOptions: extensionNodeViewOptions,
      pluginInjectionApi: pluginInjectionApi,
      macroInteractionDesignFeatureFlags: macroInteractionDesignFeatureFlags,
      showLivePagesBodiedMacrosRendererView: showLivePagesBodiedMacrosRendererView,
      showUpdatedLivePages1PBodiedExtensionUI: showUpdatedLivePages1PBodiedExtensionUI,
      rendererExtensionHandlers: rendererExtensionHandlers
    }).init();
  };
}