'use strict';

import _extends from "@babel/runtime/helpers/extends";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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;
export var DismissibleAlert =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(DismissibleAlert, _React$Component);

  function DismissibleAlert(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;

    _defineProperty(_assertThisInitialized(_this), "setShown", function () {
      EventBus.emit(constants.onTestAutomationAwait);

      if (_this.props.onShow) {
        _this.props.onShow();
      }
    });

    _defineProperty(_assertThisInitialized(_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(_assertThisInitialized(_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 = 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.createElement(Overlay, _extends({
      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 || '')))));
  };

  return DismissibleAlert;
}(React.Component);

_defineProperty(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
});

_defineProperty(DismissibleAlert, "defaultProps", {
  testAutomationId: 'dismissibleAlert',
  overlayProps: {},
  delayInterval: null,
  onShow: null,
  onHide: null,
  message: {
    text: ''
  }
});