'use strict';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.DismissibleAlert = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Alert = _interopRequireDefault(require("react-bootstrap/lib/Alert"));

var _Overlay = _interopRequireDefault(require("react-bootstrap/lib/Overlay"));

var _EventBus = _interopRequireDefault(require("../utils/EventBus"));

var _utils = require("../../utils");

var DISABLE_DISMISSIBLE_ALERT = 0;
var DEFAULT_DURATION = 3;

var DismissibleAlert =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(DismissibleAlert, _React$Component);

  function DismissibleAlert(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setShown", function () {
      _EventBus.default.emit(_utils.constants.onTestAutomationAwait);

      if (_this.props.onShow) {
        _this.props.onShow();
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setHidden", function () {
      _EventBus.default.emit(_utils.constants.onTestAutomationReady);

      if (_this.props.onHide) {
        _this.props.onHide();
      }
    });
    _this.state = {
      show: props.message.text !== '',
      message: {}
    };
    _this.onHide = _this.onHide.bind((0, _assertThisInitialized2.default)(_this));
    return _this;
  }

  var _proto = DismissibleAlert.prototype;

  _proto.componentDidMount = function componentDidMount() {
    this.setDelayTimeout();
  };

  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var _this2 = this;

    if (nextProps.message !== this.props.message) {
      this.setState({
        show: nextProps.message.text !== ''
      }, function () {
        if (_this2.state.show) {
          _this2.setShown();
        } else {
          _this2.setHidden();
        }

        _this2.setDelayTimeout();
      });
    }
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    if (this.delayTimeout) {
      clearTimeout(this.delayTimeout);
    }

    if (this.state.show) {
      this.setHidden();
    }
  };

  _proto.onHide = function onHide() {
    if (this.delayTimeout) {
      clearTimeout(this.delayTimeout);
    }

    this.setState({
      show: false,
      message: {}
    }, this.setHidden);
  };

  _proto.setDelayTimeout = function setDelayTimeout() {
    var _this3 = this;

    if (!this.state.show) {
      return;
    }

    setTimeout(function () {
      _this3.setState({
        message: _this3.props.message
      });
    }, 10);

    if (this.delayTimeout) {
      clearTimeout(this.delayTimeout);
    }

    this.delayTimeout = setTimeout(this.onHide, this.getDelayInterval());
  };

  _proto.getDelayInterval = function getDelayInterval() {
    var _getConfig = (0, _utils.getConfig)(),
        _getConfig$dismissibl = _getConfig.dismissibleAlertDuration,
        dismissibleAlertDuration = _getConfig$dismissibl === void 0 ? DEFAULT_DURATION : _getConfig$dismissibl;

    return this.props.delayInterval === null ? dismissibleAlertDuration * 1000 : this.props.delayInterval;
  };

  _proto.render = function render() {
    if (this.getDelayInterval() === DISABLE_DISMISSIBLE_ALERT) {
      return null;
    }

    var _this$props = this.props,
        overlayProps = _this$props.overlayProps,
        testAutomationId = _this$props.testAutomationId;
    return _react.default.createElement(_Overlay.default, (0, _extends2.default)({
      show: this.state.show,
      onHide: this.onHide,
      rootClose: false,
      placement: "bottom",
      container: document.body
    }, overlayProps), _react.default.createElement("div", {
      className: "bootstrap-rc"
    }, _react.default.createElement("div", {
      className: "rc-popup-dismissible-alert"
    }, _react.default.createElement(_Alert.default, {
      className: "rc-popup-dismissible-alert-content alert-dismissible",
      "data-test-automation-id": testAutomationId
    }, _react.default.createElement("button", {
      "aria-hidden": "true",
      ref: "dismissCloseButton",
      className: "close",
      "data-dismiss": "alert",
      onClick: this.onHide,
      "data-test-automation-id": "closeButton"
    }, _react.default.createElement("span", {
      "aria-hidden": "true"
    }, '\xD7')), _react.default.createElement("span", {
      "data-test-automation-id": "dismissibleAlertText"
    }, this.state.message.text || '')))));
  };

  return DismissibleAlert;
}(_react.default.Component);

exports.DismissibleAlert = DismissibleAlert;
(0, _defineProperty2.default)(DismissibleAlert, "propTypes", {
  testAutomationId: _propTypes.default.string,
  overlayProps: _propTypes.default.object,
  delayInterval: _propTypes.default.number,
  message: _propTypes.default.shape({
    text: _propTypes.default.string.isRequired
  }).isRequired,
  onShow: _propTypes.default.func,
  onHide: _propTypes.default.func
});
(0, _defineProperty2.default)(DismissibleAlert, "defaultProps", {
  testAutomationId: 'dismissibleAlert',
  overlayProps: {},
  delayInterval: null,
  onShow: null,
  onHide: null,
  message: {
    text: ''
  }
});