/**
 * 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.
 */

import { BATCH, CHECKBOX, DATE, MULTISELECT, NUMBER, RADIO } from "./Datagrid/addons/Filtering/constants.js";
import FilterFlyout from "./Datagrid/addons/Filtering/FilterFlyout.js";
import { useMemo } from "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 = useMemo(() => ({
		[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;
			});
		},
		[NUMBER]: (rows, id, value) => {
			if (value === "") return rows;
			const parsedValue = parseInt(value);
			return rows.filter((row) => {
				return row.values[id] === parsedValue;
			});
		},
		[CHECKBOX]: (rows, id, value) => handleMultiFilter(rows, id, value),
		[MULTISELECT]: (rows, id, value) => handleMultiFilter(rows, id, value),
		[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: 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
		});
	});
};
//#endregion
export { useFiltering as default };
