import { UdpSearchEnums } from "../enums/unitySystemEnums.mjs";
import { c } from "react-compiler-runtime";
import "react";
import { useLocation } from "react-router-dom";
//#region src/hooks/useUdpGridFilterModel.js
const useUdpGridFilterModel = () => {
	const $ = c(4);
	const { search } = useLocation();
	let t0;
	if ($[0] !== search) {
		t0 = new URLSearchParams(search);
		$[0] = search;
		$[1] = t0;
	} else t0 = $[1];
	const searchParams = t0;
	let constructedFilterModel;
	if ($[2] !== searchParams) {
		constructedFilterModel = {};
		searchParams.forEach((value, key) => {
			if (key.startsWith(`${UdpSearchEnums.UdpQueryParamPrefix}-`)) {
				const paramBody = key.replace(`${UdpSearchEnums.UdpQueryParamPrefix}-`, "");
				const lastHyphenIndex = paramBody.lastIndexOf("-");
				if (lastHyphenIndex !== -1) {
					const field = paramBody.substring(0, lastHyphenIndex);
					const operator = paramBody.substring(lastHyphenIndex + 1).replace(/_([a-z])/g, _temp);
					if (operator.toLowerCase() === "in") constructedFilterModel[field] = {
						filterType: "set",
						values: value.split(",")
					};
					else {
						const { filterType, filterKey, parsedValue } = parseFilterValue(value);
						const condition = {
							filterType,
							type: operator === "isNull" ? value === "true" ? "blank" : "notBlank" : operator,
							...filterKey !== null && { [filterKey]: parsedValue }
						};
						if (constructedFilterModel[field]) constructedFilterModel[field].conditions.push(condition);
						else constructedFilterModel[field] = {
							filterType,
							operator: "OR",
							conditions: [condition]
						};
					}
				}
			}
		});
		$[2] = searchParams;
		$[3] = constructedFilterModel;
	} else constructedFilterModel = $[3];
	return constructedFilterModel;
};
const parseFilterValue = (value) => {
	if (value === "true" || value === "false") return {
		filterType: "text",
		filterKey: null,
		parsedValue: null
	};
	if (!isNaN(Date.parse(value)) && (value.includes("-") || value.includes("/"))) return {
		filterType: "date",
		filterKey: "dateFrom",
		parsedValue: value
	};
	if (!isNaN(value) && !isNaN(parseFloat(value))) return {
		filterType: "number",
		filterKey: "filter",
		parsedValue: Number(value)
	};
	return {
		filterType: "text",
		filterKey: "filter",
		parsedValue: value
	};
};
function _temp(_, char) {
	return char.toUpperCase();
}
//#endregion
export { useUdpGridFilterModel };

//# sourceMappingURL=useUdpGridFilterModel.mjs.map