import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

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() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { PureComponent, createContext } from 'react';
export var Context = /*#__PURE__*/createContext({});

var DocumentObjectProvider = function DocumentObjectProvider(WrappedComponent, documentRef) {
  var _class;

  var Provider = (_class = /*#__PURE__*/function (_PureComponent) {
    _inherits(Provider, _PureComponent);

    var _super = _createSuper(Provider);

    function Provider() {
      _classCallCheck(this, Provider);

      return _super.apply(this, arguments);
    }

    _createClass(Provider, [{
      key: "componentDidMount",
      value: function componentDidMount() {
        this.forceUpdate(); // force a re-render because we have the document ref now
      }
    }, {
      key: "render",
      value: function render() {
        if (!documentRef) {
          return /*#__PURE__*/React.createElement("span", {
            style: {
              display: 'none'
            },
            ref: function ref(node) {
              if (!node) {
                return;
              }

              documentRef = node.ownerDocument;
            }
          });
        }

        return /*#__PURE__*/React.createElement(Context.Provider, {
          value: documentRef
        }, /*#__PURE__*/React.createElement(WrappedComponent, this.props));
      }
    }]);

    return Provider;
  }(PureComponent), _defineProperty(_class, "displayName", 'Provider'), _class);
  return Provider;
};

export default DocumentObjectProvider;