"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Poller;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const streamplace_1 = require("streamplace");
const streamplace_store_1 = require("../streamplace-store");
const xrpc_1 = require("../streamplace-store/xrpc");
const time_sync_1 = require("../time-sync");
function Poller({ children }) {
    const url = (0, streamplace_store_1.useStreamplaceStore)((state) => state.url);
    const setLiveUsers = (0, streamplace_store_1.useStreamplaceStore)((state) => state.setLiveUsers);
    const did = (0, streamplace_store_1.useDID)();
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    const getChatProfile = (0, streamplace_store_1.useGetChatProfile)();
    const getBskyProfile = (0, streamplace_store_1.useGetBskyProfile)();
    const liveUserRefresh = (0, streamplace_store_1.useStreamplaceStore)((state) => state.liveUsersRefresh);
    (0, time_sync_1.useTimeSync)();
    (0, react_1.useEffect)(() => {
        if (pdsAgent && did) {
            getChatProfile();
            getBskyProfile();
        }
    }, [pdsAgent, did]);
    (0, react_1.useEffect)(() => {
        const agent = new streamplace_1.StreamplaceAgent(url);
        const go = async () => {
            setLiveUsers({
                liveUsersLoading: true,
            });
            try {
                const res = await agent.place.stream.live.getLiveUsers();
                setLiveUsers({
                    liveUsers: res.data.streams || [],
                    liveUsersLoading: false,
                    liveUsersError: null,
                });
            }
            catch (e) {
                setLiveUsers({
                    liveUsersLoading: false,
                    liveUsersError: e.message,
                });
            }
        };
        go();
        const handle = setInterval(go, 3000);
        return () => clearInterval(handle);
    }, [url, liveUserRefresh]);
    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children });
}
//# sourceMappingURL=poller.js.map