import _defineProperty from "@babel/runtime/helpers/defineProperty";
/* 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 class WithProviders extends PureComponent {
  constructor(props) {
    super(props);
    _defineProperty(this, "mounted", false);
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    _defineProperty(this, "handleProviderIfMounted", (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", (name, provider) => {
      this.setState(({
        providers
      }) => {
        return {
          providers: {
            ...providers,
            [name]: provider
          }
        };
      });
    });
    this.mounted = false;

    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const _providers = {};
    this.props.providers.forEach(name => {
      const providerPromise = props.providerFactory.subscribe(name, this.handleProviderIfMounted);
      _providers[name] = providerPromise;
    });
    this.state = {
      providers: _providers
    };
  }
  componentDidMount() {
    this.mounted = true;
  }
  componentWillUnmount() {
    const {
      providers,
      providerFactory
    } = this.props;
    providers.forEach(name => {
      providerFactory.unsubscribe(name, this.handleProviderIfMounted);
    });
  }
  render() {
    const {
      state,
      props
    } = this;
    const {
      renderNode
    } = props;
    return renderNode(state.providers);
  }
}
_defineProperty(WithProviders, "displayName", 'WithProviders');