"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLivestreamInfo = useLivestreamInfo;
const react_1 = require("react");
const livestream_store_1 = require("../livestream-store");
const player_store_1 = require("../player-store");
const streamplace_store_1 = require("../streamplace-store");
function useLivestreamInfo(url) {
    const ingest = (0, player_store_1.usePlayerStore)((x) => x.ingestConnectionState);
    const profile = (0, livestream_store_1.useLivestreamStore)((x) => x.profile);
    const endLivestream = (0, streamplace_store_1.useEndLivestream)();
    const setLocalLivestreamURI = (0, livestream_store_1.useLivestreamStore)((x) => x.setLocalLivestreamURI);
    const createStreamRecord = (0, streamplace_store_1.useCreateStreamRecord)();
    const [title, setTitle] = (0, react_1.useState)("");
    const [showCountdown, setShowCountdown] = (0, react_1.useState)(false);
    const [recordSubmitted, setRecordSubmitted] = (0, react_1.useState)(false);
    const handleSubmit = async () => {
        try {
            if (title !== "") {
                setRecordSubmitted(true);
                // Create the livestream record with title and custom url if available
                const { uri } = await createStreamRecord({
                    title,
                    canonicalUrl: url || undefined,
                });
                setLocalLivestreamURI(uri);
            }
        }
        catch (error) {
            console.error("Error creating livestream:", error);
            throw new Error("Failed to create livestream record");
        }
        finally {
            setRecordSubmitted(false);
        }
    };
    const toggleGoLive = (keyboardHeight, closeKeyboard) => {
        // Optionally close keyboard if provided
        if (closeKeyboard)
            closeKeyboard();
        setShowCountdown(true);
        // wait ~3 seconds before announcing
        setTimeout(() => {
            handleSubmit();
        }, 3000);
    };
    // Stop the current broadcast
    const toggleStopStream = () => {
        console.log("Stopping stream...");
        endLivestream();
    };
    return {
        ingest,
        profile,
        title,
        setTitle,
        showCountdown,
        setShowCountdown,
        recordSubmitted,
        setRecordSubmitted,
        handleSubmit,
        toggleGoLive,
        toggleStopStream,
    };
}
//# sourceMappingURL=useLivestreamInfo.js.map