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';
import _ from 'lodash';
var NotificationDispatcher = /** @class */ (function (_super) {
    __extends(NotificationDispatcher, _super);
    function NotificationDispatcher(props, context) {
        var _this = _super.call(this, props, context) || this;
        _this.state = {
            stack: []
        };
        return _this;
    }
    NotificationDispatcher.prototype.getChildContext = function () {
        return {
            isTopFixedNotification: true
        };
    };
    NotificationDispatcher.prototype.showNotification = function (element) {
        var _this = this;
        var key = Date.now();
        var notification = React.cloneElement(element, {
            key: key,
            onClose: function () {
                _this.onNotificationClose(key);
                if (element.props.onClose) {
                    element.props.onClose();
                }
            }
        });
        return new Promise(function (resolve) {
            var stack = [{ notification: notification, key: key }].concat(_this.state.stack);
            _this.setState({ stack: stack }, resolve);
        });
    };
    NotificationDispatcher.prototype.onNotificationClose = function (key) {
        var index = _.findIndex(this.state.stack, { key: key });
        if (index !== -1) {
            this.setState(function (state) {
                var stack = state.stack.slice();
                stack.splice(index, 1);
                return { stack: stack };
            });
        }
    };
    NotificationDispatcher.prototype.render = function () {
        return React.createElement("div", null, this.state.stack.map(function (_a) {
            var notification = _a.notification;
            return notification;
        }));
    };
    NotificationDispatcher.childContextTypes = {
        isTopFixedNotification: PropTypes.bool
    };
    return NotificationDispatcher;
}(React.Component));
export { NotificationDispatcher };
//# sourceMappingURL=NotificationDispatcher.js.map