"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useRenditions = exports.useRecentSegments = exports.useSegment = exports.useLivestream = exports.useViewers = exports.useProfile = exports.usePinnedComment = exports.useChat = exports.useHandleWebsocketMessages = exports.makeLivestreamStore = void 0;
exports.getStoreFromContext = getStoreFromContext;
exports.useLivestreamStore = useLivestreamStore;
exports.useLivestreamStoreOptional = useLivestreamStoreOptional;
const react_1 = require("react");
const zustand_1 = require("zustand");
const context_1 = require("./context");
const websocket_consumer_1 = require("./websocket-consumer");
const makeLivestreamStore = () => {
    return (0, zustand_1.createStore)()((set) => ({
        profile: null,
        chatIndex: {},
        chat: [],
        livestream: null,
        viewers: null,
        pendingHides: [],
        segment: null,
        renditions: [],
        replyToMessage: null,
        chatDraft: "",
        badgeSlots: null,
        streamKey: null,
        setStreamKey: (sk) => set({ streamKey: sk }),
        authors: {},
        recentSegments: [],
        problems: [],
        activeTeleport: null,
        activeTeleportUri: null,
        setActiveTeleportUri: (uri) => set({ activeTeleportUri: uri }),
        websocketConnected: false,
        hasReceivedSegment: false,
        pinnedComment: null,
        moderationPermissions: [],
        setModerationPermissions: (perms) => set({ moderationPermissions: perms }),
        localLivestreamURI: null,
        setLocalLivestreamURI: (uri) => set({ localLivestreamURI: uri }),
    }));
};
exports.makeLivestreamStore = makeLivestreamStore;
function getStoreFromContext() {
    const context = (0, react_1.useContext)(context_1.LivestreamContext);
    if (!context) {
        throw new Error("useLivestreamStore must be used within a LivestreamProvider");
    }
    return context.store;
}
function useLivestreamStore(selector) {
    const store = getStoreFromContext();
    return (0, zustand_1.useStore)(store, selector);
}
// A shared, never-populated store used as a fallback so the optional hook
// below can call useStore unconditionally even when no LivestreamProvider is
// mounted (e.g. when the player is in "vod" mode).
const emptyLivestreamStore = (0, exports.makeLivestreamStore)();
// Like useLivestreamStore, but reads against an empty store instead of
// throwing when there's no LivestreamProvider in the tree. Useful for
// mode-generic hooks (see useTitle) that may run outside a livestream context.
function useLivestreamStoreOptional(selector) {
    const context = (0, react_1.useContext)(context_1.LivestreamContext);
    const store = context?.store ?? emptyLivestreamStore;
    return (0, zustand_1.useStore)(store, selector);
}
const useHandleWebsocketMessages = () => {
    const store = getStoreFromContext();
    return (messages) => {
        store.setState((state) => (0, websocket_consumer_1.handleWebSocketMessages)(state, messages));
    };
};
exports.useHandleWebsocketMessages = useHandleWebsocketMessages;
const useChat = () => useLivestreamStore((x) => x.chat);
exports.useChat = useChat;
const usePinnedComment = () => useLivestreamStore((x) => x.pinnedComment);
exports.usePinnedComment = usePinnedComment;
const useProfile = () => useLivestreamStore((x) => x.profile);
exports.useProfile = useProfile;
const useViewers = () => useLivestreamStore((x) => x.viewers);
exports.useViewers = useViewers;
const useLivestream = (includeEnded = false) => useLivestreamStore((x) => {
    const ls = x.livestream;
    if (!ls)
        return null;
    if (!includeEnded && ls.record.endedAt !== undefined)
        return null;
    return ls;
});
exports.useLivestream = useLivestream;
const useSegment = () => useLivestreamStore((x) => x.segment);
exports.useSegment = useSegment;
const useRecentSegments = () => useLivestreamStore((x) => x.recentSegments);
exports.useRecentSegments = useRecentSegments;
const useRenditions = () => useLivestreamStore((x) => x.renditions);
exports.useRenditions = useRenditions;
//# sourceMappingURL=livestream-store.js.map