'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 __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React from 'react';
import PropTypes from 'prop-types';
import BSAlert from 'react-bootstrap/lib/Alert';
import Overlay from 'react-bootstrap/lib/Overlay';
import EventBus from '../utils/EventBus';
import { getConfig, constants } from '../../utils';
var DISABLE_DISMISSIBLE_ALERT = 0;
var DEFAULT_DURATION = 3;
var DismissibleAlert = /** @class */ (function (_super) {
    __extends(DismissibleAlert, _super);
    function DismissibleAlert(props, context) {
        var _this = _super.call(this, props, context) || this;
        _this.setShown = function () {
            EventBus.emit(constants.onTestAutomationAwait);
            if (_this.props.onShow) {
                _this.props.onShow();
            }
        };
        _this.setHidden = function () {
            EventBus.emit(constants.onTestAutomationReady);
            if (_this.props.onHide) {
                _this.props.onHide();
            }
        };
        _this.state = {
            show: props.message.text !== '',
            message: {}
        };
        _this.onHide = _this.onHide.bind(_this);
        return _this;
    }
    DismissibleAlert.prototype.componentDidMount = function () {
        this.setDelayTimeout();
    };
    DismissibleAlert.prototype.componentWillReceiveProps = function (nextProps) {
        var _this = this;
        if (nextProps.message !== this.props.message) {
            this.setState({
                show: nextProps.message.text !== ''
            }, function () {
                if (_this.state.show) {
                    _this.setShown();
                }
                else {
                    _this.setHidden();
                }
                _this.setDelayTimeout();
            });
        }
    };
    DismissibleAlert.prototype.componentWillUnmount = function () {
        if (this.delayTimeout) {
            clearTimeout(this.delayTimeout);
        }
        if (this.state.show) {
            this.setHidden();
        }
    };
    DismissibleAlert.prototype.onHide = function () {
        if (this.delayTimeout) {
            clearTimeout(this.delayTimeout);
        }
        this.setState({
            show: false,
            message: {}
        }, this.setHidden);
    };
    DismissibleAlert.prototype.setDelayTimeout = function () {
        var _this = this;
        if (!this.state.show) {
            return;
        }
        setTimeout(function () {
            _this.setState({
                message: _this.props.message
            });
        }, 10);
        if (this.delayTimeout) {
            clearTimeout(this.delayTimeout);
        }
        this.delayTimeout = setTimeout(this.onHide, this.getDelayInterval());
    };
    DismissibleAlert.prototype.getDelayInterval = function () {
        var _a = getConfig().dismissibleAlertDuration, dismissibleAlertDuration = _a === void 0 ? DEFAULT_DURATION : _a;
        return this.props.delayInterval === null
            ? dismissibleAlertDuration * 1000
            : this.props.delayInterval;
    };
    DismissibleAlert.prototype.render = function () {
        if (this.getDelayInterval() === DISABLE_DISMISSIBLE_ALERT) {
            return null;
        }
        var _a = this.props, overlayProps = _a.overlayProps, testAutomationId = _a.testAutomationId;
        return (React.createElement(Overlay, __assign({ show: this.state.show, onHide: this.onHide, rootClose: false, placement: "bottom", container: document.body }, overlayProps),
            React.createElement("div", { className: "bootstrap-rc" },
                React.createElement("div", { className: "rc-popup-dismissible-alert" },
                    React.createElement(BSAlert, { className: "rc-popup-dismissible-alert-content alert-dismissible", "data-test-automation-id": testAutomationId },
                        React.createElement("button", { "aria-hidden": "true", ref: "dismissCloseButton", className: "close", "data-dismiss": "alert", onClick: this.onHide, "data-test-automation-id": "closeButton" },
                            React.createElement("span", { "aria-hidden": "true" }, '\xD7')),
                        React.createElement("span", { "data-test-automation-id": "dismissibleAlertText" }, this.state.message.text || ''))))));
    };
    DismissibleAlert.propTypes = {
        testAutomationId: PropTypes.string,
        overlayProps: PropTypes.object,
        delayInterval: PropTypes.number,
        message: PropTypes.shape({
            text: PropTypes.string.isRequired
        }).isRequired,
        onShow: PropTypes.func,
        onHide: PropTypes.func
    };
    DismissibleAlert.defaultProps = {
        testAutomationId: 'dismissibleAlert',
        overlayProps: {},
        delayInterval: null,
        onShow: null,
        onHide: null,
        message: {
            text: ''
        }
    };
    return DismissibleAlert;
}(React.Component));
export { DismissibleAlert };
//# sourceMappingURL=DismissibleAlert.js.map