import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

var _COLOR_BY_STATUS, _BACKGROUND_TINT_BY_S;

import { IconChevronDownSmallOutline, IconCloseSmallOutline } from '@teamleader/ui-icons';
import cx from 'classnames';
import React, { forwardRef } from 'react';
import { KEY } from '../../constants';
import Box from '../box';
import Icon from '../icon';
import Tooltip from '../tooltip';
import { Heading4, Monospaced, TextBodyCompact } from '../typography';
import theme from './theme.css';
var TooltippedBox = Tooltip(Box);
export var STATUS = {
  ACTIVE: 'active',
  DEFAULT: 'default',
  DISABLED: 'disabled',
  FOCUSED: 'focused',
  INVALID: 'invalid',
  BROKEN: 'broken'
};
var COLOR_BY_STATUS = (_COLOR_BY_STATUS = {}, _defineProperty(_COLOR_BY_STATUS, STATUS.DEFAULT, 'neutral'), _defineProperty(_COLOR_BY_STATUS, STATUS.ACTIVE, 'aqua'), _defineProperty(_COLOR_BY_STATUS, STATUS.FOCUSED, 'aqua'), _defineProperty(_COLOR_BY_STATUS, STATUS.DISABLED, 'neutral'), _defineProperty(_COLOR_BY_STATUS, STATUS.INVALID, 'gold'), _defineProperty(_COLOR_BY_STATUS, STATUS.BROKEN, 'ruby'), _COLOR_BY_STATUS);
var BACKGROUND_TINT_BY_STATUS = (_BACKGROUND_TINT_BY_S = {}, _defineProperty(_BACKGROUND_TINT_BY_S, STATUS.DEFAULT, undefined), _defineProperty(_BACKGROUND_TINT_BY_S, STATUS.ACTIVE, 'light'), _defineProperty(_BACKGROUND_TINT_BY_S, STATUS.FOCUSED, 'light'), _defineProperty(_BACKGROUND_TINT_BY_S, STATUS.DISABLED, undefined), _defineProperty(_BACKGROUND_TINT_BY_S, STATUS.INVALID, 'lightest'), _defineProperty(_BACKGROUND_TINT_BY_S, STATUS.BROKEN, 'lightest'), _BACKGROUND_TINT_BY_S);
var FilterSelection = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _ref$label = _ref.label,
      label = _ref$label === void 0 ? '' : _ref$label,
      _ref$modificationText = _ref.modificationText,
      modificationText = _ref$modificationText === void 0 ? null : _ref$modificationText,
      _ref$applied = _ref.applied,
      applied = _ref$applied === void 0 ? false : _ref$applied,
      _ref$status = _ref.status,
      status = _ref$status === void 0 ? 'default' : _ref$status,
      _ref$amountApplied = _ref.amountApplied,
      amountApplied = _ref$amountApplied === void 0 ? null : _ref$amountApplied,
      _ref$onClick = _ref.onClick,
      onClick = _ref$onClick === void 0 ? function () {} : _ref$onClick,
      _ref$onClearClick = _ref.onClearClick,
      onClearClick = _ref$onClearClick === void 0 ? function () {} : _ref$onClearClick;
  var showAmount = typeof amountApplied === 'number';
  var modified = typeof modificationText === 'string';
  var Container = modified ? TooltippedBox : Box;
  var tooltipProps = modified ? {
    tooltip: modificationText,
    tooltipColor: 'white',
    tooltipPosition: 'top',
    tooltipSize: 'small'
  } : {};

  var handleKeyDown = function handleKeyDown(event) {
    if (event.key === KEY.Enter && status !== STATUS.DISABLED) {
      onClick();
    }
  };

  var handleClearClick = function handleClearClick(event) {
    event.stopPropagation();

    if (status !== STATUS.DISABLED) {
      onClearClick();
    }
  };

  return /*#__PURE__*/React.createElement(Box, {
    ref: ref,
    className: cx(theme['select-control'], status === STATUS.DEFAULT && theme['select-control--hovered']),
    role: "button",
    tabIndex: 0,
    display: "flex",
    boxSizing: "border-box",
    paddingHorizontal: 3,
    backgroundColor: COLOR_BY_STATUS[status] || 'neutral',
    backgroundTint: BACKGROUND_TINT_BY_STATUS[status] || undefined,
    borderWidth: applied ? 2 : 0,
    borderColor: COLOR_BY_STATUS[status] || 'neutral',
    borderTint: "dark",
    borderRadius: "rounded",
    onClick: status !== STATUS.DISABLED ? onClick : undefined,
    onKeyDown: handleKeyDown
  }, /*#__PURE__*/React.createElement(Container, _extends({
    display: "flex",
    flex: 1,
    alignItems: "center",
    justifyContent: "space-between",
    marginRight: 2
  }, tooltipProps), /*#__PURE__*/React.createElement(TextBodyCompact, {
    color: status === STATUS.DEFAULT ? 'teal' : COLOR_BY_STATUS[status] || 'neutral',
    tint: status === STATUS.DISABLED ? 'dark' : 'darkest',
    marginRight: applied ? 2 : 0,
    className: theme['select-value-container']
  }, "".concat(label).concat(modified ? ' •' : '')), applied && /*#__PURE__*/React.createElement(Box, {
    display: "flex",
    alignItems: "center",
    borderRadius: "rounded",
    className: cx(theme['select-clear'], theme["select-clear--".concat(status)])
  }, showAmount && /*#__PURE__*/React.createElement(Heading4, {
    paddingLeft: 2,
    paddingRight: 1,
    color: COLOR_BY_STATUS[status] || 'neutral',
    tint: "dark",
    borderTopLeftRadius: "rounded",
    borderBottomLeftRadius: "rounded",
    borderTopRightRadius: showAmount ? 'square' : 'rounded',
    borderBottomRightRadius: showAmount ? 'square' : 'rounded'
  }, /*#__PURE__*/React.createElement(Monospaced, null, amountApplied)), /*#__PURE__*/React.createElement(Box, {
    alignItems: "center",
    display: "flex",
    onMouseDown: function onMouseDown(event) {
      return event.stopPropagation();
    },
    onClick: handleClearClick,
    paddingHorizontal: 1,
    borderTopLeftRadius: showAmount ? 'square' : 'rounded',
    borderBottomLeftRadius: showAmount ? 'square' : 'rounded',
    borderTopRightRadius: "rounded",
    borderBottomRightRadius: "rounded",
    className: theme['select-clear-icon']
  }, /*#__PURE__*/React.createElement(Icon, {
    color: COLOR_BY_STATUS[status] || 'neutral',
    tint: "dark",
    opacity: 1
  }, /*#__PURE__*/React.createElement(IconCloseSmallOutline, null))))), /*#__PURE__*/React.createElement(Icon, {
    color: status === STATUS.DEFAULT ? 'teal' : COLOR_BY_STATUS[status] || 'neutral',
    tint: status === STATUS.DISABLED ? 'dark' : 'darkest',
    className: cx(theme['select-dropdown-indicator'], status === STATUS.FOCUSED && theme['select-dropdown-indicator--focused'])
  }, /*#__PURE__*/React.createElement(IconChevronDownSmallOutline, null)));
});
FilterSelection.displayName = 'FilterSelection';
export default FilterSelection;