/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

require("../../_virtual/_rolldown/runtime.js");
const require_constants = require("./Datagrid/addons/Filtering/constants.js");
const require_FilterFlyout = require("./Datagrid/addons/Filtering/FilterFlyout.js");
let react = require("react");
//#region src/components/Datagrid/useFiltering.ts
/**
* Copyright IBM Corp. 2022, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const exactText = (rows, ids, filterValue) => {
	return rows.filter((row) => {
		return ids.some((id) => {
			const rowValue = row.values[id];
			return rowValue !== void 0 ? String(rowValue).toLowerCase() === String(filterValue).toLowerCase() : true;
		});
	});
};
const handleMultiFilter = (rows, id, value) => {
	const selectedItems = value.filter((item) => item.selected).map((item) => item.value);
	if (selectedItems.length === 0) return rows;
	return rows.filter((row) => {
		const rowValue = row.values[id];
		return selectedItems.includes(rowValue);
	});
};
const useFiltering = (hooks) => {
	/* istanbul ignore next */
	const filterTypes = (0, react.useMemo)(() => ({
		[require_constants.DATE]: (rows, id, [startDate, endDate]) => {
			return rows.filter((row) => {
				const rowValue = row.values[id];
				const startDateObj = typeof startDate === "object" ? startDate : new Date(startDate);
				const endDateObj = typeof endDate === "object" ? endDate : new Date(endDate);
				const rowValueDateObj = typeof rowValue === "object" ? rowValue : new Date(rowValue);
				if (rowValueDateObj.getTime() <= endDateObj.getTime() && rowValueDateObj.getTime() >= startDateObj.getTime()) return true;
				else return false;
			});
		},
		[require_constants.NUMBER]: (rows, id, value) => {
			if (value === "") return rows;
			const parsedValue = parseInt(value);
			return rows.filter((row) => {
				return row.values[id] === parsedValue;
			});
		},
		[require_constants.CHECKBOX]: (rows, id, value) => handleMultiFilter(rows, id, value),
		[require_constants.MULTISELECT]: (rows, id, value) => handleMultiFilter(rows, id, value),
		[require_constants.RADIO]: (rows, id, value) => exactText(rows, id, value)
	}), []);
	hooks.useInstance.push((instance) => {
		const { filterProps, setAllFilters, setFilter, headers, data, state } = instance;
		const defaultProps = {
			variation: "flyout",
			updateMethod: require_constants.BATCH,
			panelIconDescription: "Open filter panel",
			reactTableFiltersState: state.filters
		};
		const getFilterFlyoutProps = () => ({
			...defaultProps,
			...filterProps,
			setAllFilters,
			setFilter,
			headers,
			data
		});
		Object.assign(instance, {
			filterProps: {
				...defaultProps,
				...instance?.filterProps
			},
			filterTypes,
			getFilterFlyoutProps,
			FilterFlyout: require_FilterFlyout.default
		});
	});
};
//#endregion
exports.default = useFiltering;
