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

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

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import is from 'is';
import Collapse from '../../animations/Collapse';
import ButtonAlert from '../../atoms/buttons/ButtonAlert';
import EmergencyAlert from '../../molecules/EmergencyAlert';
import EmergencyHeader from '../../molecules/EmergencyHeader';
import './style.css';

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 (is.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 (is.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 = classNames((_classNames = {
      'ma__emergency-alerts': true
    }, _classNames['ma__emergency-alerts--' + theme] = theme, _classNames));
    var alertsWrapperClasses = classNames((_classNames2 = {
      'ma__emergency-alerts__content': true,
      'js-accordion-content': true
    }, _classNames2['ma__emergency-alerts__content--' + theme] = theme, _classNames2));
    var headerClasses = classNames((_classNames3 = {
      'ma__emergency-alerts__header': true
    }, _classNames3['ma__emergency-alerts__header--' + theme] = theme, _classNames3));
    var interfaceClasses = classNames((_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 = classNames((_classNames5 = {
      'ma__emergency-alerts__hide': true,
      'js-emergency-alerts-link': true
    }, _classNames5['ma__emergency-alerts__hide--' + theme] = theme, _classNames5));
    return React.createElement(
      Collapse,
      { 'in': !this.state.close, dimension: 'height' },
      React.createElement(
        'section',
        { className: sectionClasses, 'data-id': id },
        React.createElement(
          'div',
          { className: headerClasses },
          React.createElement(
            'div',
            { className: 'ma__emergency-alerts__container' },
            emergencyHeader && React.createElement(EmergencyHeader, _extends({}, emergencyHeader, { theme: theme })),
            alerts ? React.createElement(
              'div',
              { className: 'ma__emergency-alerts__header-interface js-accordion-link' },
              buttonAlert && React.createElement(ButtonAlert, _extends({}, buttonAlert, { onClick: this.handleClick, isOpen: this.state.open }))
            ) : buttonClose && React.createElement(
              'button',
              {
                className: hideButtonClasses,
                title: 'hide alert',
                'aria-label': 'hide alert',
                onClick: this.handleClose
              },
              '+'
            )
          )
        ),
        alerts && React.createElement(
          React.Fragment,
          null,
          React.createElement(
            Collapse,
            { 'in': this.state.open, dimension: 'height' },
            React.createElement(
              'div',
              { className: alertsWrapperClasses },
              React.createElement(
                'div',
                { className: 'ma__emergency-alerts__container' },

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

  return EmergencyAlerts;
}(React.Component);

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

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

export default EmergencyAlerts;