import { useEffect, useState } from "react";
import { useInterval } from "react-use";
import { useSelector } from "../../../store/redux-separate-context";
import { selectHasWindowFocus, selectStopUpdatesWhenFocusIsLost } from "../../global/selectors";
import { BIGGEST_INTERVAL_NUMBER } from "../../../utils/biggest-interval-number";
import { isPrintMode } from "../../dashboard/utils/parse-url";
export const useFetchNewDataClock = ({ areCriteriaMet, preferedIntervalTime, }) => {
    const hasWindowFocus = useSelector(selectHasWindowFocus);
    const stopUpdatesWhenFocusIsLost = useSelector(selectStopUpdatesWhenFocusIsLost);
    const shouldBeUpdating = !(!hasWindowFocus && stopUpdatesWhenFocusIsLost);
    const [shouldFetch, setShouldFetch] = useState(true);
    const [shouldFetchImmediatelyAfterFocus, setShouldFetchImmediatelyAfterFocus] = useState(false);
    useEffect(() => {
        if (shouldFetchImmediatelyAfterFocus && shouldBeUpdating) {
            setShouldFetchImmediatelyAfterFocus(false);
            setShouldFetch(true);
        }
    }, [shouldFetchImmediatelyAfterFocus, setShouldFetchImmediatelyAfterFocus, shouldBeUpdating]);
    // don't use setInterval when we loose focus
    const intervalTime = ((shouldBeUpdating || !shouldFetchImmediatelyAfterFocus) && !isPrintMode)
        ? preferedIntervalTime
        : BIGGEST_INTERVAL_NUMBER;
    useInterval(() => {
        if (areCriteriaMet) {
            if (!shouldBeUpdating) {
                setShouldFetchImmediatelyAfterFocus(true);
                return;
            }
            setShouldFetch(true);
        }
        // when there's no focus, don't ask for updated data
    }, intervalTime);
    return [shouldFetch, setShouldFetch];
};
//# sourceMappingURL=use-fetch-new-data-clock.js.map