Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_chunk = require("../../chunk-BN4qSVkS.cjs");
const require_styles_z_index = require("../../styles/z-index.cjs");
const require_components_Button = require("../Button.cjs");
const require_styles_tokens_spacing = require("../../styles/tokens/spacing.cjs");
const require_components_Select = require("../Select.cjs");
const require_components_TextInput = require("../TextInput.cjs");
const require_components_DataManagerTable_Filter = require("./Filter.cjs");
require("./filters.cjs");
const require_components_DataManagerTable_date_utils = require("./date_utils.cjs");
const require_components_DataManagerTable_DateWidget = require("./DateWidget.cjs");
let _emotion_styled = require("@emotion/styled");
_emotion_styled = require_chunk.__toESM(_emotion_styled);
let react = require("react");
let _emotion_react_jsx_runtime = require("@emotion/react/jsx-runtime");
//#region src/components/DataManagerTable/DateFilter.tsx
let DatePreset = /* @__PURE__ */ function(DatePreset) {
	DatePreset["LastHour"] = "Last hour";
	DatePreset["Last12Hours"] = "Last 12 hours";
	DatePreset["Today"] = "Today";
	DatePreset["Last7Days"] = "Last 7 days";
	DatePreset["Last14Days"] = "Last 14 days";
	DatePreset["Last30Days"] = "Last 30 days";
	DatePreset["Custom"] = "Custom";
	return DatePreset;
}({});
const SIMPLE_PRESETS = Object.values(DatePreset).filter((preset) => preset !== "Custom");
const isDateFilterState = (state) => state.type === "date";
const getDefaultDateFilterState = () => ({
	type: "date",
	start: null,
	end: null,
	isApplied: false
});
const DateFilter = ({ updateFilterState, state, isDateOnly = false, presets = SIMPLE_PRESETS }) => {
	const [showCustomOptions, setShowCustomOptions] = (0, react.useState)(state.preset === "Custom");
	const [customDateRangeData, setCustomDateRangeData] = (0, react.useState)();
	(0, react.useEffect)(() => {
		setShowCustomOptions(state.preset === "Custom");
	}, [state]);
	const updateStateWithCustomRange = (customDateRangeData) => {
		setCustomDateRangeData(customDateRangeData);
		updateFilterState({
			type: "date",
			preset: "Custom",
			isApplied: true,
			customRangeOperation: customDateRangeData.type,
			...dateRangeOptions[customDateRangeData.type].getRangeData(customDateRangeData)
		});
	};
	const dateRangeOptions = {
		["is in the last"]: {
			type: "is in the last",
			defaultData: {
				type: "is in the last",
				value: 1,
				granularity: "hours"
			},
			getRangeData: (data) => {
				const { value, granularity } = data;
				return {
					start: require_components_DataManagerTable_date_utils.subtractTime(/* @__PURE__ */ new Date(), value, granularity),
					end: /* @__PURE__ */ new Date()
				};
			},
			components: /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsxs)(TimeAndGranularity, { children: [/* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_TextInput.TextInput, {
				placeholder: "60",
				onChange: (value) => {
					updateStateWithCustomRange({
						type: "is in the last",
						value: parseInt(value) || 0,
						granularity: customDateRangeData?.granularity
					});
				},
				value: `${customDateRangeData?.value}` || ""
			}), /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_Select.Select, {
				defaultValue: {
					value: customDateRangeData?.granularity,
					label: customDateRangeData?.granularity
				},
				options: Object.values(require_components_DataManagerTable_date_utils.TimeGranularity).map((timeGranularity) => ({
					value: timeGranularity,
					label: timeGranularity
				})),
				onChange: (option) => {
					if (option) updateStateWithCustomRange({
						...customDateRangeData,
						granularity: option.value
					});
				},
				zIndex: require_styles_z_index.z.dropdown
			})] })
		},
		["is equal to"]: {
			type: "is equal to",
			defaultData: {
				type: "is equal to",
				date: /* @__PURE__ */ new Date()
			},
			getRangeData: (data) => {
				const { date } = data;
				return {
					start: require_components_DataManagerTable_date_utils.startOfDay(date),
					end: require_components_DataManagerTable_date_utils.endOfDay(date)
				};
			},
			components: /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_DataManagerTable_DateWidget.DateWidget, {
				dateRangeOperation: "is equal to",
				onChange: updateStateWithCustomRange,
				isDateOnly
			})
		},
		["is between"]: {
			type: "is between",
			defaultData: {
				type: "is between",
				start: require_components_DataManagerTable_date_utils.subtractTime(/* @__PURE__ */ new Date(), 7, "days"),
				end: /* @__PURE__ */ new Date()
			},
			getRangeData: (data) => {
				const { start, end } = data;
				return {
					start,
					end
				};
			},
			components: /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_DataManagerTable_DateWidget.DateWidget, {
				dateRangeOperation: "is between",
				onChange: updateStateWithCustomRange,
				isDateOnly
			})
		},
		["is after"]: {
			type: "is after",
			defaultData: {
				type: "is after",
				date: /* @__PURE__ */ new Date()
			},
			getRangeData: (data) => {
				const { date } = data;
				return {
					start: date,
					end: require_components_DataManagerTable_date_utils.MAX_DATE
				};
			},
			components: /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_DataManagerTable_DateWidget.DateWidget, {
				dateRangeOperation: "is after",
				onChange: updateStateWithCustomRange,
				isDateOnly
			})
		},
		["is before"]: {
			type: "is before",
			defaultData: {
				type: "is before",
				date: /* @__PURE__ */ new Date()
			},
			getRangeData: (data) => {
				const { date } = data;
				return {
					start: require_components_DataManagerTable_date_utils.MIN_DATE,
					end: date
				};
			},
			components: /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_DataManagerTable_DateWidget.DateWidget, {
				dateRangeOperation: "is before",
				onChange: updateStateWithCustomRange,
				isDateOnly
			})
		}
	};
	const handlePreset = (preset) => {
		let start = void 0;
		let end = void 0;
		switch (preset) {
			case "Last hour":
				start = require_components_DataManagerTable_date_utils.subtractTime(/* @__PURE__ */ new Date(), 1, "hours");
				end = /* @__PURE__ */ new Date();
				break;
			case "Last 12 hours":
				start = require_components_DataManagerTable_date_utils.subtractTime(/* @__PURE__ */ new Date(), 12, "hours");
				end = /* @__PURE__ */ new Date();
				break;
			case "Today":
				start = require_components_DataManagerTable_date_utils.startOfDay(/* @__PURE__ */ new Date());
				end = require_components_DataManagerTable_date_utils.endOfDay(/* @__PURE__ */ new Date());
				break;
			case "Last 7 days":
				start = require_components_DataManagerTable_date_utils.subtractTime(/* @__PURE__ */ new Date(), 7, "days");
				end = /* @__PURE__ */ new Date();
				break;
			case "Last 14 days":
				start = require_components_DataManagerTable_date_utils.subtractTime(/* @__PURE__ */ new Date(), 14, "days");
				end = /* @__PURE__ */ new Date();
				break;
			case "Last 30 days":
				start = require_components_DataManagerTable_date_utils.subtractTime(/* @__PURE__ */ new Date(), 30, "days");
				end = /* @__PURE__ */ new Date();
				break;
			default: break;
		}
		if (start && end) {
			updateFilterState({
				type: "date",
				preset: state.preset === preset ? void 0 : preset,
				start,
				end,
				isApplied: state.preset !== preset
			});
			setShowCustomOptions(false);
		}
	};
	const toggleCustom = () => {
		if (state.preset === "Custom") {
			updateFilterState({
				type: "date",
				preset: void 0,
				start: null,
				end: null,
				isApplied: false
			});
			setShowCustomOptions(false);
			return;
		}
		handleSelectCustomRangeOption("is in the last");
		setShowCustomOptions(true);
	};
	const handleSelectCustomRangeOption = (operation) => {
		updateStateWithCustomRange(dateRangeOptions[operation].defaultData);
	};
	return /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_DataManagerTable_Filter.Filter, {
		label: "date",
		children: /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsxs)(Container, { children: [/* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsxs)(ButtonsContainer, { children: [presets.map((preset) => /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_Button.Button, {
			kind: preset === state.preset ? "primary" : void 0,
			text: preset,
			size: "ExtraSmall",
			onClick: () => handlePreset(preset)
		}, preset)), /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_Button.Button, {
			kind: state.preset === "Custom" ? "primary" : void 0,
			text: "Custom",
			size: "ExtraSmall",
			onClick: toggleCustom
		})] }), showCustomOptions && customDateRangeData && /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsxs)(_emotion_react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_Select.Select, {
			defaultValue: {
				value: customDateRangeData.type,
				label: `${customDateRangeData.type}`
			},
			options: Object.values(require_components_DataManagerTable_date_utils.DateRangeOperation).map((operation) => ({
				value: operation,
				label: `${operation}`
			})),
			onChange: (option) => {
				if (option) handleSelectCustomRangeOption(option.value);
			},
			zIndex: require_styles_z_index.z.dropdown
		}), dateRangeOptions[customDateRangeData.type].components] })] })
	});
};
const Container = _emotion_styled.default.div`
  display: flex;
  flex-direction: column;
  gap: ${require_styles_tokens_spacing.Spacing.px.xs};
`;
const ButtonsContainer = _emotion_styled.default.div`
  display: flex;
  gap: ${require_styles_tokens_spacing.Spacing.px.xs};
  flex-wrap: wrap;
`;
const TimeAndGranularity = _emotion_styled.default.div`
  display: flex;
  flex-direction: row;
  gap: ${require_styles_tokens_spacing.Spacing.px.xs};
  & > * {
    flex: 1;
  }

  input {
    height: 100%;
  }
`;
//#endregion
exports.DateFilter = DateFilter;
exports.DatePreset = DatePreset;
exports.getDefaultDateFilterState = getDefaultDateFilterState;
exports.isDateFilterState = isDateFilterState;
