'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 classnames_1 = __importDefault(require("classnames"));
var Icon_1 = require("./Icon");
var EventBus_1 = __importDefault(require("../utils/EventBus"));
var utils_1 = require("./utils");
var utils_2 = require("../../utils");
var DISABLE_NOTIFICATION = 0;
var OPEN_TIMEOUT = 100; // Wait for render before change className
var ANIMATION_TIMEOUT = 500; // Css animation duration
var DEFAULT_TIMEOUT = 3;
var Notification = /** @class */ (function (_super) {
    __extends(Notification, _super);
    function Notification(props, context) {
        var _this = _super.call(this, props, context) || this;
        _this.showNotification = function () {
            if (_this.getTimeout() !== DISABLE_NOTIFICATION) {
                var height = utils_1.getElementHeight(_this.refs.notification, true);
                _this.setState({
                    height: height,
                    open: true,
                    shouldRenderChildren: false
                }, _this.startCloseTimeout);
            }
        };
        _this.startCloseTimeout = function () {
            _this.stopCloseTimeout();
            var timeout = _this.getTimeout();
            setTimeout(function () {
                _this.setState({
                    shouldRenderChildren: true
                });
            }, 10);
            if (timeout > 0) {
                _this.closeTimeout = setTimeout(_this.close, timeout + ANIMATION_TIMEOUT);
            }
        };
        _this.stopCloseTimeout = function () {
            clearTimeout(_this.closeTimeout);
        };
        _this.onClose = function () {
            if (_this.waitForTestAutomation()) {
                EventBus_1.default.emit(utils_2.constants.onTestAutomationReady);
            }
            if (_this.props.onClose) {
                _this.props.onClose();
            }
        };
        _this.close = function () {
            _this.stopCloseTimeout();
            _this.setState({ height: 0, open: false }, function () { return setTimeout(_this.onClose, ANIMATION_TIMEOUT); });
        };
        _this.state = {
            height: 0,
            open: false,
            //UIA-69650 delay rendering children to support accessibility
            shouldRenderChildren: true
        };
        return _this;
    }
    Notification.prototype.componentDidMount = function () {
        if (this.waitForTestAutomation()) {
            EventBus_1.default.emit(utils_2.constants.onTestAutomationAwait);
        }
        setTimeout(this.showNotification, OPEN_TIMEOUT);
    };
    Notification.prototype.componentWillReceiveProps = function (nextProps) {
        var changed = nextProps.success !== this.props.success ||
            nextProps.warning !== this.props.warning ||
            nextProps.error !== this.props.error ||
            nextProps.danger !== this.props.danger ||
            nextProps.timeout !== this.props.timeout ||
            nextProps.className !== this.props.className ||
            nextProps.children !== this.props.children;
        if (changed) {
            this.showNotification();
        }
    };
    Notification.prototype.componentWillUnmount = function () {
        this.stopCloseTimeout();
    };
    Notification.prototype.waitForTestAutomation = function () {
        var timeout = this.getTimeout();
        return this.context.isTopFixedNotification && timeout > 0;
    };
    Notification.prototype.getTimeout = function () {
        var _a = utils_2.getConfig().dismissibleAlertDuration, dismissibleAlertDuration = _a === void 0 ? DEFAULT_TIMEOUT : _a;
        return this.props.timeout === null ? dismissibleAlertDuration * 1000 : this.props.timeout;
    };
    Notification.prototype.render = function () {
        if (this.getTimeout() === DISABLE_NOTIFICATION) {
            return null;
        }
        var _a = this.props, success = _a.success, warning = _a.warning, error = _a.error, danger = _a.danger, className = _a.className, style = _a.style, testAutomationId = _a.testAutomationId, children = _a.children;
        var wrapperClassName = classnames_1.default('notification-wrapper', {
            'notification-open': this.state.open
        });
        var notificationClassName = classnames_1.default(className, 'notification', {
            'notification-success': success && !warning && !error && !danger,
            'notification-warning': warning && !error && !danger,
            'notification-error': error && !danger,
            'notification-danger': danger
        });
        return (react_1.default.createElement("div", { className: wrapperClassName, style: { height: this.state.height } },
            react_1.default.createElement("div", { className: notificationClassName, onMouseEnter: this.stopCloseTimeout, onMouseMove: this.stopCloseTimeout, onMouseLeave: this.startCloseTimeout, "data-test-automation-id": testAutomationId, style: style, role: "alert", ref: "notification" },
                react_1.default.createElement("span", { className: "notification-close-button", onClick: this.close, "aria-hidden": "true", "data-test-automation-id": "closeButton" },
                    react_1.default.createElement(Icon_1.Icon, { name: "close", testAutomationId: '' })),
                react_1.default.createElement("span", { "data-test-automation-id": "text" }, this.state.shouldRenderChildren ? children : null))));
    };
    Notification.propTypes = {
        success: prop_types_1.default.bool,
        warning: prop_types_1.default.bool,
        error: prop_types_1.default.bool,
        danger: prop_types_1.default.bool,
        timeout: prop_types_1.default.number,
        className: prop_types_1.default.string,
        style: prop_types_1.default.object,
        testAutomationId: prop_types_1.default.string,
        children: prop_types_1.default.node,
        onClose: prop_types_1.default.func
    };
    Notification.defaultProps = {
        success: false,
        warning: false,
        error: false,
        danger: false,
        timeout: null,
        className: '',
        style: {},
        testAutomationId: 'notification',
        children: null,
        onClose: null
    };
    Notification.contextTypes = {
        isTopFixedNotification: prop_types_1.default.bool
    };
    return Notification;
}(react_1.default.PureComponent));
exports.Notification = Notification;
//# sourceMappingURL=Notification.js.map