'use strict';

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 classnames from 'classnames';
import { Icon } from './Icon';
import EventBus from '../utils/EventBus';
import { getElementHeight } from './utils';
import { getConfig, constants } from '../../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;
export var Notification =
/*#__PURE__*/
function (_React$PureComponent) {
  _inheritsLoose(Notification, _React$PureComponent);

  function Notification(props, context) {
    var _this;

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

    _defineProperty(_assertThisInitialized(_this), "showNotification", function () {
      if (_this.getTimeout() !== DISABLE_NOTIFICATION) {
        var height = getElementHeight(_this.refs.notification, true);

        _this.setState({
          height: height,
          open: true,
          shouldRenderChildren: false
        }, _this.startCloseTimeout);
      }
    });

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

    _defineProperty(_assertThisInitialized(_this), "stopCloseTimeout", function () {
      clearTimeout(_this.closeTimeout);
    });

    _defineProperty(_assertThisInitialized(_this), "onClose", function () {
      if (_this.waitForTestAutomation()) {
        EventBus.emit(constants.onTestAutomationReady);
      }

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

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

  return Notification;
}(React.PureComponent);

_defineProperty(Notification, "propTypes", {
  success: PropTypes.bool,
  warning: PropTypes.bool,
  error: PropTypes.bool,
  danger: PropTypes.bool,
  timeout: PropTypes.number,
  className: PropTypes.string,
  style: PropTypes.object,
  testAutomationId: PropTypes.string,
  children: PropTypes.node,
  onClose: PropTypes.func
});

_defineProperty(Notification, "defaultProps", {
  success: false,
  warning: false,
  error: false,
  danger: false,
  timeout: null,
  className: '',
  style: {},
  testAutomationId: 'notification',
  children: null,
  onClose: null
});

_defineProperty(Notification, "contextTypes", {
  isTopFixedNotification: PropTypes.bool
});