function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

import React from 'react';
import PropTypes from 'prop-types';
import { Icon, ListSeparator } from "./..";
/**
 * Topbar Mobile
 * @param props
 */

var TopbarMobile =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(TopbarMobile, _React$Component);

  function TopbarMobile() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      isMobileOpen: false
    };

    _this.handleClose = function () {
      _this.setState({
        isMobileOpen: false
      });
    };

    _this.handleOpen = function () {
      _this.setState({
        isMobileOpen: true
      });
    };

    _this.handleKeyDown = function (e) {
      if (e.which === 32 || e.which === 13 || e.charCode === 32 || e.charCode === 13) {
        _this.handleClose();

        e.preventDefault();
      }
    };

    return _this;
  }

  var _proto = TopbarMobile.prototype;

  _proto.render = function render() {
    var _this2 = this;

    var _this$props = this.props,
        brandNode = _this$props.brandNode,
        children = _this$props.children,
        closeMenuAriaLabel = _this$props.closeMenuAriaLabel,
        shouldCloseOnClick = _this$props.shouldCloseOnClick,
        openMenuAriaLabel = _this$props.openMenuAriaLabel;
    var isMobileOpen = this.state.isMobileOpen;
    var mobileButton = React.createElement(Icon, {
      name: "list-menu_20",
      buttonClassName: "cui-top-bar__mobile-menu-button",
      onClick: this.handleOpen,
      ariaLabel: openMenuAriaLabel,
      "aria-pressed": isMobileOpen
    });
    var passClickHandlerToChildren = React.Children.map(children, function (child) {
      if (!child) return;
      return React.cloneElement(child, {
        onClick: _this2.handleClose
      });
    });
    return React.createElement("div", null, !isMobileOpen && mobileButton, React.createElement("div", {
      className: 'cui-top-bar__mobile cui-tb-mobile' + ("" + (isMobileOpen ? ' open' : '')),
      onClick: function onClick() {
        return shouldCloseOnClick ? _this2.handleClose : null;
      },
      onKeyDown: this.handleKeyDown,
      role: "menu",
      tabIndex: 0
    }, React.createElement(Icon, {
      name: "cancel_20",
      buttonClassName: "cui-tb-mobile__close",
      "aria-pressed": isMobileOpen,
      onClick: this.handleClose,
      ariaLabel: closeMenuAriaLabel
    }), React.createElement("span", {
      onClick: this.handleClose,
      onKeyDown: this.handleKeyDown
    }, brandNode), React.createElement(ListSeparator, null), React.createElement("nav", {
      className: "cui-tb-mobile__nav"
    }, !shouldCloseOnClick && passClickHandlerToChildren || children)), React.createElement("div", {
      className: 'cui-tb-mobile__mask' + ("" + (isMobileOpen ? ' open' : '')),
      onClick: this.handleClose,
      role: "none"
    }));
  };

  return TopbarMobile;
}(React.Component);

TopbarMobile.displayName = 'TopbarMobile';
TopbarMobile.propTypes = {
  /** @prop Brand Node | null */
  brandNode: PropTypes.node,

  /** @prop Children node to render inside of TopbarMobile | null */
  children: PropTypes.node,

  /** @prop Aria Label for close Button | 'Close Menu' */
  closeMenuAriaLabel: PropTypes.string,

  /** @prop Set mobile menu to close on any click | true */
  shouldCloseOnClick: PropTypes.bool,

  /** @prop Aria Label for open Button | 'Open Menu */
  openMenuAriaLabel: PropTypes.string
};
TopbarMobile.defaultProps = {
  brandNode: null,
  children: null,
  closeMenuAriaLabel: 'Close Menu',
  shouldCloseOnClick: true,
  openMenuAriaLabel: 'Open Menu'
};
TopbarMobile.displayName = 'TopbarMobile';
export default TopbarMobile;