"use strict";

exports.__esModule = true;
exports.OperatorFilterPicker = void 0;
var _react = _interopRequireWildcard(require("react"));
var _mobxReactLite = require("mobx-react-lite");
var _designSystem = require("@wix/design-system");
var _bexCore = require("@wix/bex-core");
var _react2 = require("@wix/bex-core/react");
var _useOperatorFilterState = require("../../hooks/useOperatorFilterState");
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _renderOperatorFilterToolbarTag = require("./renderOperatorFilterToolbarTag");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/OperatorFilterPicker/OperatorFilterPicker.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _OperatorFilterPicker(props) {
  var _state$value;
  const {
    filter
  } = props;
  const {
    translate: t
  } = (0, _react2.useWixPatternsContainer)();
  const filterState = filter;
  const allowedOperators = filterState.allowedOperators;
  const hasOperatorWithValue = (0, _react.useMemo)(() => allowedOperators.some(op => (0, _bexCore.operatorNeedsValue)(op)), [allowedOperators]);
  if (!hasOperatorWithValue) {
    throw new Error(`[OperatorFilterPicker] Invalid configuration for filter "${filter.name}": ` + `All operators in allowedOperators [${allowedOperators.join(', ')}] don't require a value. ` + `When using only value-less operators like EXISTS, IS_EMPTY, or NOT_EXISTS, ` + `you should use a Checkbox filter component instead of OperatorFilterPicker. ` + `OperatorFilterPicker is designed for operators that require user input (e.g., EQUALS, CONTAINS, STARTS_WITH).`);
  }
  const state = (0, _useOperatorFilterState.useOperatorFilterState)({
    filter
  });
  (0, _react.useEffect)(() => state.init(), [state]);
  const operatorOptions = allowedOperators.map(op => (0, _designSystem.listItemSelectBuilder)({
    id: op,
    title: t((0, _bexCore.getOperatorLabel)(op)),
    label: t((0, _bexCore.getOperatorLabel)(op))
  }));
  const showValueInput = state.operatorNeedsValue();
  return /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    dataHook: `operator-picker-${filter.name}`,
    direction: "vertical",
    gap: "12px",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 65,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.FormField, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 70,
      columnNumber: 7
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.DropdownBase, {
    options: operatorOptions,
    selectedId: state.operator,
    onSelect: option => state.setOperator(option.id),
    dataHook: "operator-picker-operator-dropdown",
    appendTo: "window",
    flip: true,
    placement: "bottom-start",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 71,
      columnNumber: 9
    }
  }, ({
    toggle
  }) => /*#__PURE__*/_react.default.createElement(_designSystem.TextButton, {
    skin: "dark",
    onClick: toggle,
    suffixIcon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.ChevronDown, {
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 84,
        columnNumber: 27
      }
    }),
    size: "small",
    dataHook: "operator-picker-trigger",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 81,
      columnNumber: 13
    }
  }, t((0, _bexCore.getOperatorLabel)(state.operator))))), showValueInput && /*#__PURE__*/_react.default.createElement(_designSystem.FormField, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 95,
      columnNumber: 9
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Input, {
    border: "round",
    value: ((_state$value = state.value) == null ? void 0 : _state$value.toString()) || '',
    placeholder: t('cairo.filterOperator.valuePlaceholder'),
    onChange: e => state.setValue(e.target.value),
    onBlur: () => state.applyImmediately(),
    onKeyDown: e => {
      if (e.key === 'Enter') {
        state.applyImmediately();
      }
    },
    dataHook: "operator-picker-value-input",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 96,
      columnNumber: 11
    }
  })));
}
const OperatorFilterPicker = exports.OperatorFilterPicker = (0, _mobxReactLite.observer)(_OperatorFilterPicker);
OperatorFilterPicker.displayName = 'OperatorFilterPicker';
OperatorFilterPicker.renderPresetToolbarTag = _renderOperatorFilterToolbarTag.renderOperatorFilterToolbarTag;
//# sourceMappingURL=OperatorFilterPicker.js.map