"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _js = require("../../globals/js");

var _excluded = ["className", "index", "kind", "name", "onClick", "onKeyDown", "selected", "text", "icon", "href"];
var prefix = _js.settings.prefix;

var Switch = function Switch(props) {
  var className = props.className,
      index = props.index,
      kind = props.kind,
      name = props.name,
      onClick = props.onClick,
      onKeyDown = props.onKeyDown,
      selected = props.selected,
      text = props.text,
      icon = props.icon,
      href = props.href,
      other = (0, _objectWithoutProperties2.default)(props, _excluded);

  var handleClick = function handleClick(e) {
    e.preventDefault();
    onClick({
      index: index,
      name: name,
      text: text
    });
  };

  var handleKeyDown = function handleKeyDown(e) {
    var key = e.key || e.which;

    if (key === 'Enter' || key === 13 || key === ' ' || key === 32) {
      onKeyDown({
        index: index,
        name: name,
        text: text
      });
    }
  };

  var classes = (0, _classnames.default)(className, "".concat(prefix, "--content-switcher-btn"), (0, _defineProperty2.default)({}, "".concat(prefix, "--content-switcher--selected"), selected));
  var commonProps = {
    onClick: handleClick,
    onKeyDown: handleKeyDown,
    className: classes
  };
  var btnIcon = icon ? /*#__PURE__*/_react.default.cloneElement(icon, {
    className: (0, _classnames.default)(icon.props.className, "".concat(prefix, "--content-switcher__icon"))
  }) : null;

  if (kind === 'button') {
    return /*#__PURE__*/_react.default.createElement("button", (0, _extends2.default)({}, other, commonProps), btnIcon, text);
  }

  return /*#__PURE__*/_react.default.createElement("a", (0, _extends2.default)({
    href: href
  }, other, commonProps), btnIcon, text);
};

Switch.propTypes = {
  className: _propTypes.default.string,
  index: _propTypes.default.number,
  kind: _propTypes.default.oneOf(['button', 'anchor']).isRequired,
  name: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
  onClick: _propTypes.default.func,
  onKeyDown: _propTypes.default.func,
  selected: _propTypes.default.bool,
  text: _propTypes.default.string.isRequired,
  icon: _propTypes.default.element,
  href: _propTypes.default.string
};
Switch.defaultProps = {
  selected: false,
  kind: 'anchor',
  text: 'Provide text',
  href: '',
  onClick: function onClick() {},
  onKeyDown: function onKeyDown() {}
};
var _default = Switch;
exports.default = _default;