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, { Component } from 'react';
import PropTypes from 'prop-types';
import shortid from 'shortid';

import UtilityPanel from '../UtilityPanel';
import Icon from '../../base/Icon';
import './styles.css';

var UtilityNav = function (_Component) {
  _inherits(UtilityNav, _Component);

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

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _this.state = {
      navSelected: -1,
      isOpen: _this.props.isOpen
    };
    _this.onClick = _this.onClick.bind(_this);
    _this.ident = shortid.generate();
    return _this;
  }

  UtilityNav.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var isOpen = nextProps.isOpen;

    this.setState({ isOpen: isOpen, navSelected: -1 });
  };

  UtilityNav.prototype.onClick = function onClick(divId, e) {
    e.preventDefault();
    this.setState({
      navSelected: this.state.navSelected === -1 ? divId : -1,
      isOpen: true
    });
  };

  UtilityNav.prototype.render = function render() {
    var _this2 = this;

    var navSelected = this.state.navSelected;
    var _props = this.props,
        googleLanguages = _props.googleLanguages,
        items = _props.items;

    return React.createElement(
      'div',
      { className: 'ma__utility-nav js-util-nav' },
      React.createElement(
        'ul',
        { className: 'ma__utility-nav__items' },
        googleLanguages && React.createElement(GoogleLanguages, null),
        items.map(function (item, itemIndex) {
          var newItem = Object.assign({}, item);
          newItem.navSelected = navSelected;
          // Use utility nav ident to make unique item ids.
          newItem.navIdent = _this2.ident;
          var isOpen = _this2.state.isOpen;

          return item.panel ?
          /* eslint-disable react/no-array-index-key */
          React.createElement(NavItem, { handleClick: _this2.onClick, data: newItem, key: 'navItem.' + itemIndex, index: itemIndex, isOpen: isOpen }) : React.createElement(NavItemLink, { key: 'navItem.' + itemIndex, data: item });
        })
      )
    );
  };

  return UtilityNav;
}(Component);

var GoogleLanguages = function GoogleLanguages() {
  return React.createElement(
    'li',
    { key: 'li.googleLanguage', className: 'ma__utility-nav__item' },
    React.createElement(
      'div',
      { className: 'ma__utility-nav__translate' },
      React.createElement('div', { id: 'google_translate_element' }),
      React.createElement(
        'div',
        { className: 'ma__utility-nav__translate-icon' },
        React.createElement(Icon, { name: 'latlonglobe' })
      )
    )
  );
};

var NavItem = function NavItem(obj) {
  var item = obj.data;
  var divId = 'nav-content-' + item.navIdent + '-' + obj.index;
  var oneIsOpen = obj.isOpen;
  var thisIsOpen = item.navSelected === divId;
  var isExpanded = oneIsOpen && thisIsOpen ? 'is-open' : 'is-closed';
  var divProps = {
    className: 'ma__utility-nav__content js-util-nav-content ' + isExpanded,
    'aria-hidden': isExpanded ? 'false' : 'true',
    id: divId
  };
  var iconProps = {
    name: item.icon,
    ariaHidden: true
  };
  return React.createElement(
    'li',
    { className: 'ma__utility-nav__item js-util-nav-toggle' },
    React.createElement(
      'button',
      { onClick: function onClick(e) {
          return obj.handleClick(divId, e);
        }, className: 'ma__utility-nav__link ' + isExpanded, href: '#', 'aria-label': item.ariaLabelText || item.text },
      React.createElement(Icon, iconProps),
      React.createElement(
        'span',
        null,
        item.text
      )
    ),
    React.createElement(
      'div',
      divProps,
      React.createElement(
        'div',
        { className: 'ma__utility-nav__container' },
        React.createElement(
          'div',
          { className: 'ma__utility-nav__content-title' },
          React.createElement(
            'button',
            { onClick: function onClick(e) {
                return obj.handleClick(divId, e);
              }, className: 'ma__utility-nav__close js-close-util-nav' },
            React.createElement(
              'span',
              null,
              item.closeText
            ),
            React.createElement(
              'span',
              { className: 'ma__utility-nav__close-icon', 'aria-hidden': 'true' },
              '+'
            )
          ),
          React.createElement(Icon, iconProps),
          React.createElement(
            'span',
            null,
            item.text
          )
        ),
        React.createElement(
          'div',
          { className: 'ma__utility-nav__content-body' },
          React.createElement(UtilityPanel, item.panel)
        )
      )
    )
  );
};

var NavItemLink = function NavItemLink(obj) {
  var item = obj.data;
  var iconProps = {
    name: item.icon,
    ariaHidden: true
  };
  return React.createElement(
    'li',
    { className: 'ma__utility-nav__item js-util-nav-toggle' },
    React.createElement(
      'a',
      { className: 'ma__utility-nav__link', href: item.href, 'aria-label': item.ariaLabelText || item.text },
      React.createElement(Icon, iconProps),
      React.createElement(
        'span',
        null,
        item.text
      )
    )
  );
};

UtilityNav.propTypes = process.env.NODE_ENV !== "production" ? {
  /** Boolean that controls when to show the google language dom. */
  googleLanguages: PropTypes.bool,
  /** An array of navigation items to display to the user. */
  items: PropTypes.arrayOf(PropTypes.shape({
    /** The text to display for the main navigation item. */
    text: PropTypes.string.isRequired,
    /** Defines the label to use with aria-label. */
    ariaLabelText: PropTypes.string,
    /** The icon to display to the left of text. */
    icon: PropTypes.oneOf(['building', 'login']),
    /** The href for the link if not a button. */
    href: PropTypes.string,
    /** The text to use on the close link. */
    closeText: PropTypes.string,
    /** Displays an utility panel when text is clicked. */
    panel: PropTypes.shape(UtilityPanel.propTypes)
  })),
  /** Boolean that controls if any UtilityNav div should be should be open on mobile. */
  isOpen: PropTypes.bool
} : {};

export default UtilityNav;