"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.WithProviders = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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; })(); }
var WithProviders = exports.WithProviders = /*#__PURE__*/function (_PureComponent) {
  (0, _inherits2.default)(WithProviders, _PureComponent);
  var _super = _createSuper(WithProviders);
  function WithProviders(props) {
    var _this;
    (0, _classCallCheck2.default)(this, WithProviders);
    _this = _super.call(this, props);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleProvider", function (name, provider) {
      _this.setState(function (_ref) {
        var providers = _ref.providers;
        return {
          providers: _objectSpread(_objectSpread({}, providers), {}, (0, _defineProperty2.default)({}, name, provider))
        };
      });
    });
    var _providers = {};
    _this.props.providers.forEach(function (name) {
      _providers[name] = undefined;
    });
    _this.state = {
      providers: _providers
    };
    return _this;
  }
  (0, _createClass2.default)(WithProviders, [{
    key: "UNSAFE_componentWillMount",
    value: function UNSAFE_componentWillMount() {
      var _this2 = this;
      var _this$props = this.props,
        providers = _this$props.providers,
        providerFactory = _this$props.providerFactory;
      providers.forEach(function (name) {
        providerFactory.subscribe(name, _this2.handleProvider);
      });
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var _this3 = this;
      var _this$props2 = this.props,
        providers = _this$props2.providers,
        providerFactory = _this$props2.providerFactory;
      providers.forEach(function (name) {
        providerFactory.unsubscribe(name, _this3.handleProvider);
      });
    }
  }, {
    key: "render",
    value: function render() {
      var state = this.state,
        props = this.props;
      var renderNode = props.renderNode;
      return renderNode(state.providers);
    }
  }]);
  return WithProviders;
}(_react.PureComponent);
(0, _defineProperty2.default)(WithProviders, "displayName", 'WithProviders');