"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.srcToUrl = srcToUrl;
const syntax_1 = require("@atproto/syntax");
const react_1 = require("react");
const player_state_1 = require("../../player-store/player-state");
const streamplace_store_1 = require("../../streamplace-store");
const protocolSuffixes = {
    m3u8: player_state_1.PlayerProtocol.HLS,
    mp4: player_state_1.PlayerProtocol.PROGRESSIVE_MP4,
    webm: player_state_1.PlayerProtocol.PROGRESSIVE_WEBM,
    webrtc: player_state_1.PlayerProtocol.WEBRTC,
};
function srcToUrl(props, protocol) {
    const url = (0, streamplace_store_1.useStreamplaceStore)((x) => x.url);
    return (0, react_1.useMemo)(() => {
        if (props.src.startsWith("at://")) {
            const aturi = new syntax_1.AtUri(props.src);
            if (aturi.collection === "place.stream.video") {
                return {
                    url: `${url}/xrpc/place.stream.playback.getVideoPlaylist?uri=${props.src}&ext=hls.m3u8`,
                    protocol: player_state_1.PlayerProtocol.HLS,
                };
            }
            else {
                throw new Error(`this player doesn't know how to play ${aturi.collection} records (${props.src})`);
            }
        }
        if (props.src.startsWith("http://") || props.src.startsWith("https://")) {
            const segments = props.src.split(/[./]/);
            const suffix = segments[segments.length - 1];
            return {
                url: props.src,
                protocol: protocolSuffixes[suffix] ?? player_state_1.PlayerProtocol.HLS,
            };
        }
        let outUrl;
        if (protocol === player_state_1.PlayerProtocol.HLS) {
            if (props.selectedRendition === "auto") {
                outUrl = `${url}/xrpc/place.stream.playback.getLivePlaylist?streamer=${props.src}`;
            }
            else {
                // todo: re-implement track selection here
                outUrl = `${url}/xrpc/place.stream.playback.getLivePlaylist?streamer=${props.src}`;
            }
        }
        else if (protocol === player_state_1.PlayerProtocol.PROGRESSIVE_MP4) {
            outUrl = `${url}/api/playback/${props.src}/stream.mp4`;
        }
        else if (protocol === player_state_1.PlayerProtocol.PROGRESSIVE_WEBM) {
            outUrl = `${url}/api/playback/${props.src}/stream.webm`;
        }
        else if (protocol === player_state_1.PlayerProtocol.WEBRTC) {
            outUrl = `${url}/api/playback/${props.src}/webrtc?rendition=${props.selectedRendition || "source"}`;
        }
        else {
            throw new Error(`unknown playback protocol: ${protocol}`);
        }
        return {
            protocol: protocol,
            url: outUrl,
        };
    }, [props.src, props.selectedRendition, protocol, url]);
}
//# sourceMappingURL=shared.js.map