import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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) { _defineProperty(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 = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(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 */
import { PureComponent } from 'react';
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export var WithProviders = /*#__PURE__*/function (_PureComponent) {
  function WithProviders(props) {
    var _this;
    _classCallCheck(this, WithProviders);
    _this = _callSuper(this, WithProviders, [props]);
    _defineProperty(_this, "mounted", false);
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    _defineProperty(_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
    _defineProperty(_this, "handleProvider", function (name, provider) {
      _this.setState(function (_ref) {
        var providers = _ref.providers;
        return {
          providers: _objectSpread(_objectSpread({}, providers), {}, _defineProperty({}, 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;
  }
  _inherits(WithProviders, _PureComponent);
  return _createClass(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);
    }
  }]);
}(PureComponent);
_defineProperty(WithProviders, "displayName", 'WithProviders');