/**
 * 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("./constants.js");
//#region src/components/Datagrid/Datagrid/addons/Filtering/utils.js
/**
* 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 applyInitialFilters = (filterState, initialFilters) => {
	Object.keys(filterState).forEach((key) => {
		const hasInitialFilter = initialFilters.find((filter) => filter.id === key);
		if (hasInitialFilter) filterState[key].value = hasInitialFilter.value;
	});
};
const getInitialStateFromFilters = (filters, variation, initialFilters) => {
	const initialFilterState = {};
	const setInitialState = ({ type, column, props }) => {
		if (type === "checkbox") initialFilterState[column] = {
			value: props.Checkbox.map(({ id, labelText, value, ...rest }) => ({
				id,
				labelText,
				value,
				selected: false,
				...rest
			})),
			type
		};
		else if (type === "date") initialFilterState[column] = {
			value: [null, null],
			type
		};
		else if (type === "number") initialFilterState[column] = {
			value: "",
			type
		};
		else if (type === "radio") initialFilterState[column] = {
			value: "",
			type
		};
		else if (type === "dropdown") initialFilterState[column] = {
			value: "",
			type
		};
		else if (type === "multiSelect") initialFilterState[column] = {
			value: props.MultiSelect.items.map((item) => ({
				id: typeof item === "string" ? item : item.id,
				value: typeof item === "string" ? item : item.text,
				selected: false
			})),
			type
		};
	};
	if (variation === "flyout") filters.forEach(setInitialState);
	else if (variation === "panel") filters.forEach(({ filters: sections = [] }) => {
		sections.forEach(({ filter }) => setInitialState(filter));
	});
	if (initialFilters.length > 0) applyInitialFilters(initialFilterState, initialFilters);
	return initialFilterState;
};
//#endregion
exports.getInitialStateFromFilters = getInitialStateFromFilters;
