import { jsx as ___EmotionJSX } from "@emotion/core";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/core';
import CheckmarkIcon from '../../svgs/icons/checkmark-icon.svg';
import ErrorIcon from '../../svgs/icons/error-icon.svg';
import InfoIcon from '../../svgs/icons/info-icon.svg';
import { AlertsContainer, AlertContainer, AlertContentContainer, alertIconStyles } from './style';
var ANIMATION_DELAY = 500;
var alertIconMapping = {
  success: CheckmarkIcon,
  danger: ErrorIcon,
  info: InfoIcon
};

var Alert =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Alert, _React$Component);

  function Alert() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Alert);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Alert)).call.apply(_getPrototypeOf2, [this].concat(args)));
    _this.state = {
      exiting: false,
      exited: false
    };

    _this.exit = function () {
      var _this$props = _this.props,
          onExit = _this$props.onExit,
          rest = _objectWithoutProperties(_this$props, ["onExit"]);

      _this.setState({
        exiting: true
      }); // eslint-disable-next-line no-undef


      window.clearTimeout(_this.timer); // eslint-disable-next-line no-undef

      window.setTimeout(function () {
        onExit(_objectSpread({}, rest));

        _this.setState({
          exited: true
        });
      }, ANIMATION_DELAY);
    };

    return _this;
  }

  _createClass(Alert, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var duration = this.props.duration;

      if (duration !== 'sticky') {
        // eslint-disable-next-line no-undef
        this.timer = window.setTimeout(this.exit, Number(duration) * 1000 - ANIMATION_DELAY);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.timer) {
        // eslint-disable-next-line no-undef
        window.clearTimeout(this.timer);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          text = _this$props2.text,
          type = _this$props2.type,
          onExit = _this$props2.onExit,
          rest = _objectWithoutProperties(_this$props2, ["text", "type", "onExit"]);

      var _this$state = this.state,
          exiting = _this$state.exiting,
          exited = _this$state.exited;
      var Icon = alertIconMapping[type];

      if (exited) {
        return null;
      }

      return ___EmotionJSX(AlertContainer, _extends({
        alertType: type,
        exiting: exiting,
        onClick: this.exit
      }, rest), ___EmotionJSX(AlertContentContainer, null, ___EmotionJSX(Icon, {
        css:
        /*#__PURE__*/
        css(alertIconStyles, ";label:Alert;" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9zaGFyZWQtY29tcG9uZW50cy9hbGVydC9pbmRleC5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFzRm9CIiwiZmlsZSI6Ii4uLy4uLy4uL3NyYy9za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cblxuICAgIC8vIGVzbGludC1kaXNhYmxlLW5leHQtbGluZSBuby11bmRlZlxuICAgIHdpbmRvdy5zZXRUaW1lb3V0KCgpID0+IHtcbiAgICAgIG9uRXhpdCh7IC4uLnJlc3QgfSk7XG4gICAgICB0aGlzLnNldFN0YXRlKHsgZXhpdGVkOiB0cnVlIH0pO1xuICAgIH0sIEFOSU1BVElPTl9ERUxBWSk7XG4gIH07XG5cbiAgcmVuZGVyKCkge1xuICAgIGNvbnN0IHsgdGV4dCwgdHlwZSwgb25FeGl0LCAuLi5yZXN0IH0gPSB0aGlzLnByb3BzO1xuICAgIGNvbnN0IHsgZXhpdGluZywgZXhpdGVkIH0gPSB0aGlzLnN0YXRlO1xuICAgIGNvbnN0IEljb24gPSBhbGVydEljb25NYXBwaW5nW3R5cGVdO1xuXG4gICAgaWYgKGV4aXRlZCkgeyByZXR1cm4gbnVsbDsgfVxuXG4gICAgcmV0dXJuIChcbiAgICAgIDxBbGVydENvbnRhaW5lclxuICAgICAgICBhbGVydFR5cGU9e3R5cGV9XG4gICAgICAgIGV4aXRpbmc9e2V4aXRpbmd9XG4gICAgICAgIG9uQ2xpY2s9e3RoaXMuZXhpdH1cbiAgICAgICAgey4uLnJlc3R9XG4gICAgICA+XG4gICAgICAgIDxBbGVydENvbnRlbnRDb250YWluZXI+XG4gICAgICAgICAgPEljb25cbiAgICAgICAgICAgIGNzcz17Y3NzYCR7YWxlcnRJY29uU3R5bGVzfTtgfVxuICAgICAgICAgICAgZmlsbD1cImN1cnJlbnRDb2xvclwiXG4gICAgICAgICAgLz5cbiAgICAgICAgICB7dGV4dH1cbiAgICAgICAgPC9BbGVydENvbnRlbnRDb250YWluZXI+XG4gICAgICA8L0FsZXJ0Q29udGFpbmVyPlxuICAgICk7XG4gIH1cbn1cblxuZXhwb3J0IGRlZmF1bHQgQWxlcnQ7XG4iXX0= */")),
        fill: "currentColor"
      }), text));
    }
  }]);

  return Alert;
}(React.Component);

Alert.Container = function (_ref) {
  var children = _ref.children;
  return ___EmotionJSX(AlertsContainer, null, children);
};

Alert.propTypes = {
  text: PropTypes.string.isRequired,
  type: PropTypes.oneOf(['success', 'danger', 'info']).isRequired,
  duration: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
  onExit: PropTypes.func.isRequired
};
export default Alert;