"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 _classnames = _interopRequireDefault(require("classnames"));

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

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

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

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

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

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

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

var _COLOR_BY_STATUS, _BACKGROUND_TINT_BY_S;

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 COLOR_BY_STATUS = (_COLOR_BY_STATUS = {}, (0, _defineProperty2.default)(_COLOR_BY_STATUS, STATUS.DEFAULT, 'neutral'), (0, _defineProperty2.default)(_COLOR_BY_STATUS, STATUS.ACTIVE, 'aqua'), (0, _defineProperty2.default)(_COLOR_BY_STATUS, STATUS.FOCUSED, 'aqua'), (0, _defineProperty2.default)(_COLOR_BY_STATUS, STATUS.DISABLED, 'neutral'), (0, _defineProperty2.default)(_COLOR_BY_STATUS, STATUS.INVALID, 'gold'), (0, _defineProperty2.default)(_COLOR_BY_STATUS, STATUS.BROKEN, 'ruby'), _COLOR_BY_STATUS);
var BACKGROUND_TINT_BY_STATUS = (_BACKGROUND_TINT_BY_S = {}, (0, _defineProperty2.default)(_BACKGROUND_TINT_BY_S, STATUS.DEFAULT, undefined), (0, _defineProperty2.default)(_BACKGROUND_TINT_BY_S, STATUS.ACTIVE, 'light'), (0, _defineProperty2.default)(_BACKGROUND_TINT_BY_S, STATUS.FOCUSED, 'light'), (0, _defineProperty2.default)(_BACKGROUND_TINT_BY_S, STATUS.DISABLED, undefined), (0, _defineProperty2.default)(_BACKGROUND_TINT_BY_S, STATUS.INVALID, 'lightest'), (0, _defineProperty2.default)(_BACKGROUND_TINT_BY_S, STATUS.BROKEN, 'lightest'), _BACKGROUND_TINT_BY_S);
var FilterSelection = /*#__PURE__*/(0, _react.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.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: 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.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' : COLOR_BY_STATUS[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: COLOR_BY_STATUS[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: COLOR_BY_STATUS[status] || 'neutral',
    tint: "dark",
    opacity: 1
  }, /*#__PURE__*/_react.default.createElement(_uiIcons.IconCloseSmallOutline, null))))), /*#__PURE__*/_react.default.createElement(_icon.default, {
    color: status === STATUS.DEFAULT ? 'teal' : COLOR_BY_STATUS[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.displayName = 'FilterSelection';
var _default = FilterSelection;
exports.default = _default;