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

var _ColorByStatus, _BackgroundTintByStat;

import { IconCloseSmallOutline, IconChevronDownSmallOutline } from '@teamleader/ui-icons';
import React, { forwardRef } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { KEY } from '../../constants';
import { Heading4, Monospaced, TextBodyCompact } from '../typography';
import Icon from '../icon';
import Box from '../box';
import Tooltip from '../tooltip';
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 ColorByStatus = (_ColorByStatus = {}, _defineProperty(_ColorByStatus, Status.DEFAULT, 'neutral'), _defineProperty(_ColorByStatus, Status.ACTIVE, 'aqua'), _defineProperty(_ColorByStatus, Status.FOCUSED, 'aqua'), _defineProperty(_ColorByStatus, Status.DISABLED, 'neutral'), _defineProperty(_ColorByStatus, Status.INVALID, 'gold'), _defineProperty(_ColorByStatus, Status.BROKEN, 'ruby'), _ColorByStatus);
var BackgroundTintByStatus = (_BackgroundTintByStat = {}, _defineProperty(_BackgroundTintByStat, Status.DEFAULT, undefined), _defineProperty(_BackgroundTintByStat, Status.ACTIVE, 'light'), _defineProperty(_BackgroundTintByStat, Status.FOCUSED, 'light'), _defineProperty(_BackgroundTintByStat, Status.DISABLED, undefined), _defineProperty(_BackgroundTintByStat, Status.INVALID, 'lightest'), _defineProperty(_BackgroundTintByStat, Status.BROKEN, 'lightest'), _BackgroundTintByStat);
/** @type {React.ComponentType<any>} */

var FilterSelection = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var label = _ref.label,
      modificationText = _ref.modificationText,
      applied = _ref.applied,
      status = _ref.status,
      amountApplied = _ref.amountApplied,
      onClick = _ref.onClick,
      onClearClick = _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: ColorByStatus[status] || 'neutral',
    backgroundTint: BackgroundTintByStatus[status] || undefined,
    borderWidth: applied ? 2 : 0,
    borderColor: ColorByStatus[status] || 'neutral',
    borderTint: "dark",
    borderRadius: "rounded",
    onClick: status !== Status.DISABLED && onClick,
    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' : ColorByStatus[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: ColorByStatus[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: ColorByStatus[status] || 'neutral',
    tint: "dark",
    opacity: 1
  }, /*#__PURE__*/React.createElement(IconCloseSmallOutline, null))))), /*#__PURE__*/React.createElement(Icon, {
    color: status === Status.DEFAULT ? 'teal' : ColorByStatus[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.defaultProps = {
  label: '',
  modificationText: null,
  applied: false,
  status: Status.DEFAULT,
  amountApplied: null,
  onClick: null,
  onClearClick: null
};
FilterSelection.displayName = 'FilterSelection';
export default FilterSelection;