"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.DateRangeFilter = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@wix/bex-core/react");
var _mobxReactLite = require("mobx-react-lite");
var _DateRangePicker = require("../DateRangePicker");
var _designSystem = require("@wix/design-system");
var _bexCore = require("@wix/bex-core");
var _hooks = require("../../hooks");
var _PatternsThemeProvider = require("../../themes/PatternsThemeProvider");
var _renderPresetToolbarTag = require("./renderPresetToolbarTag");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/DateRangeFilter/DateRangeFilter.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 _DateRangeFilter(props) {
  const {
    filter,
    filterByTime,
    mode = 'COMBINE',
    dateRangeOptions,
    ...rest
  } = props;
  const {
    translate: t
  } = (0, _react2.useWixPatternsContainer)();
  const {
    radioGroup: {
      spacing
    }
  } = (0, _PatternsThemeProvider.usePatternsTheme)();
  const state = (0, _hooks.useDateRangeFilterState)({
    filter,
    filterByTime,
    mode
  });
  const dateRangePreset = (0, _react.useMemo)(() => {
    if (!dateRangeOptions) {
      return _bexCore.DefaultDateRangePreset;
    }
    const dateRangePresetArr = Object.keys(_bexCore.DateRangePreset).filter(key => dateRangeOptions.includes(key));
    dateRangePresetArr.unshift(_bexCore.DateRangePreset.NONE);
    if (mode === 'COMBINE') {
      dateRangePresetArr.push(_bexCore.DateRangePreset.CUSTOM);
    }
    const dateRangePresetObjects = dateRangePresetArr.reduce((cur, key) => ({
      ...cur,
      [key]: _bexCore.DateRangePreset[key]
    }), {});
    return dateRangePresetObjects;
  }, [mode, dateRangeOptions]);
  (0, _react.useEffect)(() => state.init(), []);
  const customElement = /*#__PURE__*/_react.default.createElement(_DateRangePicker.DateRangePicker, (0, _extends2.default)({
    dataHook: "date-range-picker",
    state: state.dateRangePicker
  }, rest, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 91,
      columnNumber: 5
    }
  }));
  const innerComponent = mode === 'ONLY_CUSTOM' ? customElement : /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    direction: "vertical",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 101,
      columnNumber: 7
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.RadioGroup, {
    value: state.dateRangePicker.selectedPreset,
    onChange: v => v && state.dateRangePicker.onPresetChanged(v.toString()),
    spacing: spacing,
    dataHook: "date-range-filter-group",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 102,
      columnNumber: 9
    }
  }, Object.values(dateRangePreset).map(preset => preset === dateRangePreset.CUSTOM ? null : /*#__PURE__*/_react.default.createElement(_designSystem.RadioGroup.Radio, {
    key: preset,
    value: preset,
    dataHook: `date-range-filter-option-${preset}`,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 112,
      columnNumber: 15
    }
  }, t(_bexCore.MAP_DATE_PRESET_TO_KEY[preset]))), mode === 'COMBINE' && dateRangePreset.CUSTOM && /*#__PURE__*/_react.default.createElement(_designSystem.RadioGroup.Radio, {
    value: dateRangePreset.CUSTOM,
    dataHook: `date-range-filter-option-${dateRangePreset.CUSTOM}`,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 122,
      columnNumber: 13
    }
  }, t(_bexCore.MAP_DATE_PRESET_TO_KEY[dateRangePreset.CUSTOM]))), /*#__PURE__*/_react.default.createElement(_designSystem.Collapse, {
    open: state.dateRangePicker.selectedPreset === dateRangePreset.CUSTOM,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 130,
      columnNumber: 9
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    marginTop: "14px",
    overflowY: "visible",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 133,
      columnNumber: 11
    }
  }, customElement)));
  return /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    dataHook: `date-range-filter-${filter.name}`,
    direction: "vertical",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 141,
      columnNumber: 5
    }
  }, innerComponent);
}
const DateRangeFilter = exports.DateRangeFilter = (0, _mobxReactLite.observer)(_DateRangeFilter);
DateRangeFilter.displayName = 'DateRangeFilter';
DateRangeFilter.renderPresetToolbarTag = _renderPresetToolbarTag.renderPresetToolbarTag;
//# sourceMappingURL=DateRangeFilter.js.map