"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePeriodicSync = usePeriodicSync;
const react_1 = require("react");
const TrelloContext_1 = require("../state/TrelloContext");
function usePeriodicSync(intervalMs) {
    const { syncCache, state } = (0, TrelloContext_1.useTrello)();
    const timerRef = (0, react_1.useRef)(null);
    (0, react_1.useEffect)(() => {
        if (intervalMs <= 0)
            return;
        timerRef.current = setInterval(async () => {
            if (!state.loading) {
                try {
                    await syncCache();
                }
                catch {
                    // Silently fail - user will see stale data at worst
                }
            }
        }, intervalMs);
        return () => {
            if (timerRef.current) {
                clearInterval(timerRef.current);
            }
        };
    }, [intervalMs, syncCache, state.loading]);
}
