"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 _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
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 _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; })(); } /* eslint-disable @repo/internal/react/no-class-components */
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var WithProviders = exports.WithProviders = /*#__PURE__*/function (_PureComponent) {
  function WithProviders(props) {
    var _this;
    (0, _classCallCheck2.default)(this, WithProviders);
    _this = _callSuper(this, WithProviders, [props]);
    (0, _defineProperty2.default)(_this, "mounted", false);
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (0, _defineProperty2.default)(_this, "handleProviderIfMounted", function (name, provider) {
      if (!_this.mounted) {
        return;
      }
      _this.handleProvider(name, provider);
    });
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (0, _defineProperty2.default)(_this, "handleProvider", function (name, provider) {
      _this.setState(function (_ref) {
        var providers = _ref.providers;
        return {
          providers: _objectSpread(_objectSpread({}, providers), {}, (0, _defineProperty2.default)({}, name, provider))
        };
      });
    });
    _this.mounted = false;

    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    var _providers = {};
    _this.props.providers.forEach(function (name) {
      var providerPromise = props.providerFactory.subscribe(name, _this.handleProviderIfMounted);
      _providers[name] = providerPromise;
    });
    _this.state = {
      providers: _providers
    };
    return _this;
  }
  (0, _inherits2.default)(WithProviders, _PureComponent);
  return (0, _createClass2.default)(WithProviders, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.mounted = true;
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var _this2 = this;
      var _this$props = this.props,
        providers = _this$props.providers,
        providerFactory = _this$props.providerFactory;
      providers.forEach(function (name) {
        providerFactory.unsubscribe(name, _this2.handleProviderIfMounted);
      });
    }
  }, {
    key: "render",
    value: function render() {
      var state = this.state,
        props = this.props;
      var renderNode = props.renderNode;
      return renderNode(state.providers);
    }
  }]);
}(_react.PureComponent);
(0, _defineProperty2.default)(WithProviders, "displayName", 'WithProviders');