"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _chunkBLUUIEGRcjs = require('./chunk-BLUUIEGR.cjs');








var _chunkFBWTYGYEcjs = require('./chunk-FBWTYGYE.cjs');


var _chunkY45PAGZ2cjs = require('./chunk-Y45PAGZ2.cjs');


var _chunk6HFFGWQXcjs = require('./chunk-6HFFGWQX.cjs');


var _chunkILBP4CTUcjs = require('./chunk-ILBP4CTU.cjs');


var _chunkZUAO4DW3cjs = require('./chunk-ZUAO4DW3.cjs');


var _chunkKKOBB4X5cjs = require('./chunk-KKOBB4X5.cjs');


var _chunkZU72XWNEcjs = require('./chunk-ZU72XWNE.cjs');



var _chunkZ6EUSUADcjs = require('./chunk-Z6EUSUAD.cjs');

// src/components/DataManagerTable/DateFilter.tsx
var _styled = require('@emotion/styled'); var _styled2 = _interopRequireDefault(_styled);
var _react = require('react');
var _jsxruntime = require('@emotion/react/jsx-runtime');
var DatePreset = /* @__PURE__ */ ((DatePreset2) => {
  DatePreset2["LastHour"] = "Last hour";
  DatePreset2["Last12Hours"] = "Last 12 hours";
  DatePreset2["Today"] = "Today";
  DatePreset2["Last7Days"] = "Last 7 days";
  DatePreset2["Last14Days"] = "Last 14 days";
  DatePreset2["Last30Days"] = "Last 30 days";
  DatePreset2["Custom"] = "Custom";
  return DatePreset2;
})(DatePreset || {});
var SIMPLE_PRESETS = Object.values(DatePreset).filter(
  (preset) => preset !== "Custom" /* Custom */
);
var isDateFilterState = (state) => state.type === "date" /* DATE */;
var getDefaultDateFilterState = () => ({
  type: "date" /* DATE */,
  start: null,
  end: null,
  isApplied: false
});
var DateFilter = ({
  updateFilterState,
  state,
  isDateOnly = false,
  presets = SIMPLE_PRESETS
}) => {
  const [showCustomOptions, setShowCustomOptions] = _react.useState.call(void 0, 
    state.preset === "Custom" /* Custom */
  );
  const [customDateRangeData, setCustomDateRangeData] = _react.useState.call(void 0, );
  _react.useEffect.call(void 0, () => {
    setShowCustomOptions(state.preset === "Custom" /* Custom */);
  }, [state]);
  const updateStateWithCustomRange = (customDateRangeData2) => {
    setCustomDateRangeData(customDateRangeData2);
    updateFilterState(_chunkZ6EUSUADcjs.__spreadValues.call(void 0, {
      type: "date" /* DATE */,
      preset: "Custom" /* Custom */,
      isApplied: true,
      customRangeOperation: customDateRangeData2.type
    }, dateRangeOptions[customDateRangeData2.type].getRangeData(
      customDateRangeData2
    )));
  };
  const dateRangeOptions = {
    ["is in the last" /* IsInTheLast */]: {
      type: "is in the last" /* IsInTheLast */,
      defaultData: {
        type: "is in the last" /* IsInTheLast */,
        value: 1,
        granularity: "hours" /* Hours */
      },
      getRangeData: (data) => {
        const { value, granularity } = data;
        return {
          start: _chunkFBWTYGYEcjs.subtractTime.call(void 0, /* @__PURE__ */ new Date(), value, granularity),
          end: /* @__PURE__ */ new Date()
        };
      },
      components: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, TimeAndGranularity, { children: [
        /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
          _chunkILBP4CTUcjs.TextInput,
          {
            placeholder: "60",
            onChange: (value) => {
              updateStateWithCustomRange({
                type: "is in the last" /* IsInTheLast */,
                value: parseInt(value) || 0,
                granularity: customDateRangeData == null ? void 0 : customDateRangeData.granularity
              });
            },
            value: `${customDateRangeData == null ? void 0 : customDateRangeData.value}` || ""
          }
        ),
        /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
          _chunk6HFFGWQXcjs.Select_default,
          {
            defaultValue: {
              value: customDateRangeData == null ? void 0 : customDateRangeData.granularity,
              label: customDateRangeData == null ? void 0 : customDateRangeData.granularity
            },
            options: Object.values(_chunkFBWTYGYEcjs.TimeGranularity).map((timeGranularity) => ({
              value: timeGranularity,
              label: timeGranularity
            })),
            onChange: (option) => {
              if (option) {
                updateStateWithCustomRange(_chunkZ6EUSUADcjs.__spreadProps.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, {}, customDateRangeData), {
                  granularity: option.value
                }));
              }
            },
            zIndex: _chunkZU72XWNEcjs.z.modalContainer
          }
        )
      ] })
    },
    ["is equal to" /* IsEqualTo */]: {
      type: "is equal to" /* IsEqualTo */,
      defaultData: { type: "is equal to" /* IsEqualTo */, date: /* @__PURE__ */ new Date() },
      getRangeData: (data) => {
        const { date } = data;
        return { start: _chunkFBWTYGYEcjs.startOfDay.call(void 0, date), end: _chunkFBWTYGYEcjs.endOfDay.call(void 0, date) };
      },
      components: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        _chunkBLUUIEGRcjs.DateWidget,
        {
          dateRangeOperation: "is equal to" /* IsEqualTo */,
          onChange: updateStateWithCustomRange,
          isDateOnly
        }
      )
    },
    ["is between" /* IsBetween */]: {
      type: "is between" /* IsBetween */,
      defaultData: {
        type: "is between" /* IsBetween */,
        start: _chunkFBWTYGYEcjs.subtractTime.call(void 0, /* @__PURE__ */ new Date(), 7, "days" /* Days */),
        end: /* @__PURE__ */ new Date()
      },
      getRangeData: (data) => {
        const { start, end } = data;
        return { start, end };
      },
      components: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        _chunkBLUUIEGRcjs.DateWidget,
        {
          dateRangeOperation: "is between" /* IsBetween */,
          onChange: updateStateWithCustomRange,
          isDateOnly
        }
      )
    },
    ["is after" /* IsAfter */]: {
      type: "is after" /* IsAfter */,
      defaultData: { type: "is after" /* IsAfter */, date: /* @__PURE__ */ new Date() },
      getRangeData: (data) => {
        const { date } = data;
        return { start: date, end: _chunkFBWTYGYEcjs.MAX_DATE };
      },
      components: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        _chunkBLUUIEGRcjs.DateWidget,
        {
          dateRangeOperation: "is after" /* IsAfter */,
          onChange: updateStateWithCustomRange,
          isDateOnly
        }
      )
    },
    ["is before" /* IsBefore */]: {
      type: "is before" /* IsBefore */,
      defaultData: { type: "is before" /* IsBefore */, date: /* @__PURE__ */ new Date() },
      getRangeData: (data) => {
        const { date } = data;
        return { start: _chunkFBWTYGYEcjs.MIN_DATE, end: date };
      },
      components: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        _chunkBLUUIEGRcjs.DateWidget,
        {
          dateRangeOperation: "is before" /* IsBefore */,
          onChange: updateStateWithCustomRange,
          isDateOnly
        }
      )
    }
  };
  const handlePreset = (preset) => {
    let start = void 0;
    let end = void 0;
    switch (preset) {
      case "Last hour" /* LastHour */:
        start = _chunkFBWTYGYEcjs.subtractTime.call(void 0, /* @__PURE__ */ new Date(), 1, "hours" /* Hours */);
        end = /* @__PURE__ */ new Date();
        break;
      case "Last 12 hours" /* Last12Hours */:
        start = _chunkFBWTYGYEcjs.subtractTime.call(void 0, /* @__PURE__ */ new Date(), 12, "hours" /* Hours */);
        end = /* @__PURE__ */ new Date();
        break;
      case "Today" /* Today */:
        start = _chunkFBWTYGYEcjs.startOfDay.call(void 0, /* @__PURE__ */ new Date());
        end = _chunkFBWTYGYEcjs.endOfDay.call(void 0, /* @__PURE__ */ new Date());
        break;
      case "Last 7 days" /* Last7Days */:
        start = _chunkFBWTYGYEcjs.subtractTime.call(void 0, /* @__PURE__ */ new Date(), 7, "days" /* Days */);
        end = /* @__PURE__ */ new Date();
        break;
      case "Last 14 days" /* Last14Days */:
        start = _chunkFBWTYGYEcjs.subtractTime.call(void 0, /* @__PURE__ */ new Date(), 14, "days" /* Days */);
        end = /* @__PURE__ */ new Date();
        break;
      case "Last 30 days" /* Last30Days */:
        start = _chunkFBWTYGYEcjs.subtractTime.call(void 0, /* @__PURE__ */ new Date(), 30, "days" /* Days */);
        end = /* @__PURE__ */ new Date();
        break;
      default:
        break;
    }
    if (start && end) {
      updateFilterState({
        type: "date" /* DATE */,
        preset: state.preset === preset ? void 0 : preset,
        start,
        end,
        isApplied: state.preset !== preset
      });
      setShowCustomOptions(false);
    }
  };
  const toggleCustom = () => {
    if (state.preset === "Custom" /* Custom */) {
      updateFilterState({
        type: "date" /* DATE */,
        preset: void 0,
        start: null,
        end: null,
        isApplied: false
      });
      setShowCustomOptions(false);
      return;
    }
    handleSelectCustomRangeOption("is in the last" /* IsInTheLast */);
    setShowCustomOptions(true);
  };
  const handleSelectCustomRangeOption = (operation) => {
    updateStateWithCustomRange(dateRangeOptions[operation].defaultData);
  };
  return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkY45PAGZ2cjs.Filter, { label: "date", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, Container, { children: [
    /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, ButtonsContainer, { children: [
      presets.map((preset) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        _chunkKKOBB4X5cjs.Button,
        {
          kind: preset === state.preset ? "primary" : void 0,
          text: preset,
          size: "ExtraSmall",
          onClick: () => handlePreset(preset)
        },
        preset
      )),
      /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        _chunkKKOBB4X5cjs.Button,
        {
          kind: state.preset === "Custom" /* Custom */ ? "primary" : void 0,
          text: "Custom",
          size: "ExtraSmall",
          onClick: toggleCustom
        }
      )
    ] }),
    showCustomOptions && customDateRangeData && /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, { children: [
      /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        _chunk6HFFGWQXcjs.Select_default,
        {
          defaultValue: {
            value: customDateRangeData.type,
            label: `${customDateRangeData.type}`
          },
          options: Object.values(_chunkFBWTYGYEcjs.DateRangeOperation).map((operation) => ({
            value: operation,
            label: `${operation}`
          })),
          onChange: (option) => {
            if (option) {
              handleSelectCustomRangeOption(
                option.value
              );
            }
          },
          zIndex: _chunkZU72XWNEcjs.z.modalContainer
        }
      ),
      dateRangeOptions[customDateRangeData.type].components
    ] })
  ] }) });
};
var Container = _styled2.default.div`
  display: flex;
  flex-direction: column;
  gap: ${_chunkZUAO4DW3cjs.Spacing.px.xs};
`;
var ButtonsContainer = _styled2.default.div`
  display: flex;
  gap: ${_chunkZUAO4DW3cjs.Spacing.px.xs};
  flex-wrap: wrap;
`;
var TimeAndGranularity = _styled2.default.div`
  display: flex;
  flex-direction: row;
  gap: ${_chunkZUAO4DW3cjs.Spacing.px.xs};
  & > * {
    flex: 1;
  }

  input {
    height: 100%;
  }
`;






exports.DatePreset = DatePreset; exports.isDateFilterState = isDateFilterState; exports.getDefaultDateFilterState = getDefaultDateFilterState; exports.DateFilter = DateFilter;
