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); }
import React from 'react';
import { Popover } from '../../Popover';
import { Typography } from '../../Typography';
import { StyledFilterPresetPopoverContent } from '../SuperSelect.presets.styles';
import { StyledLabel, StyledTrigger } from '../SuperSelect.styles';
import { useSuperSelectContext } from '../useSuperSelect';
var getFilterPresetProps = function getFilterPresetProps(props) {
  var _props$filter;
  return (_props$filter = props === null || props === void 0 ? void 0 : props.filter) !== null && _props$filter !== void 0 ? _props$filter : {};
};
var getValueLabel = function getValueLabel(isValueEmpty, value, placeholder, filterName) {
  var prefixLabel = isValueEmpty ? placeholder || filterName : filterName;
  var valueLabel = value ? "".concat(prefixLabel ? ': ' : '').concat(value) : '';
  return "".concat(prefixLabel).concat(valueLabel);
};
export var filterPreset = {
  components: {
    Label: /*#__PURE__*/React.forwardRef(function () {
      var ctx = useSuperSelectContext();
      var presetProps = getFilterPresetProps(ctx.config.presetProps);
      var isValueEmpty = ['', null, undefined].includes(ctx.state.value);
      return /*#__PURE__*/React.createElement(StyledLabel, {
        $hoverable: false
      }, getValueLabel(isValueEmpty, ctx.state.selectedLabel, ctx.config.placeholder, presetProps.filterName));
    }),
    TriggerContainer: /*#__PURE__*/React.forwardRef(function (props, ref) {
      var ctx = useSuperSelectContext();
      if (!Array.isArray(ctx.state.value)) {
        return /*#__PURE__*/React.createElement(StyledTrigger, _extends({
          $block: ctx.config.block
        }, props, {
          ref: ref
        }));
      }
      return /*#__PURE__*/React.createElement(Popover, {
        placement: "top",
        trigger: ctx.state.value.length > 1 ? 'hover' : 'none',
        overlay: /*#__PURE__*/React.createElement(StyledFilterPresetPopoverContent, null, /*#__PURE__*/React.createElement(Typography, null, ctx.state.value.map(function (v) {
          var option = ctx.state.options.find(function (option) {
            return ctx.option.value(option) === v;
          });
          return option ? ctx.option.label(option) : '';
        }).join(', ')))
      }, /*#__PURE__*/React.createElement(StyledTrigger, _extends({
        $block: ctx.config.block
      }, props, {
        ref: ref
      })));
    }),
    MultiValueContainer: function MultiValueContainer() {
      var ctx = useSuperSelectContext();
      var presetProps = getFilterPresetProps(ctx.config.presetProps);
      if (Array.isArray(ctx.state.value)) {
        var isValueEmpty = !ctx.state.value.length;
        var value = ctx.state.value.length > 1 ? "(".concat(ctx.state.value.length, ")") : ctx.state.value.length === 1 ? "".concat(ctx.state.selectedLabel) : '';
        return /*#__PURE__*/React.createElement(StyledLabel, {
          $hoverable: false
        }, getValueLabel(isValueEmpty, value, ctx.config.placeholder, presetProps.filterName));
      }
      return /*#__PURE__*/React.createElement("div", null, "Value is not an array");
    }
  },
  selectionStyle: 'highlight'
};
//# sourceMappingURL=filterPreset.js.map