"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlayerUI = void 0;
exports.Player = Player;
exports.usePlayerStatus = usePlayerStatus;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const atoms_1 = require("../../lib/theme/atoms");
const player_store_1 = require("../../player-store");
const streamplace_store_1 = require("../../streamplace-store");
const ui_1 = require("../ui");
const fullscreen_1 = require("./fullscreen");
const report_modal_1 = tslib_1.__importDefault(require("./ui/report-modal"));
const OFFLINE_THRESHOLD = 10000;
exports.PlayerUI = tslib_1.__importStar(require("./ui"));
function Player(props) {
    const setIngest = (0, player_store_1.usePlayerStore)((x) => x.setIngestConnectionState);
    const clearControlsTimeout = (0, player_store_1.usePlayerStore)((x) => x.clearControlsTimeout);
    const setReportingURL = (0, player_store_1.usePlayerStore)((x) => x.setReportingURL);
    const setEmbedded = (0, player_store_1.usePlayerStore)((x) => x.setEmbedded);
    const setMode = (0, player_store_1.usePlayerStore)((x) => x.setMode);
    const reportModalOpen = (0, player_store_1.usePlayerStore)((x) => x.reportModalOpen);
    const setReportModalOpen = (0, player_store_1.usePlayerStore)((x) => x.setReportModalOpen);
    const reportSubject = (0, player_store_1.usePlayerStore)((x) => x.reportSubject);
    const setMuted = (0, streamplace_store_1.useSetMuted)();
    const muted = (0, streamplace_store_1.useMuted)();
    // if we set muted, set it and restore after
    (0, react_1.useEffect)(() => {
        let wasMuted = null;
        setTimeout(() => {
            if (props.muted != undefined) {
                wasMuted = muted;
                setMuted(props.muted);
            }
        }, 200);
        return () => {
            wasMuted !== null && setMuted(wasMuted);
        };
    }, [props.muted]);
    (0, react_1.useEffect)(() => {
        setReportingURL(props.reportingURL ?? null);
    }, [props.reportingURL]);
    (0, react_1.useEffect)(() => {
        setEmbedded(props.embedded ?? false);
    }, [props.embedded]);
    (0, react_1.useEffect)(() => {
        setMode(props.mode ?? "live");
    }, [props.mode]);
    // Will call back every few seconds to send health updates
    usePlayerStatus();
    (0, react_1.useEffect)(() => {
        setIngest(props.ingest ? "new" : null);
    }, []);
    if (typeof props.src !== "string") {
        return ((0, jsx_runtime_1.jsx)(ui_1.View, { children: (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "No source provided \uD83E\uDD37" }) }));
    }
    (0, react_1.useEffect)(() => {
        return () => {
            clearControlsTimeout();
        };
    }, []);
    return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsxs)(ui_1.View, { style: [
                atoms_1.zIndex[0],
                atoms_1.w.percent[100],
                atoms_1.h.percent[100],
                atoms_1.flex.shrink[1],
                atoms_1.layout.flex.center,
            ], children: [(0, jsx_runtime_1.jsx)(report_modal_1.default, { open: reportModalOpen, onOpenChange: setReportModalOpen, subject: reportSubject }), (0, jsx_runtime_1.jsx)(fullscreen_1.Fullscreen, { src: props.src, objectFit: props.objectFit, pictureInPictureEnabled: props.pictureInPictureEnabled, children: props.children })] }) }));
}
const POLL_INTERVAL = 5000;
function usePlayerStatus() {
    const playerStatus = (0, player_store_1.usePlayerStore)((x) => x.status);
    const url = (0, streamplace_store_1.useStreamplaceStore)((x) => x.url);
    const playerEvent = (0, player_store_1.usePlayerStore)((x) => x.playerEvent);
    const [whatDoing, setWhatDoing] = (0, react_1.useState)(player_store_1.PlayerStatus.START);
    const [whatDid, setWhatDid] = (0, react_1.useState)({});
    const [doingSince, setDoingSince] = (0, react_1.useState)(Date.now());
    const [lastUpdated, setLastUpdated] = (0, react_1.useState)(0);
    const updateWhatDid = (now) => {
        const prev = whatDid[whatDoing] ?? 0;
        const duration = now.getTime() - doingSince;
        const ret = {
            ...whatDid,
            [whatDoing]: prev + duration,
        };
        return ret;
    };
    // callback to update the status
    (0, react_1.useEffect)(() => {
        const now = new Date();
        if (playerStatus !== whatDoing) {
            setWhatDid(updateWhatDid(now));
            setWhatDoing(playerStatus);
            setDoingSince(now.getTime());
        }
    }, [playerStatus]);
    (0, react_1.useEffect)(() => {
        if (lastUpdated === 0) {
            return;
        }
        const now = new Date();
        const fullWhatDid = updateWhatDid(now);
        setWhatDid({});
        setDoingSince(now.getTime());
        playerEvent(url, now.toISOString(), "aq-played", {
            whatHappened: fullWhatDid,
        });
    }, [lastUpdated]);
    (0, react_1.useEffect)(() => {
        const interval = setInterval((_) => {
            setLastUpdated(Date.now());
        }, POLL_INTERVAL);
        return () => clearInterval(interval);
    }, []);
    return [whatDoing];
}
//# sourceMappingURL=player.js.map