import * as tslib_1 from "tslib";
import { getDisplayName } from "recompose";
import { PureComponent } from "./../React";
export function connect(callback) {
    return (WrappedComponent) => {
        const displayName = getDisplayName(WrappedComponent);
        class Connector extends PureComponent {
            /**
             * @returns {null}
             */
            constructor(props, context) {
                super(props, context);
                this.state = {};
                this.listeners = [];
                this.mounted = false;
                const streams = callback(props);
                Object.keys(streams).forEach(streamName => {
                    this.listeners.push(streams[streamName].subscribe((value) => {
                        if (this.mounted) {
                            this.setState({ [streamName]: value });
                        }
                        else {
                            this.state[streamName] = value;
                        }
                    }));
                });
            }
            render() {
                const props = tslib_1.__assign({}, this.props, this.state);
                return <WrappedComponent {...props}/>;
            }
            /**
             * @returns {null}
             */
            componentDidMount() {
                this.mounted = true;
                return null;
            }
            /**
             * @returns {null}
             */
            componentWillUnmount() {
                this.listeners.forEach(listener => listener.unsubscribe());
                this.mounted = false;
                return null;
            }
        }
        Connector.WrappedComponent = WrappedComponent;
        Connector.displayName = displayName;
        return Connector;
    };
}
