import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["active", "children", "className", "counter", "size", "onClick", "element"];
import cx from 'classnames';
import React, { forwardRef, useImperativeHandle, useRef } from 'react';
import Box from '../box';
import theme from './theme.css';
import { Heading4, Heading5 } from '../typography';
var TitleTab = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _ref$active = _ref.active,
      active = _ref$active === void 0 ? false : _ref$active,
      children = _ref.children,
      className = _ref.className,
      _ref$counter = _ref.counter,
      counter = _ref$counter === void 0 ? null : _ref$counter,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'medium' : _ref$size,
      onClick = _ref.onClick,
      _ref$element = _ref.element,
      element = _ref$element === void 0 ? 'a' : _ref$element,
      others = _objectWithoutProperties(_ref, _excluded);

  var tabRef = useRef(null);
  useImperativeHandle(ref, function () {
    return tabRef.current;
  });

  var blur = function blur() {
    var currentTabRef = tabRef.current;

    if (currentTabRef !== null && currentTabRef !== void 0 && currentTabRef.blur) {
      currentTabRef.blur();
    }
  };

  var handleClick = function handleClick(event) {
    if (onClick) {
      onClick(event);
    }

    if (event.pageX !== 0 && event.pageY !== 0) {
      blur();
    }
  };

  var getPaddingHorizontal = function getPaddingHorizontal() {
    switch (size) {
      case 'small':
        return 2;

      case 'medium':
      default:
        return 3;
    }
  };

  var classNames = cx(theme['title-tab'], _defineProperty({}, theme['is-active'], active), className);
  var TextElement = size === 'small' ? Heading5 : Heading4;
  return /*#__PURE__*/React.createElement(Box, _extends({
    "data-teamleader-ui": "title-tab",
    className: classNames,
    marginHorizontal: size === 'small' ? 1 : 2,
    paddingHorizontal: getPaddingHorizontal(),
    ref: tabRef,
    onClick: handleClick,
    element: element
  }, others), /*#__PURE__*/React.createElement(TextElement, {
    element: "span"
  }, children), counter);
});
TitleTab.displayName = 'TitleTab';
export default TitleTab;