import { isString } from "@pisell/utils";
//#region src/components/dataSourceComponents/fields/utils.ts
const formFieldFilterFuncMap = {
	Checkbox: (value) => {
		return { [value ? "$isTruly" : "$isFalsy"]: true };
	},
	ColorPicker: (value) => {
		return { $eq: value };
	},
	DatePicker: (value) => {
		const [start, end] = value;
		if (!start || !end) return null;
		const startDate = isString(start) ? start : start.format("YYYY-MM-DD HH:mm:ss");
		const endDate = isString(end) ? end : end.format("YYYY-MM-DD HH:mm:ss");
		return { $and: [{ $dateAfter: startDate }, { $dateBefore: endDate }] };
	},
	Input: (value) => {
		return { $includes: value };
	},
	Radio: (value) => {
		return { $eq: value };
	},
	Select: (value, field) => {
		const key = field.type === "string" ? "$in" : "$anyOf";
		const formatValue = Array.isArray(value) ? value : [value];
		return { [key]: formatValue };
	},
	Percent: (value) => {
		return { $eq: value };
	},
	InputNumber: (value) => {
		return { $eq: value };
	},
	TimePicker: (value) => {
		return { $eq: value };
	},
	"Input.TextArea": (value) => {
		return { $includes: value };
	},
	"Input.JSON": (value) => {
		return { $includes: value };
	},
	"Input.URL": (value) => {
		return { $includes: value };
	},
	"Input.Password": (value) => {
		return { $includes: value };
	},
	DateRangePicker: (value) => {
		const [start, end] = value;
		const startDate = isString(start) ? start : start.format("YYYY-MM-DD HH:mm:ss");
		const endDate = isString(end) ? end : end.format("YYYY-MM-DD HH:mm:ss");
		return { $and: [{ $dateAfter: startDate }, { $dateBefore: endDate }] };
	},
	"Radio.Group": (value) => {
		return { $in: Array.isArray(value) ? value : [value] };
	},
	"Checkbox.Group": (value) => {
		return { $anyOf: Array.isArray(value) ? value : [value] };
	},
	RecordListWrapper: () => {
		return null;
	}
};
//#endregion
export { formFieldFilterFuncMap };
