'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 _Alert = require('./Alert');

var _Alert2 = _interopRequireDefault(_Alert);

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 AlertSystem = function (_React$Component) {
  _inherits(AlertSystem, _React$Component);

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

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

    _this.state = {
      alerts: _this.props.alerts
    };

    _this.getAlerts = function () {
      return _this.state.alerts.map(function (alert, index) {
        return !alert.hidden ? _react2.default.createElement(
          _Alert2.default,
          { key: alert.key, type: alert.type || 'success', optClass: alert.class || '', closable: typeof alert.closable !== 'undefined' ? alert.closable : true, timeout: alert.timeout, onClose: _this.removeAlert.bind(_this, alert.key) },
          alert.content
        ) : null;
      });
    };

    _this.removeAlert = function (key) {
      var alerts = _this.state.alerts;

      alerts.map(function (alert, index) {
        if (alert.key === key) {
          alert.hidden = true;
        }
      });

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

    _this.componentWillReceiveProps = function (nextProps) {
      var alerts = nextProps.alerts;
      alerts.map(function (alert, index) {
        if (!alert.key) {
          alert.key = (alert.type || 'success') + '-' + new Date().getTime();
        }
      });

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

    return _this;
  }

  _createClass(AlertSystem, [{
    key: 'render',
    value: function render() {
      var cx = _bind2.default.bind(_style2.default);
      var alertSystemClasses = cx(_style2.default['alert-system'], this.props.optClass);

      return _react2.default.createElement(
        'div',
        { className: alertSystemClasses },
        this.getAlerts()
      );
    }
  }]);

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

AlertSystem.propTypes = {
  /**
   * The alerts to display.
   */
  alerts: _react2.default.PropTypes.array.isRequired,
  /**
   * Optional styles to add to the alert system component.
   */
  optClass: _react2.default.PropTypes.string
};
exports.default = AlertSystem;