"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SinglePlayerProvider = void 0;
exports.useSinglePlayerContext = useSinglePlayerContext;
exports.useCurrentPlayerId = useCurrentPlayerId;
exports.useCurrentPlayerStore = useCurrentPlayerStore;
exports.useCurrentPlayerProtocol = useCurrentPlayerProtocol;
exports.useCurrentPlayerRendition = useCurrentPlayerRendition;
exports.useCurrentPlayerIngest = useCurrentPlayerIngest;
exports.withSinglePlayer = withSinglePlayer;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const zustand_1 = require("zustand");
const player_state_1 = require("./player-state");
const player_store_1 = require("./player-store");
const SinglePlayerContext = (0, react_1.createContext)(null);
/**
 * Provider component for a single player that creates a scoped context
 * This allows components to access a specific player's state without passing IDs around
 */
const SinglePlayerProvider = ({ children, playerId: providedPlayerId, protocol = player_state_1.PlayerProtocol.WEBRTC, rendition = "auto", }) => {
    const { players, createPlayer } = (0, player_store_1.usePlayerContext)();
    // Create or get a player ID
    const playerId = (0, react_1.useMemo)(() => {
        // If a player ID is provided and exists, use it
        if (providedPlayerId && players[providedPlayerId]) {
            return providedPlayerId;
        }
        // If a player ID is provided but doesn't exist, create it
        if (providedPlayerId) {
            return createPlayer(providedPlayerId);
        }
        // Otherwise create a new player
        return createPlayer();
    }, [providedPlayerId, players, createPlayer]);
    // Get the player store
    const playerStore = (0, react_1.useMemo)(() => {
        return players[playerId];
    }, [players, playerId]);
    // Set initial protocol and rendition if provided
    react_1.default.useEffect(() => {
        if (protocol) {
            playerStore.setState((state) => ({
                ...state,
                protocol,
            }));
        }
        if (rendition) {
            playerStore.setState((state) => ({
                ...state,
                selectedRendition: rendition,
            }));
        }
    }, [playerStore, protocol, rendition]);
    // Create context value
    const contextValue = (0, react_1.useMemo)(() => ({
        playerId,
        playerStore,
    }), [playerId, playerStore]);
    return ((0, jsx_runtime_1.jsx)(SinglePlayerContext.Provider, { value: contextValue, children: children }));
};
exports.SinglePlayerProvider = SinglePlayerProvider;
/**
 * Hook to access the current single player context
 */
function useSinglePlayerContext() {
    const context = (0, react_1.useContext)(SinglePlayerContext);
    if (!context) {
        throw new Error("useSinglePlayerContext must be used within a SinglePlayerProvider");
    }
    return context;
}
/**
 * Hook to access the current player ID from the single player context
 */
function useCurrentPlayerId() {
    const { playerId } = useSinglePlayerContext();
    return playerId;
}
/**
 * Hook to access state from the current player without needing to specify the ID
 */
function useCurrentPlayerStore(selector) {
    const { playerStore } = useSinglePlayerContext();
    return (0, zustand_1.useStore)(playerStore, selector);
}
/**
 * Hook to get the protocol of the current player
 */
function useCurrentPlayerProtocol() {
    return useCurrentPlayerStore((state) => [state.protocol, state.setProtocol]);
}
/**
 * Hook to get the selected rendition of the current player
 */
function useCurrentPlayerRendition() {
    return useCurrentPlayerStore((state) => [state.selectedRendition, state.setSelectedRendition]);
}
/**
 * Hook to get the ingest state of the current player
 */
function useCurrentPlayerIngest() {
    return useCurrentPlayerStore((state) => ({
        connectionState: state.ingestConnectionState,
        setConnectionState: state.setIngestConnectionState,
        startedTimestamp: state.ingestStarted,
        setStartedTimestamp: state.setIngestStarted,
    }));
}
/**
 * HOC to wrap components with a SinglePlayerProvider
 */
function withSinglePlayer(Component) {
    return function WithSinglePlayer(props) {
        const { playerId, protocol, rendition, ...componentProps } = props;
        return ((0, jsx_runtime_1.jsx)(exports.SinglePlayerProvider, { playerId: playerId, protocol: protocol, rendition: rendition, children: (0, jsx_runtime_1.jsx)(Component, { ...componentProps }) }));
    };
}
//# sourceMappingURL=single-player-provider.js.map