'use strict';

exports.__esModule = true;

var _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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _is = require('is');

var _is2 = _interopRequireDefault(_is);

var _Collapse = require('../../animations/Collapse');

var _Collapse2 = _interopRequireDefault(_Collapse);

var _ButtonAlert = require('../../atoms/buttons/ButtonAlert');

var _ButtonAlert2 = _interopRequireDefault(_ButtonAlert);

var _EmergencyAlert = require('../../molecules/EmergencyAlert');

var _EmergencyAlert2 = _interopRequireDefault(_EmergencyAlert);

var _EmergencyHeader = require('../../molecules/EmergencyHeader');

var _EmergencyHeader2 = _interopRequireDefault(_EmergencyHeader);

require('./style.css');

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

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

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.handleClick = function (e) {
      var currentTarget = e.currentTarget;

      _this.setState({
        open: !_this.state.open
      });
      if (_is2.default.fn(_this.props.onButtonAlertClick)) {
        _this.props.onButtonAlertClick({ open: !_this.state.open, currentTarget: currentTarget });
      }
    };

    _this.handleClose = function (e) {
      var currentTarget = e.currentTarget;

      _this.setState({
        close: !_this.state.close
      });
      if (_is2.default.fn(_this.props.onButtonCloseClick)) {
        _this.props.onButtonCloseClick({ close: !_this.state.close, currentTarget: currentTarget });
      }
    };

    _this.state = {
      open: false,
      close: false
    };
    return _this;
  }

  EmergencyAlerts.prototype.render = function render() {
    var _classNames, _classNames2, _classNames3, _classNames4, _classNames5;

    var _props = this.props,
        id = _props.id,
        emergencyHeader = _props.emergencyHeader,
        buttonAlert = _props.buttonAlert,
        alerts = _props.alerts,
        theme = _props.theme,
        buttonClose = _props.buttonClose;

    var sectionClasses = (0, _classnames2.default)((_classNames = {
      'ma__emergency-alerts': true
    }, _classNames['ma__emergency-alerts--' + theme] = theme, _classNames));
    var alertsWrapperClasses = (0, _classnames2.default)((_classNames2 = {
      'ma__emergency-alerts__content': true,
      'js-accordion-content': true
    }, _classNames2['ma__emergency-alerts__content--' + theme] = theme, _classNames2));
    var headerClasses = (0, _classnames2.default)((_classNames3 = {
      'ma__emergency-alerts__header': true
    }, _classNames3['ma__emergency-alerts__header--' + theme] = theme, _classNames3));
    var interfaceClasses = (0, _classnames2.default)((_classNames4 = {
      'ma__emergency-alerts__interface': true,
      'js-accordion-link': true
    }, _classNames4['ma__emergency-alerts__interface--' + theme] = theme, _classNames4.open = this.state.open, _classNames4.closed = !this.state.open, _classNames4));
    var hideButtonClasses = (0, _classnames2.default)((_classNames5 = {
      'ma__emergency-alerts__hide': true,
      'js-emergency-alerts-link': true
    }, _classNames5['ma__emergency-alerts__hide--' + theme] = theme, _classNames5));
    return _react2.default.createElement(
      _Collapse2.default,
      { 'in': !this.state.close, dimension: 'height' },
      _react2.default.createElement(
        'section',
        { className: sectionClasses, 'data-id': id },
        _react2.default.createElement(
          'div',
          { className: headerClasses },
          _react2.default.createElement(
            'div',
            { className: 'ma__emergency-alerts__container' },
            emergencyHeader && _react2.default.createElement(_EmergencyHeader2.default, _extends({}, emergencyHeader, { theme: theme })),
            alerts ? _react2.default.createElement(
              'div',
              { className: 'ma__emergency-alerts__header-interface js-accordion-link' },
              buttonAlert && _react2.default.createElement(_ButtonAlert2.default, _extends({}, buttonAlert, { onClick: this.handleClick, isOpen: this.state.open }))
            ) : buttonClose && _react2.default.createElement(
              'button',
              {
                className: hideButtonClasses,
                title: 'hide alert',
                'aria-label': 'hide alert',
                onClick: this.handleClose
              },
              '+'
            )
          )
        ),
        alerts && _react2.default.createElement(
          _react2.default.Fragment,
          null,
          _react2.default.createElement(
            _Collapse2.default,
            { 'in': this.state.open, dimension: 'height' },
            _react2.default.createElement(
              'div',
              { className: alertsWrapperClasses },
              _react2.default.createElement(
                'div',
                { className: 'ma__emergency-alerts__container' },

                /* eslint-disable-next-line react/no-array-index-key */
                alerts.map(function (alert, i) {
                  return _react2.default.createElement(_EmergencyAlert2.default, _extends({}, alert, { theme: theme, key: 'alert-nested--' + i }));
                })
              )
            )
          ),
          _react2.default.createElement(
            'div',
            { className: interfaceClasses },
            buttonAlert && _react2.default.createElement(_ButtonAlert2.default, _extends({}, buttonAlert, { onClick: this.handleClick, isOpen: this.state.open }))
          )
        )
      )
    );
  };

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

EmergencyAlerts.propTypes = process.env.NODE_ENV !== "production" ? {
  /** The data-id of the organism */
  id: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]).isRequired,
  /** A string that controls different color themes for the component. */
  theme: _propTypes2.default.oneOf(['c-warning', 'c-primary-alt', 'c-primary', 'c-gray', 'c-error']),
  /** An on button alert click callback function */
  onButtonAlertClick: _propTypes2.default.func,
  /** An on button close click callback function */
  onButtonCloseClick: _propTypes2.default.func,
  /** The emergency header props */
  emergencyHeader: _propTypes2.default.shape(_EmergencyHeader2.default.propTypes),
  /** The props for the button alert */
  buttonAlert: _propTypes2.default.shape(_ButtonAlert2.default.propTypes),
  /* An array of alter messages */
  alerts: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    /** A message describing the event. */
    message: _propTypes2.default.string.isRequired,
    /** A string representing the time of the event. */
    timeStamp: _propTypes2.default.string,
    /** An optional function whose return value is a link to take the user to page with more information. */
    link: _propTypes2.default.func
  })),
  /** Whether or not to render a close button if not alerts are provided */
  buttonClose: _propTypes2.default.bool
} : {};

EmergencyAlerts.defaultProps = {
  theme: 'c-warning',
  buttonClose: true
};

exports.default = EmergencyAlerts;
module.exports = exports['default'];