"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useUnreadPolling = void 0;
const react_1 = require("react");
const useUnreadPolling = (fetchUnreadCount, intervalMs = 5000, enabled = true) => {
    (0, react_1.useEffect)(() => {
        if (!enabled)
            return;
        let intervalId = null;
        const start = () => {
            if (!intervalId) {
                fetchUnreadCount();
                intervalId = setInterval(fetchUnreadCount, intervalMs);
            }
        };
        const stop = () => {
            if (intervalId) {
                clearInterval(intervalId);
                intervalId = null;
            }
        };
        const onVis = () => {
            if (document.visibilityState === 'visible')
                start();
            else
                stop();
        };
        onVis();
        document.addEventListener('visibilitychange', onVis);
        return () => {
            stop();
            document.removeEventListener('visibilitychange', onVis);
        };
    }, [fetchUnreadCount, intervalMs, enabled]);
};
exports.useUnreadPolling = useUnreadPolling;
//# sourceMappingURL=useUnreadPolling.js.map