"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.RadioGroupFilter = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _mobxReactLite = require("mobx-react-lite");
var _hooks = require("../../hooks");
var _designSystem = require("@wix/design-system");
var _PatternsThemeProvider = require("../../themes/PatternsThemeProvider");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/RadioGroupFilter/RadioGroupFilter.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 _RadioGroupFilter(props) {
  var _selectedValues$;
  const {
    filter,
    data = [],
    renderItem
  } = props;
  const {
    dropdownSelect,
    init
  } = (0, _hooks.useDropdownSelectFilterState)({
    filter
  });
  const {
    radioGroup: {
      spacing
    }
  } = (0, _PatternsThemeProvider.usePatternsTheme)();
  const {
    select: {
      selectedValues
    },
    container: {
      translate: t
    }
  } = dropdownSelect;
  const defaultValue = {
    id: 'all',
    name: t('cairo.filtersPanel.default.All')
  };
  (0, _react.useEffect)(() => {
    return init();
  }, []);
  const keyedItems = (0, _react.useMemo)(() => data.map((item, index) => {
    const key = filter.itemKey(item);
    return {
      id: key,
      key,
      item,
      index,
      indexWithinPage: index,
      pageIndex: 0
    };
  }), [data]);
  return /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    direction: "vertical",
    gap: "SP2",
    dataHook: `radio-group-filter-${filter.name}`,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 64,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.RadioGroup, {
    dataHook: "radio-group-filter-group",
    value: ((_selectedValues$ = selectedValues[0]) == null ? void 0 : _selectedValues$.key) || defaultValue.id,
    spacing: spacing,
    onChange: v => {
      if ((v == null ? void 0 : v.toString()) === defaultValue.id) {
        dropdownSelect.clear();
      } else {
        const newItem = keyedItems.find(item => item.key === (v == null ? void 0 : v.toString()));
        newItem && dropdownSelect.setSingle(newItem);
      }
    },
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 69,
      columnNumber: 7
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.RadioGroup.Radio, {
    value: defaultValue.id,
    dataHook: "radio-group-filter-option-all",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 84,
      columnNumber: 9
    }
  }, defaultValue.name), keyedItems.map((keyedItem, index) => {
    const radioProps = renderItem == null ? void 0 : renderItem(keyedItem.item, index);
    const key = keyedItem.key;
    return /*#__PURE__*/_react.default.createElement(_designSystem.RadioGroup.Radio, (0, _extends2.default)({}, radioProps, {
      key: key,
      value: key,
      dataHook: `radio-group-filter-option-${key}`,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 94,
        columnNumber: 13
      }
    }), (radioProps == null ? void 0 : radioProps.children) ?? filter.itemName(keyedItem.item));
  })));
}
const RadioGroupFilter = exports.RadioGroupFilter = (0, _mobxReactLite.observer)(_RadioGroupFilter);
RadioGroupFilter.displayName = 'RadioGroupFilter';
//# sourceMappingURL=RadioGroupFilter.js.map