// (C) 2021-2026 GoodData Corporation
import { useCallback, useEffect, useState } from "react";
import { useDashboardDispatch, useDashboardSelector, } from "../../../../model/react/DashboardStoreProvider.js";
import { uiActions } from "../../../../model/store/ui/index.js";
import { selectFilterBarExpanded } from "../../../../model/store/ui/uiSelectors.js";
import { CalculatedRowsDefault } from "./useRowsCalculator.js";
//NOTE: This 1px is size of border bottom on filter bar
const BorderWidth = 1;
//NOTE: This must be same value as $transition-length
const TransitionLength = 200;
export function useFilterBarState() {
    const [calculatedRows, setCalculatedRows] = useState(CalculatedRowsDefault);
    const { expandedHeight, collapsedHeight, rows } = calculatedRows;
    const dispatch = useDashboardDispatch();
    const isFilterBarExpanded = useDashboardSelector(selectFilterBarExpanded);
    const scrollable = useScrollable(isFilterBarExpanded);
    const setFilterBarExpanded = useCallback((isExpanded) => dispatch(uiActions.setFilterBarExpanded(isExpanded)), [dispatch]);
    const innerHeight = isFilterBarExpanded ? expandedHeight : collapsedHeight;
    return {
        rows,
        scrollable,
        height: innerHeight + BorderWidth,
        isFilterBarExpanded,
        setCalculatedRows,
        setFilterBarExpanded,
    };
}
function useScrollable(expanded) {
    const [scrollable, setScrollable] = useState(false);
    useEffect(() => {
        let timer = null;
        if (expanded) {
            timer = window.setTimeout(() => setScrollable(true), TransitionLength);
        }
        else {
            setScrollable(false);
        }
        return () => {
            if (timer !== null) {
                window.clearTimeout(timer);
            }
        };
    }, [expanded]);
    return scrollable;
}
