"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamplaceProvider = StreamplaceProvider;
exports.BrandingFetcher = BrandingFetcher;
exports.ChatProfileCreator = ChatProfileCreator;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const profile_cache_1 = require("../context/profile-cache");
const hooks_1 = require("../hooks");
const streamplace_store_1 = require("../streamplace-store");
const streamplace_store_2 = require("../streamplace-store/streamplace-store");
const context_1 = require("./context");
const poller_1 = tslib_1.__importDefault(require("./poller"));
function StreamplaceProvider({ children, url, oauthSession, }) {
    // todo: handle url changes?
    const store = (0, react_1.useRef)((0, streamplace_store_2.makeStreamplaceStore)({ url })).current;
    (0, react_1.useEffect)(() => {
        store.setState({ url });
    }, [url]);
    (0, react_1.useEffect)(() => {
        store.setState({ oauthSession });
    }, [oauthSession]);
    return ((0, jsx_runtime_1.jsx)(context_1.StreamplaceContext.Provider, { value: { store: store }, children: (0, jsx_runtime_1.jsx)(profile_cache_1.ProfileCacheProvider, { children: (0, jsx_runtime_1.jsx)(BrandingFetcher, { children: (0, jsx_runtime_1.jsx)(ChatProfileCreator, { oauthSession: oauthSession, children: (0, jsx_runtime_1.jsx)(poller_1.default, { children: children }) }) }) }) }));
}
function BrandingFetcher({ children }) {
    const fetchBroadcasterDID = (0, streamplace_store_1.useFetchBroadcasterDID)();
    const fetchEnvConfig = (0, streamplace_store_1.useFetchEnvConfig)();
    (0, streamplace_store_1.useBrandingAutoFetch)();
    (0, hooks_1.useDocumentTitle)();
    (0, react_1.useEffect)(() => {
        fetchBroadcasterDID();
        fetchEnvConfig();
    }, [fetchBroadcasterDID, fetchEnvConfig]);
    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children });
}
function ChatProfileCreator({ oauthSession, children, }) {
    const getChatProfile = (0, streamplace_store_1.useGetChatProfile)();
    (0, react_1.useEffect)(() => {
        if (oauthSession) {
            getChatProfile();
        }
    }, [oauthSession]);
    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children });
}
//# sourceMappingURL=index.js.map