"use strict";

import * as React from 'react';
import { polyfill } from 'react-lifecycles-compat';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { jsx as _jsx } from "react/jsx-runtime";
export class ComponentWrapper {
  wrap(componentName, OriginalComponentGenerator, store, componentEventsObserver, concreteComponentProvider = OriginalComponentGenerator, ReduxProvider, reduxStore) {
    const GeneratedComponentClass = OriginalComponentGenerator();
    class WrappedComponent extends React.Component {
      static getDerivedStateFromProps(nextProps, prevState) {
        return {
          allProps: {
            ...nextProps,
            ...store.getPropsForId(prevState.componentId)
          }
        };
      }
      _isMounted = false;
      get isMounted() {
        return this._isMounted;
      }
      constructor(props) {
        super(props);
        this._assertComponentId();
        this.state = {
          componentId: props.componentId,
          allProps: {}
        };
        store.setComponentInstance(props.componentId, this);
      }
      setProps(newProps, callback) {
        this.setState(prevState => ({
          allProps: {
            ...prevState.allProps,
            ...newProps
          }
        }), callback);
      }
      componentDidMount() {
        this._isMounted = true;
        store.consumePendingProps(this.state.componentId);
      }
      componentWillUnmount() {
        store.clearComponent(this.state.componentId);
        componentEventsObserver.unmounted(this.state.componentId);
      }
      render() {
        return /*#__PURE__*/_jsx(GeneratedComponentClass, {
          ...this.state.allProps,
          componentId: this.state.componentId,
          componentName: componentName
        });
      }
      _assertComponentId() {
        if (!this.props.componentId) {
          throw new Error(`Component ${componentName} does not have a componentId!`);
        }
      }
    }
    polyfill(WrappedComponent);
    hoistNonReactStatics(WrappedComponent, concreteComponentProvider === OriginalComponentGenerator ? GeneratedComponentClass : concreteComponentProvider());
    return ReduxProvider ? this.wrapWithRedux(WrappedComponent, ReduxProvider, reduxStore) : WrappedComponent;
  }
  wrapWithRedux(WrappedComponent, ReduxProvider, reduxStore) {
    class ReduxWrapper extends React.Component {
      render() {
        return /*#__PURE__*/_jsx(ReduxProvider, {
          store: reduxStore,
          children: /*#__PURE__*/_jsx(WrappedComponent, {
            ...this.props
          })
        });
      }
    }
    hoistNonReactStatics(ReduxWrapper, WrappedComponent);
    return ReduxWrapper;
  }
}
//# sourceMappingURL=ComponentWrapper.js.map