"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useVideoError = exports.useVideoLoading = exports.useVideo = exports.makeVideoStore = void 0;
exports.getVideoStoreFromContext = getVideoStoreFromContext;
exports.useVideoStore = useVideoStore;
exports.useVideoStoreOptional = useVideoStoreOptional;
const react_1 = require("react");
const zustand_1 = require("zustand");
const context_1 = require("./context");
const makeVideoStore = ({ aturi }) => {
    return (0, zustand_1.createStore)()((set) => ({
        aturi,
        setAturi: (aturi) => set({ aturi }),
        video: null,
        setVideo: (video) => set({ video }),
        loading: false,
        setLoading: (loading) => set({ loading }),
        error: null,
        setError: (error) => set({ error }),
    }));
};
exports.makeVideoStore = makeVideoStore;
function getVideoStoreFromContext() {
    const context = (0, react_1.useContext)(context_1.VideoContext);
    if (!context) {
        throw new Error("useVideoStore must be used within a VideoProvider");
    }
    return context.store;
}
function useVideoStore(selector) {
    const store = getVideoStoreFromContext();
    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 VideoProvider is
// mounted (e.g. when the player is in "live" mode).
const emptyVideoStore = (0, exports.makeVideoStore)({ aturi: "" });
// Like useVideoStore, but reads against an empty store instead of throwing
// when there's no VideoProvider in the tree. Useful for mode-generic hooks
// (see useTitle) that may run outside a video context.
function useVideoStoreOptional(selector) {
    const context = (0, react_1.useContext)(context_1.VideoContext);
    const store = context?.store ?? emptyVideoStore;
    return (0, zustand_1.useStore)(store, selector);
}
/* Convenience selectors/hooks */
const useVideo = () => useVideoStore((x) => x.video);
exports.useVideo = useVideo;
const useVideoLoading = () => useVideoStore((x) => x.loading);
exports.useVideoLoading = useVideoLoading;
const useVideoError = () => useVideoStore((x) => x.error);
exports.useVideoError = useVideoError;
//# sourceMappingURL=video-store.js.map