import _pt from "prop-types";

function _extends() { _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; }; return _extends.apply(this, arguments); }

import React from 'react';
import classNames from 'classnames';
import { ArrowIcon } from '../Icons';
import { t } from '../i18n';
var DEFAULT_COMPONENT_TYPE = 'div';
export var VerticalNavItemLabel = function VerticalNavItemLabel(props) {
  /**
   * The type of element rendered ultimately depends on whether
   * this is meant to be a subnav toggle, link, or generic label
   * @return {String} The type of HTML tag
   */
  var componentType = function componentType() {
    if (props.hasSubnav) {
      return 'button';
    } else if (props.component) {
      return props.component;
    } else if (props.url) {
      return 'a';
    }

    return DEFAULT_COMPONENT_TYPE;
  };

  var LabelComponent = componentType();

  var handleClick = function handleClick(evt) {
    return props.onClick(evt);
  };

  var commonProps = {
    className: classNames('ds-c-vertical-nav__label', {
      'ds-c-vertical-nav__label--current': props.selected,
      'ds-c-vertical-nav__label--parent': props.hasSubnav
    }),
    onClick: props.onClick ? handleClick : undefined
  };
  var otherProps;

  if (LabelComponent === 'button') {
    var _props$ariaCollapsedS, _props$ariaExpandedSt;

    var collapsedLabel = (_props$ariaCollapsedS = props.ariaCollapsedStateButtonLabel) !== null && _props$ariaCollapsedS !== void 0 ? _props$ariaCollapsedS : t('verticalNav.expand');
    var expandedLabel = (_props$ariaExpandedSt = props.ariaExpandedStateButtonLabel) !== null && _props$ariaExpandedSt !== void 0 ? _props$ariaExpandedSt : t('verticalNav.collapse');
    otherProps = {
      'aria-controls': props.subnavId,
      'aria-expanded': !props.collapsed,
      title: props.collapsed ? collapsedLabel : expandedLabel
    };
  } else if (LabelComponent !== DEFAULT_COMPONENT_TYPE) {
    // Apply href if <a> or custom component type
    otherProps = {
      'aria-current': props.selected,
      href: props.url
    };
  }

  return /*#__PURE__*/React.createElement(LabelComponent, _extends({}, commonProps, otherProps), props.label, props.hasSubnav && /*#__PURE__*/React.createElement(ArrowIcon, {
    direction: props.collapsed ? 'down' : 'up'
  }));
};
VerticalNavItemLabel.propTypes = {
  ariaCollapsedStateButtonLabel: _pt.string,
  ariaExpandedStateButtonLabel: _pt.string,
  collapsed: _pt.bool,
  component: _pt.any,
  hasSubnav: _pt.bool,
  label: _pt.node.isRequired,
  onClick: _pt.func,
  selected: _pt.bool,
  subnavId: _pt.string.isRequired,
  url: _pt.string
};
export default VerticalNavItemLabel;