function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }

/*
Copyright (c) Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { getOverrides } from '../helpers/overrides';
import { Action as StyledAction, Root as StyledRoot, StartEnhancerContainer as StyledStartEnhancerContainer, Text as StyledText } from './styled-components';
import { KIND, VARIANT, SIZE } from './constants';
import { getTextFromChildren } from './utils';
import DeleteIcon from '../icon/delete';
import { isFocusVisible, forkFocus, forkBlur } from '../utils/focusVisible';

// Previously, Tag used a hardcoded SVG as its 'close' icon. Replacing it with
// Delete requires modifying Delete's viewbox to prevent visual regressions.
var ModifiedViewBoxDeleteIcon = function ModifiedViewBoxDeleteIcon(props) {
  return /*#__PURE__*/React.createElement(DeleteIcon, _extends({
    viewBox: "5 5 13.186 13.186"
  }, props));
};

var Tag = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _SIZE$small$SIZE$medi;

  var children = props.children,
      _props$closeable = props.closeable,
      closeable = _props$closeable === void 0 ? true : _props$closeable,
      color = props.color,
      _props$size = props.size,
      size = _props$size === void 0 ? SIZE.small : _props$size,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$isFocused = props.isFocused,
      isFocused = _props$isFocused === void 0 ? false : _props$isFocused,
      _props$isHovered = props.isHovered,
      isHovered = _props$isHovered === void 0 ? false : _props$isHovered,
      _props$kind = props.kind,
      kind = _props$kind === void 0 ? KIND.primary : _props$kind,
      _props$onActionClick = props.onActionClick,
      onActionClick = _props$onActionClick === void 0 ? function (event) {} : _props$onActionClick,
      _props$onActionKeyDow = props.onActionKeyDown,
      onActionKeyDown = _props$onActionKeyDow === void 0 ? function (event) {} : _props$onActionKeyDow,
      _props$onClick = props.onClick,
      onClick = _props$onClick === void 0 ? null : _props$onClick,
      _props$onKeyDown = props.onKeyDown,
      onKeyDown = _props$onKeyDown === void 0 ? null : _props$onKeyDown,
      _props$overrides = props.overrides,
      overrides = _props$overrides === void 0 ? {} : _props$overrides,
      startEnhancer = props.startEnhancer,
      title = props.title,
      _props$variant = props.variant,
      variant = _props$variant === void 0 ? VARIANT.light : _props$variant;

  var _React$useState = React.useState(false),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      focusVisible = _React$useState2[0],
      setFocusVisible = _React$useState2[1];

  function handleFocus(event) {
    if (isFocusVisible(event)) {
      setFocusVisible(true);
    }
  }

  function handleBlur(event) {
    if (focusVisible !== false) {
      setFocusVisible(false);
    }
  }

  function handleKeyDown(event) {
    if (event.currentTarget !== event.target) {
      return;
    }

    var key = event.key;

    if (onClick && key === 'Enter') {
      onClick(event);
    }

    if (closeable && (key === 'Backspace' || key === 'Delete')) {
      onActionClick(event);
      onActionKeyDown(event);
    }

    if (onKeyDown) {
      onKeyDown(event);
    }
  }

  var _getOverrides = getOverrides(overrides.Root, StyledRoot),
      _getOverrides2 = _slicedToArray(_getOverrides, 2),
      Root = _getOverrides2[0],
      rootProps = _getOverrides2[1];

  var _getOverrides3 = getOverrides(overrides.Action, StyledAction),
      _getOverrides4 = _slicedToArray(_getOverrides3, 2),
      Action = _getOverrides4[0],
      actionProps = _getOverrides4[1];

  var _getOverrides5 = getOverrides(overrides.ActionIcon, ModifiedViewBoxDeleteIcon),
      _getOverrides6 = _slicedToArray(_getOverrides5, 2),
      ActionIcon = _getOverrides6[0],
      actionIconProps = _getOverrides6[1];

  var _getOverrides7 = getOverrides(overrides.StartEnhancerContainer, StyledStartEnhancerContainer),
      _getOverrides8 = _slicedToArray(_getOverrides7, 2),
      StartEnhancerContainer = _getOverrides8[0],
      startEnhancerContainerProps = _getOverrides8[1];

  var _getOverrides9 = getOverrides(overrides.Text, StyledText),
      _getOverrides10 = _slicedToArray(_getOverrides9, 2),
      Text = _getOverrides10[0],
      textProps = _getOverrides10[1];

  var clickable = typeof onClick === 'function';
  var rootHandlers = disabled ? {} : {
    onClick: onClick,
    onKeyDown: handleKeyDown
  };
  var actionHandlers = disabled ? {} : {
    onClick: function onClick(event) {
      // we don't want onClick to be called when the close icon is clicked
      event.stopPropagation();
      onActionClick(event);
    }
  };
  var sharedProps = {
    $clickable: clickable,
    $closeable: closeable,
    $color: color,
    $disabled: disabled,
    $isFocused: isFocused,
    $isHovered: isHovered,
    $kind: kind,
    $variant: variant,
    $isFocusVisible: focusVisible,
    $size: size
  };
  var titleText = title || getTextFromChildren(children);
  var isButton = (clickable || closeable) && !disabled;
  var actionSize = (_SIZE$small$SIZE$medi = {}, _defineProperty(_SIZE$small$SIZE$medi, SIZE.small, 12), _defineProperty(_SIZE$small$SIZE$medi, SIZE.medium, 16), _defineProperty(_SIZE$small$SIZE$medi, SIZE.large, 20), _SIZE$small$SIZE$medi)[size]; // Capitalize for JSX

  var StartEnhancer = startEnhancer;
  return /*#__PURE__*/React.createElement(Root, _extends({
    ref: ref,
    "data-baseweb": "tag",
    "aria-label": isButton && closeable ? "".concat(typeof children === 'string' ? "".concat(children, ", ") : '', "close by backspace") : null,
    "aria-disabled": disabled ? true : null,
    role: isButton ? 'button' : null,
    tabIndex: isButton ? 0 : null
  }, rootHandlers, sharedProps, rootProps, {
    onFocus: forkFocus(rootProps, handleFocus),
    onBlur: forkBlur(rootProps, handleBlur)
  }), StartEnhancer && // @ts-expect-error todo(flow->ts) it is not expected to be a number
  StartEnhancer !== 0 && /*#__PURE__*/React.createElement(StartEnhancerContainer, startEnhancerContainerProps, /*#__PURE__*/React.createElement(StartEnhancer, null)), /*#__PURE__*/React.createElement(Text, _extends({
    title: titleText
  }, textProps), children), closeable ? /*#__PURE__*/React.createElement(Action, _extends({
    "aria-hidden": true,
    role: "presentation"
  }, actionHandlers, sharedProps, actionProps), /*#__PURE__*/React.createElement(ActionIcon, _extends({
    size: actionSize
  }, actionIconProps))) : null);
});
Tag.displayName = 'Tag';
export default Tag;