"use strict";

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

var _typeof = require("@babel/runtime/helpers/typeof");

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

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

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

var _uiIcons = require("@teamleader/ui-icons");

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

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

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

var _constants = require("../../constants");

var _typography = require("../typography");

var _icon = _interopRequireDefault(require("../icon"));

var _box = _interopRequireDefault(require("../box"));

var _tooltip = _interopRequireDefault(require("../tooltip"));

var _theme = _interopRequireDefault(require("./theme.css"));

var _ColorByStatus, _BackgroundTintByStat;

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var TooltippedBox = (0, _tooltip.default)(_box.default);
var Status = {
  ACTIVE: 'active',
  DEFAULT: 'default',
  DISABLED: 'disabled',
  FOCUSED: 'focused',
  INVALID: 'invalid',
  BROKEN: 'broken'
};
exports.Status = Status;
var ColorByStatus = (_ColorByStatus = {}, (0, _defineProperty2.default)(_ColorByStatus, Status.DEFAULT, 'neutral'), (0, _defineProperty2.default)(_ColorByStatus, Status.ACTIVE, 'aqua'), (0, _defineProperty2.default)(_ColorByStatus, Status.FOCUSED, 'aqua'), (0, _defineProperty2.default)(_ColorByStatus, Status.DISABLED, 'neutral'), (0, _defineProperty2.default)(_ColorByStatus, Status.INVALID, 'gold'), (0, _defineProperty2.default)(_ColorByStatus, Status.BROKEN, 'ruby'), _ColorByStatus);
var BackgroundTintByStatus = (_BackgroundTintByStat = {}, (0, _defineProperty2.default)(_BackgroundTintByStat, Status.DEFAULT, undefined), (0, _defineProperty2.default)(_BackgroundTintByStat, Status.ACTIVE, 'light'), (0, _defineProperty2.default)(_BackgroundTintByStat, Status.FOCUSED, 'light'), (0, _defineProperty2.default)(_BackgroundTintByStat, Status.DISABLED, undefined), (0, _defineProperty2.default)(_BackgroundTintByStat, Status.INVALID, 'lightest'), (0, _defineProperty2.default)(_BackgroundTintByStat, Status.BROKEN, 'lightest'), _BackgroundTintByStat);
/** @type {React.ComponentType<any>} */

var FilterSelection = /*#__PURE__*/(0, _react.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.default;
  var tooltipProps = modified ? {
    tooltip: modificationText,
    tooltipColor: 'white',
    tooltipPosition: 'top',
    tooltipSize: 'small'
  } : {};

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

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

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

  return /*#__PURE__*/_react.default.createElement(_box.default, {
    ref: ref,
    className: (0, _classnames.default)(_theme.default['select-control'], status === Status.DEFAULT && _theme.default['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.default.createElement(Container, (0, _extends2.default)({
    display: "flex",
    flex: 1,
    alignItems: "center",
    justifyContent: "space-between",
    marginRight: 2
  }, tooltipProps), /*#__PURE__*/_react.default.createElement(_typography.TextBodyCompact, {
    color: status === Status.DEFAULT ? 'teal' : ColorByStatus[status] || 'neutral',
    tint: status === Status.DISABLED ? 'dark' : 'darkest',
    marginRight: applied ? 2 : 0,
    className: _theme.default['select-value-container']
  }, "".concat(label).concat(modified ? ' •' : '')), applied && /*#__PURE__*/_react.default.createElement(_box.default, {
    display: "flex",
    alignItems: "center",
    borderRadius: "rounded",
    className: (0, _classnames.default)(_theme.default['select-clear'], _theme.default["select-clear--".concat(status)])
  }, showAmount && /*#__PURE__*/_react.default.createElement(_typography.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.default.createElement(_typography.Monospaced, null, amountApplied)), /*#__PURE__*/_react.default.createElement(_box.default, {
    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.default['select-clear-icon']
  }, /*#__PURE__*/_react.default.createElement(_icon.default, {
    color: ColorByStatus[status] || 'neutral',
    tint: "dark",
    opacity: 1
  }, /*#__PURE__*/_react.default.createElement(_uiIcons.IconCloseSmallOutline, null))))), /*#__PURE__*/_react.default.createElement(_icon.default, {
    color: status === Status.DEFAULT ? 'teal' : ColorByStatus[status] || 'neutral',
    tint: status === Status.DISABLED ? 'dark' : 'darkest',
    className: (0, _classnames.default)(_theme.default['select-dropdown-indicator'], status === Status.FOCUSED && _theme.default['select-dropdown-indicator--focused'])
  }, /*#__PURE__*/_react.default.createElement(_uiIcons.IconChevronDownSmallOutline, null)));
});
FilterSelection.defaultProps = {
  label: '',
  modificationText: null,
  applied: false,
  status: Status.DEFAULT,
  amountApplied: null,
  onClick: null,
  onClearClick: null
};
FilterSelection.displayName = 'FilterSelection';
var _default = FilterSelection;
exports.default = _default;