'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _recompose = require('recompose');

var _AlertGroup = require('./styles/AlertGroup');

var _AlertGroup2 = _interopRequireDefault(_AlertGroup);

var _reactCollapse = require('react-collapse');

var _AlertCloseButton = require('./styles/AlertCloseButton');

var _AlertCloseButton2 = _interopRequireDefault(_AlertCloseButton);

var _AlertIcon = require('./styles/AlertIcon');

var _AlertIcon2 = _interopRequireDefault(_AlertIcon);

var _AlertBorder = require('./styles/AlertBorder');

var _AlertBorder2 = _interopRequireDefault(_AlertBorder);

var _AlertText = require('./styles/AlertText');

var _AlertText2 = _interopRequireDefault(_AlertText);

var _AlertContent = require('./styles/AlertContent');

var _AlertContent2 = _interopRequireDefault(_AlertContent);

var _icons = require('../Icon/icons');

var _icons2 = _interopRequireDefault(_icons);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var getIcon = function getIcon(type) {
  switch (type) {
    case 'error':
      return '!';
    case 'success':
      return (0, _icons2.default)('checkmark');
    default:
      return 'i';
  }
};

/**
 * Alerts are used to present messages to the user. Every alert is an interruption, so
 * you should use alerts only when they provide real value!
 */

var Alert = function (_React$PureComponent) {
  (0, _inherits3.default)(Alert, _React$PureComponent);

  function Alert() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, Alert);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Alert.__proto__ || (0, _getPrototypeOf2.default)(Alert)).call.apply(_ref, [this].concat(args))), _this), _this.closeTimeoutRef = null, _this.closingTimeoutRef = null, _this.state = {
      closing: false
    }, _this.getCloseButtonType = function () {
      var _this$props = _this.props,
          CloseButton = _this$props.CloseButton,
          type = _this$props.type;

      switch (type) {
        case 'error':
          return CloseButton.Negative || CloseButton;
        case 'success':
          return CloseButton.Positive || CloseButton;
        default:
          return CloseButton;
      }
    }, _this.startClose = function () {
      _this.setState({ closing: true });
      _this.closingTimeoutRef = setTimeout(_this.completeClose, 200);
    }, _this.completeClose = function () {
      _this.props.onClose();
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(Alert, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _props = this.props,
          closeTimeout = _props.closeTimeout,
          onClose = _props.onClose;

      if (closeTimeout && onClose) {
        this.closeTimeoutRef = setTimeout(this.startClose, closeTimeout);
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      if (this.closeTimeoutRef) {
        clearTimeout(this.closeTimeoutRef);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          Border = _props2.Border,
          Text = _props2.Text,
          Icon = _props2.Icon,
          Content = _props2.Content,
          type = _props2.type,
          textOnly = _props2.textOnly,
          children = _props2.children,
          onClose = _props2.onClose,
          rest = (0, _objectWithoutProperties3.default)(_props2, ['Border', 'Text', 'Icon', 'Content', 'type', 'textOnly', 'children', 'onClose']);
      var closing = this.state.closing;


      var CloseButton = this.getCloseButtonType();

      return _react2.default.createElement(
        Border,
        (0, _extends3.default)({ type: type, closing: closing }, rest),
        _react2.default.createElement(
          _reactCollapse.UnmountClosed,
          {
            isOpened: !closing,
            forceInitialAnimation: true,
            springConfig: { stiffness: 300, damping: 20 }
          },
          _react2.default.createElement(
            Content,
            null,
            !textOnly && _react2.default.createElement(Icon, { type: type }),
            _react2.default.createElement(
              Text,
              null,
              children
            ),
            onClose && _react2.default.createElement(CloseButton, { icon: 'delete', onClick: this.startClose })
          )
        )
      );
    }
  }]);
  return Alert;
}(_react2.default.PureComponent);

Alert.propTypes = {
  /**
   * An alert type
   */
  type: _propTypes2.default.oneOf(['info', 'success', 'error']),
  /**
   * Specify a CSS value or an object { top, right, bottom, left } or { vertical, horizontal } to
   * control the spacing around the heading. Defaults to a large space below the element.
   */
  spacing: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.object]),
  /**
   * Whether to render only text (true) or an icon alongside text (false).
   */
  textOnly: _propTypes2.default.bool,
  /**
   * A callback for when this alert is closed (dismissed). Providing this prop will
   * automatically create a close button for the user.
   */
  onClose: _propTypes2.default.func,
  /**
   * A timeout for closing the alert (ms). Only works if `onClose` is also provided. Will
   * call `onClose` after time specified.
   */
  closeTimeout: _propTypes2.default.number,
  /**
   * Additional class name to pass to the alert.
   */
  className: _propTypes2.default.string,
  /**
   * Additional id to pass to the alert.
   */
  id: _propTypes2.default.string,
  /**
   * A component to render the border container
   */
  Border: _propTypes2.default.func,
  /**
   * A component to render the text inside
   */
  Text: _propTypes2.default.func,
  /**
   * A component to render the icon
   */
  Icon: _propTypes2.default.func,
  /**
   * A containing element around the alert, used for expanding style calc.
   */
  Content: _propTypes2.default.func,
  /**
   * The anchor component to use when rendering a close button
   */
  CloseButton: _propTypes2.default.func
};
Alert.defaultProps = {
  type: 'info',
  textOnly: false,
  className: null,
  id: null,
  spacing: 0,
  Border: _AlertBorder2.default,
  Content: _AlertContent2.default,
  Text: _AlertText2.default,
  Icon: _AlertIcon2.default,
  CloseButton: _AlertCloseButton2.default
};
Alert.Small = (0, _recompose.withProps)({
  Border: _AlertBorder2.default.Small,
  Icon: _AlertIcon2.default.Small,
  Content: _AlertContent2.default.Small
})(Alert);
Alert.Group = _AlertGroup2.default;
exports.default = Alert;