// (C) 2022-2026 GoodData Corporation
import { useEffect, useRef } from "react";
import { useDashboardDrop } from "../../../dragAndDrop/useDashboardDrop.js";
export function useFilterExpansionByDragAndDrop(containsMultipleRows, isFilterBarExpanded, setFilterBarExpanded) {
    const lastCanDrop = useRef(false);
    const lastMultiRows = useRef(false);
    const shouldBeExpandedAfterDrop = useRef(false);
    const [{ canDrop }, dropRef] = useDashboardDrop(["attributeFilter", "dateFilter", "attributeFilter-placeholder"], {
        drop: (_, monitor) => {
            if (monitor.didDrop()) {
                shouldBeExpandedAfterDrop.current = true;
            }
        },
    }, []);
    useEffect(() => {
        const dragStateUnchanged = canDrop === lastCanDrop.current;
        const multiRowsUnchanged = containsMultipleRows === lastMultiRows.current;
        if (dragStateUnchanged && multiRowsUnchanged) {
            return;
        }
        if (canDrop && containsMultipleRows) {
            shouldBeExpandedAfterDrop.current = isFilterBarExpanded;
            setFilterBarExpanded(true);
        }
        else if (shouldBeExpandedAfterDrop.current !== isFilterBarExpanded) {
            setFilterBarExpanded(shouldBeExpandedAfterDrop.current);
            shouldBeExpandedAfterDrop.current = false;
        }
        lastCanDrop.current = canDrop;
        lastMultiRows.current = containsMultipleRows;
    }, [canDrop, containsMultipleRows, isFilterBarExpanded, setFilterBarExpanded]);
    return dropRef;
}
