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

const require_runtime = require('../../../../../_virtual/_rolldown/runtime.js');
const require_index = require('../../../../../node_modules/classnames/index.js');
const require_settings = require('../../../../../settings.js');
const require_useIsomorphicEffect = require('../../../../../global/js/hooks/useIsomorphicEffect.js');
const require_usePresence = require('../../../../../global/js/hooks/usePresence.js');
const require_usePrefersReducedMotion = require('../../../../../global/js/hooks/usePrefersReducedMotion.js');
const require_ActionSet = require('../../../../ActionSet/ActionSet.js');
const require_constants = require('./constants.js');
const require_FilterProvider = require('./FilterProvider.js');
const require_useSubscribeToEventEmitter = require('./hooks/useSubscribeToEventEmitter.js');
const require_useFilters = require('./hooks/useFilters.js');
const require_useShouldDisableButtons = require('./hooks/useShouldDisableButtons.js');
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let _carbon_react = require("@carbon/react");
let _carbon_react_icons = require("@carbon/react/icons");
let _carbon_layout = require("@carbon/layout");

//#region src/components/Datagrid/Datagrid/addons/Filtering/FilterPanel.jsx
/**
* 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.
*/
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--datagrid`;
const componentClass = `${blockClass}-filter-panel`;
const defaults = {
	title: "Filter",
	closeIconDescription: "Close filter panel",
	primaryActionLabel: "Apply",
	secondaryActionLabel: "Cancel",
	searchLabelText: "Filter search",
	searchPlaceholder: "Find filters"
};
const emptyArray = [];
const FilterPanel = ({ title = defaults.title, closeIconDescription = defaults.closeIconDescription, updateMethod, filterSections, setAllFilters, onApply = () => {}, onCancel = () => {}, onPanelOpen = () => {}, onPanelClose = () => {}, showFilterSearch = false, filterPanelMinHeight = 600, primaryActionLabel = defaults.primaryActionLabel, secondaryActionLabel = defaults.secondaryActionLabel, searchLabelText = defaults.searchLabelText, searchPlaceholder = defaults.searchPlaceholder, reactTableFiltersState = emptyArray, autoHideFilters = false, isFetching = false }) => {
	/** State */
	const [showDividerLine, setShowDividerLine] = (0, react.useState)(false);
	/** Context */
	const { panelOpen, setPanelOpen, dispatch: localDispatch } = (0, react.useContext)(require_FilterProvider.FilterContext);
	const { filtersState, prevFiltersObjectArrayRef, prevFiltersRef, cancel, reset, renderFilter, filtersObjectArray, lastAppliedFilters } = require_useFilters.default({
		updateMethod,
		filters: filterSections,
		setAllFilters,
		variation: require_constants.PANEL,
		reactTableFiltersState,
		onCancel,
		panelOpen,
		autoHideFilters,
		isFetching
	});
	/** Refs */
	const filterPanelRef = (0, react.useRef)(void 0);
	const filterHeadingRef = (0, react.useRef)(void 0);
	const filterSearchRef = (0, react.useRef)(void 0);
	const actionSetRef = (0, react.useRef)(void 0);
	const innerContainerRef = (0, react.useRef)(void 0);
	/** State from hooks */
	const [shouldDisableButtons, setShouldDisableButtons] = require_useShouldDisableButtons.default({
		initialValue: true,
		filtersState,
		prevFiltersRef,
		open: panelOpen
	});
	const shouldReduceMotion = require_usePrefersReducedMotion.usePrefersReducedMotion();
	const { shouldRender } = require_usePresence.usePresence(panelOpen, filterPanelRef, shouldReduceMotion ? "filter-panel-exit-reduced" : "filter-panel-exit-left");
	const [animationComplete, setAnimationComplete] = (0, react.useState)(false);
	/** Memos */
	const showActionSet = (0, react.useMemo)(() => updateMethod === require_constants.BATCH, [updateMethod]);
	/** Methods */
	const closePanel = () => {
		cancel();
		setPanelOpen(false);
	};
	(0, react.useEffect)(() => {
		if (shouldReduceMotion) setAnimationComplete(true);
	}, [shouldReduceMotion]);
	const onAnimationStart = () => {
		setAnimationComplete(false);
	};
	const onAnimationEnd = () => {
		setAnimationComplete(!animationComplete);
	};
	const apply = () => {
		setAllFilters(filtersObjectArray);
		onApply({
			filtersState,
			filtersObjectArray,
			lastAppliedFilters
		});
		setShouldDisableButtons(true);
		prevFiltersRef.current = JSON.stringify(filtersState);
		prevFiltersObjectArrayRef.current = JSON.stringify(filtersObjectArray);
		lastAppliedFilters.current = JSON.stringify(filtersObjectArray);
		localDispatch({
			type: require_constants.SAVED_FILTERS,
			payload: { savedFilters: filtersObjectArray }
		});
	};
	const renderActionSet = () => {
		return showActionSet && /* @__PURE__ */ react.default.createElement(require_ActionSet.ActionSet, {
			actions: [{
				key: 1,
				kind: "primary",
				label: primaryActionLabel,
				onClick: apply,
				disabled: shouldDisableButtons
			}, {
				key: 2,
				kind: "secondary",
				label: secondaryActionLabel,
				onClick: cancel,
				disabled: shouldDisableButtons
			}],
			className: (0, import_classnames.default)(`${componentClass}__action-set`, { [`${componentClass}__animationComplete`]: animationComplete }),
			ref: actionSetRef
		});
	};
	const onInnerContainerScroll = (event) => {
		if (event.target.scrollTop > 0) setShowDividerLine(true);
		else setShowDividerLine(false);
	};
	/** Effects */
	(0, react.useEffect)(function liftOpenStateToParent() {
		if (panelOpen) onPanelOpen(panelOpen);
		else onPanelClose(panelOpen);
	}, [
		panelOpen,
		onPanelClose,
		onPanelOpen
	]);
	(0, react.useEffect)(function setPanelMinimumHeight() {
		filterPanelRef.current?.style.setProperty("--filter-panel-min-height", (0, _carbon_layout.rem)(filterPanelMinHeight));
	}, [filterPanelMinHeight, shouldRender]);
	require_useSubscribeToEventEmitter.default(require_constants.CLEAR_FILTERS, (tableId) => {
		reset(tableId);
	});
	const getScrollableContainerHeight = (0, react.useCallback)(() => {
		const filterHeadingHeight = filterHeadingRef.current?.getBoundingClientRect().height;
		const filterSearchHeight = filterSearchRef.current?.getBoundingClientRect().height;
		const actionSetHeight = actionSetRef.current?.getBoundingClientRect().height;
		return panelOpen ? `calc(100vh - ${filterHeadingHeight}px - ${showFilterSearch ? filterSearchHeight : 0}px - ${updateMethod === require_constants.BATCH ? actionSetHeight : 0}px)` : 0;
	}, [
		filterHeadingRef,
		filterSearchRef,
		actionSetRef,
		panelOpen,
		showFilterSearch,
		updateMethod
	]);
	require_useIsomorphicEffect.useIsomorphicEffect(() => {
		const height = getScrollableContainerHeight();
		if (innerContainerRef.current && innerContainerRef.current.style && height) innerContainerRef.current.style.height = height;
	}, [
		getScrollableContainerHeight,
		innerContainerRef,
		shouldRender
	]);
	return shouldRender ? /* @__PURE__ */ react.default.createElement("div", {
		ref: filterPanelRef,
		onAnimationEnd,
		onAnimationStart,
		className: (0, import_classnames.default)(componentClass, `${componentClass}__container`, `${componentClass}--left-placement`, {
			[`${componentClass}--open`]: panelOpen,
			[`${componentClass}--closing`]: !panelOpen,
			[`${componentClass}--reduced-motion`]: shouldReduceMotion,
			[`${componentClass}--batch`]: showActionSet,
			[`${componentClass}--instant`]: !showActionSet
		})
	}, /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("header", {
		ref: filterHeadingRef,
		className: (0, import_classnames.default)(`${componentClass}__heading`, { [`${componentClass}__heading--with-divider`]: showDividerLine })
	}, /* @__PURE__ */ react.default.createElement("div", { className: `${componentClass}__title` }, title), /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
		hasIconOnly: true,
		renderIcon: (props) => /* @__PURE__ */ react.default.createElement(_carbon_react_icons.Close, {
			size: 16,
			...props
		}),
		iconDescription: closeIconDescription,
		kind: "ghost",
		tooltipPosition: "bottom",
		tooltipAlignment: "end",
		onClick: closePanel
	}), showFilterSearch && /* @__PURE__ */ react.default.createElement("div", {
		ref: filterSearchRef,
		className: `${componentClass}__search`
	}, /* @__PURE__ */ react.default.createElement(_carbon_react.Layer, null, /* @__PURE__ */ react.default.createElement(_carbon_react.Search, {
		labelText: searchLabelText,
		placeholder: searchPlaceholder,
		size: "sm"
	})))), /* @__PURE__ */ react.default.createElement("div", {
		className: `${componentClass}__inner-container`,
		ref: innerContainerRef,
		onScroll: onInnerContainerScroll
	}, filterSections.map(({ categoryTitle = null, filters = [], hasAccordion }, index) => {
		return /* @__PURE__ */ react.default.createElement("div", {
			key: index,
			className: `${componentClass}__category`
		}, categoryTitle && /* @__PURE__ */ react.default.createElement("div", { className: `${componentClass}__category-title` }, categoryTitle), hasAccordion ? /* @__PURE__ */ react.default.createElement(_carbon_react.Accordion, null, filters.map(({ filterLabel, filter }) => {
			return /* @__PURE__ */ react.default.createElement(_carbon_react.AccordionItem, {
				title: filterLabel,
				key: filterLabel
			}, renderFilter(filter));
		})) : filters.map(({ filter }) => renderFilter(filter)));
	})), renderActionSet())) : null;
};
FilterPanel.propTypes = {
	autoHideFilters: prop_types.default.bool,
	closeIconDescription: prop_types.default.string,
	filterPanelMinHeight: prop_types.default.number,
	filterSections: prop_types.default.array,
	isFetching: prop_types.default.bool,
	onApply: prop_types.default.func,
	onCancel: prop_types.default.func,
	onPanelClose: prop_types.default.func,
	onPanelOpen: prop_types.default.func,
	open: prop_types.default.bool,
	primaryActionLabel: prop_types.default.string,
	reactTableFiltersState: prop_types.default.arrayOf(prop_types.default.shape({
		id: prop_types.default.string.isRequired,
		type: prop_types.default.string.isRequired,
		value: prop_types.default.any.isRequired
	})),
	searchLabelText: prop_types.default.string,
	searchPlaceholder: prop_types.default.string,
	secondaryActionLabel: prop_types.default.string,
	setAllFilters: prop_types.default.func,
	showFilterSearch: prop_types.default.bool,
	title: prop_types.default.string,
	updateMethod: prop_types.default.oneOf([require_constants.BATCH, require_constants.INSTANT])
};

//#endregion
exports.default = FilterPanel;