var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import React from 'react';
import PropTypes from 'prop-types';
var PopupDispatcher = /** @class */ (function (_super) {
    __extends(PopupDispatcher, _super);
    function PopupDispatcher(props, context) {
        var _this = _super.call(this, props, context) || this;
        _this.state = {
            stack: []
        };
        return _this;
    }
    PopupDispatcher.prototype.getChildContext = function () {
        var popupContext = this.props.popupContext;
        if (!popupContext) {
            return {};
        }
        return {
            store: popupContext.store || null
        };
    };
    PopupDispatcher.prototype.showPopup = function (element) {
        var _this = this;
        if (element.props.onHide) {
            throw new Error('showPopup do not support onHide property of Popup');
        }
        var popup = React.cloneElement(element, {
            onHide: function () {
                var stack = _this.state.stack;
                var index = stack.indexOf(popup);
                if (index !== -1) {
                    stack.splice(index, 1);
                    _this.setState({ stack: stack });
                }
            }
        });
        return new Promise(function (resolve) {
            if (popup.props.stackable) {
                _this.setState({ stack: _this.state.stack.concat([popup]) }, resolve);
            }
            else {
                _this.setState({ stack: [popup] }, resolve);
            }
        });
    };
    PopupDispatcher.prototype.closePopup = function () {
        if (this.state.stack.length) {
            this.state.stack[this.state.stack.length - 1].props.onHide();
        }
    };
    PopupDispatcher.prototype.render = function () {
        return React.createElement("div", null, this.state.stack.map(function (popup, key) { return React.cloneElement(popup, { key: key }); }));
    };
    PopupDispatcher.propTypes = {
        popupContext: PropTypes.object
    };
    PopupDispatcher.defaultProps = {
        popupContext: null
    };
    // TODO (UIA-70822) for now it is hardcoded since childContextTypes is mandatory, redo with new context API when React is updated
    PopupDispatcher.childContextTypes = {
        store: PropTypes.object // Redux store
    };
    return PopupDispatcher;
}(React.Component));
export { PopupDispatcher };
//# sourceMappingURL=PopupDispatcher.js.map