"use strict";
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 __());
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var lodash_1 = __importDefault(require("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_1.default.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 = lodash_1.default.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_1.default.createElement("div", null, this.state.stack.map(function (_a) {
            var notification = _a.notification;
            return notification;
        }));
    };
    NotificationDispatcher.childContextTypes = {
        isTopFixedNotification: prop_types_1.default.bool
    };
    return NotificationDispatcher;
}(react_1.default.Component));
exports.NotificationDispatcher = NotificationDispatcher;
//# sourceMappingURL=NotificationDispatcher.js.map