'use strict';

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

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

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 _classnames = _interopRequireDefault(require("classnames"));

var _Icon = require("./Icon");

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

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

var _utils2 = 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 =
/*#__PURE__*/
function (_React$PureComponent) {
  (0, _inheritsLoose2.default)(Notification, _React$PureComponent);

  function Notification(props, context) {
    var _this;

    _this = _React$PureComponent.call(this, props, context) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "showNotification", function () {
      if (_this.getTimeout() !== DISABLE_NOTIFICATION) {
        var height = (0, _utils.getElementHeight)(_this.refs.notification, true);

        _this.setState({
          height: height,
          open: true,
          shouldRenderChildren: false
        }, _this.startCloseTimeout);
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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);
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "stopCloseTimeout", function () {
      clearTimeout(_this.closeTimeout);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onClose", function () {
      if (_this.waitForTestAutomation()) {
        _EventBus.default.emit(_utils2.constants.onTestAutomationReady);
      }

      if (_this.props.onClose) {
        _this.props.onClose();
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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;
  }

  var _proto = Notification.prototype;

  _proto.componentDidMount = function componentDidMount() {
    if (this.waitForTestAutomation()) {
      _EventBus.default.emit(_utils2.constants.onTestAutomationAwait);
    }

    setTimeout(this.showNotification, OPEN_TIMEOUT);
  };

  _proto.componentWillReceiveProps = function componentWillReceiveProps(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();
    }
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    this.stopCloseTimeout();
  };

  _proto.waitForTestAutomation = function waitForTestAutomation() {
    var timeout = this.getTimeout();
    return this.context.isTopFixedNotification && timeout > 0;
  };

  _proto.getTimeout = function getTimeout() {
    var _getConfig = (0, _utils2.getConfig)(),
        _getConfig$dismissibl = _getConfig.dismissibleAlertDuration,
        dismissibleAlertDuration = _getConfig$dismissibl === void 0 ? DEFAULT_TIMEOUT : _getConfig$dismissibl;

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

  _proto.render = function render() {
    if (this.getTimeout() === DISABLE_NOTIFICATION) {
      return null;
    }

    var _this$props = this.props,
        success = _this$props.success,
        warning = _this$props.warning,
        error = _this$props.error,
        danger = _this$props.danger,
        className = _this$props.className,
        style = _this$props.style,
        testAutomationId = _this$props.testAutomationId,
        children = _this$props.children;
    var wrapperClassName = (0, _classnames.default)('notification-wrapper', {
      'notification-open': this.state.open
    });
    var notificationClassName = (0, _classnames.default)(className, 'notification', {
      'notification-success': success && !warning && !error && !danger,
      'notification-warning': warning && !error && !danger,
      'notification-error': error && !danger,
      'notification-danger': danger
    });
    return _react.default.createElement("div", {
      className: wrapperClassName,
      style: {
        height: this.state.height
      }
    }, _react.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.default.createElement("span", {
      className: "notification-close-button",
      onClick: this.close,
      "aria-hidden": "true",
      "data-test-automation-id": "closeButton"
    }, _react.default.createElement(_Icon.Icon, {
      name: "close",
      testAutomationId: null
    })), _react.default.createElement("span", {
      "data-test-automation-id": "text"
    }, this.state.shouldRenderChildren ? children : null)));
  };

  return Notification;
}(_react.default.PureComponent);

exports.Notification = Notification;
(0, _defineProperty2.default)(Notification, "propTypes", {
  success: _propTypes.default.bool,
  warning: _propTypes.default.bool,
  error: _propTypes.default.bool,
  danger: _propTypes.default.bool,
  timeout: _propTypes.default.number,
  className: _propTypes.default.string,
  style: _propTypes.default.object,
  testAutomationId: _propTypes.default.string,
  children: _propTypes.default.node,
  onClose: _propTypes.default.func
});
(0, _defineProperty2.default)(Notification, "defaultProps", {
  success: false,
  warning: false,
  error: false,
  danger: false,
  timeout: null,
  className: '',
  style: {},
  testAutomationId: 'notification',
  children: null,
  onClose: null
});
(0, _defineProperty2.default)(Notification, "contextTypes", {
  isTopFixedNotification: _propTypes.default.bool
});