'use strict';

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

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _style = require('./style.scss');

var _style2 = _interopRequireDefault(_style);

var _bind = require('classnames/bind');

var _bind2 = _interopRequireDefault(_bind);

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

var _Icon2 = _interopRequireDefault(_Icon);

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

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var Alert = function (_React$Component) {
  _inherits(Alert, _React$Component);

  function Alert(props) {
    _classCallCheck(this, Alert);

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Alert).call(this, props));

    _this.state = {
      timerStart: 0,
      timeout: _this.props.timeout,
      timer: false
    };

    _this.startTimer = function () {
      if (_this.props.timeout) {
        _this.setState({ timerStart: new Date(), timer: setTimeout(_this.closeAlert, _this.state.timeout) });
      }
    };

    _this.pauseTimer = function () {
      clearTimeout(_this.state.timer);
      var timeout = _this.state.timeout;
      timeout -= new Date() - _this.state.timerStart;

      _this.setState({ timeout: timeout });
    };

    _this.closeAlert = function () {
      if (_this.state.timer) {
        clearTimeout(_this.state.timer);
      }
      _this.props.onClose();
    };

    _this.componentDidMount = function () {
      _this.startTimer();
    };

    return _this;
  }

  _createClass(Alert, [{
    key: 'render',
    value: function render() {
      var cx = _bind2.default.bind(_style2.default);
      var alertClasses = cx(_style2.default.alert, this.props.optClass, this.props.type, this.props.closable ? 'closable' : '');
      var alertIcons = {
        success: 'icon-check-circle-2-1',
        warning: 'icon-alert-1',
        info: 'icon-information',
        danger: 'icon-delete-3'
      };

      return _react2.default.createElement(
        'div',
        { className: alertClasses, onMouseOver: this.pauseTimer, onMouseOut: this.startTimer },
        this.props.closable ? _react2.default.createElement(
          'div',
          { className: _style2.default['close-icon'], onClick: this.closeAlert },
          _react2.default.createElement(_Icon2.default, { name: 'icon-delete-1', width: '12', height: '12' })
        ) : null,
        _react2.default.createElement(_Icon2.default, { name: alertIcons[this.props.type], width: '17', height: '17' }),
        _react2.default.createElement(
          'div',
          null,
          this.props.children
        )
      );
    }
  }]);

  return Alert;
}(_react2.default.Component);

Alert.defaultProps = {
  type: 'success'
};
Alert.propTypes = {
  /**
   * The alert type.
   */
  type: _react2.default.PropTypes.oneOf(['success', 'warning', 'info', 'danger']),
  /**
   * Optional styles to add to the alert component.
   */
  optClass: _react2.default.PropTypes.string,
  /**
   * Whether the alert can be closed.
   */
  closable: _react2.default.PropTypes.bool,
  /**
   * How long before the alert disappears.
   */
  timeout: _react2.default.PropTypes.number,
  /**
   * A callback to be triggered when the close icon is clicked or when the timeout expires.
   */
  onClose: _react2.default.PropTypes.func

};
exports.default = Alert;