"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteTeleport = deleteTeleport;
exports.createTeleport = createTeleport;
exports.registerTeleportCommand = registerTeleportCommand;
const slash_commands_1 = require("../slash-commands");
async function deleteTeleport(pdsAgent, userDID, uri) {
    const rkey = uri.split("/").pop();
    if (!rkey) {
        throw new Error("No rkey found in teleport URI");
    }
    return await pdsAgent.com.atproto.repo.deleteRecord({
        repo: userDID,
        collection: "place.stream.live.teleport",
        rkey: rkey,
    });
}
async function createTeleport(pdsAgent, userDID, targetHandle, countdownSeconds, setActiveTeleportUri) {
    if (countdownSeconds < 5 || countdownSeconds > 300) {
        return {
            success: false,
            error: "Countdown must be between 5 seconds and 5 minutes",
        };
    }
    let targetDID;
    try {
        const resolution = await pdsAgent.resolveHandle({
            handle: targetHandle,
        });
        targetDID = resolution.data.did;
    }
    catch (err) {
        return {
            success: false,
            error: `Could not resolve handle: ${targetHandle}`,
        };
    }
    if (targetDID === userDID) {
        return {
            success: false,
            error: "You cannot teleport to yourself",
        };
    }
    const startsAt = new Date(Date.now() + countdownSeconds * 1000).toISOString();
    const record = {
        $type: "place.stream.live.teleport",
        streamer: targetDID,
        startsAt,
        countdownSeconds,
    };
    try {
        const result = await pdsAgent.com.atproto.repo.createRecord({
            repo: userDID,
            collection: "place.stream.live.teleport",
            record,
        });
        if (setActiveTeleportUri) {
            setActiveTeleportUri(result.data.uri);
        }
        return { success: true };
    }
    catch (err) {
        return {
            success: false,
            error: err instanceof Error ? err.message : "Failed to create teleport",
        };
    }
}
function registerTeleportCommand(pdsAgent, userDID, setActiveTeleportUri, onOpenModal) {
    const teleportHandler = async (args, rawInput) => {
        if (args.length === 0) {
            if (onOpenModal) {
                onOpenModal();
                return { handled: true };
            }
            return {
                handled: true,
                error: "Usage: /teleport @handle.bsky.social [duration_seconds]",
            };
        }
        let targetHandle = args[0];
        if (targetHandle.startsWith("@")) {
            targetHandle = targetHandle.slice(1);
        }
        if (!targetHandle.includes(".")) {
            return {
                handled: true,
                error: "Invalid handle format. Expected: handle.bsky.social",
            };
        }
        let countdownSeconds = 10;
        if (args.length > 1) {
            const parsedDuration = parseInt(args[1], 10);
            if (isNaN(parsedDuration)) {
                return {
                    handled: true,
                    error: "Countdown must be a number (seconds)",
                };
            }
            if (parsedDuration < 5 || parsedDuration > 300) {
                return {
                    handled: true,
                    error: "Countdown must be between 5 seconds and 5 minutes",
                };
            }
            countdownSeconds = parsedDuration;
        }
        let targetDID;
        try {
            const resolution = await pdsAgent.resolveHandle({
                handle: targetHandle,
            });
            targetDID = resolution.data.did;
        }
        catch (err) {
            return {
                handled: true,
                error: `Could not resolve handle: ${targetHandle}`,
            };
        }
        if (targetDID === userDID) {
            return {
                handled: true,
                error: "You cannot teleport to yourself",
            };
        }
        const startsAt = new Date(Date.now() + countdownSeconds * 1000).toISOString();
        const record = {
            $type: "place.stream.live.teleport",
            streamer: targetDID,
            startsAt,
            countdownSeconds,
        };
        try {
            const result = await pdsAgent.com.atproto.repo.createRecord({
                repo: userDID,
                collection: "place.stream.live.teleport",
                record,
            });
            // store the URI in the livestream store
            if (setActiveTeleportUri) {
                setActiveTeleportUri(result.data.uri);
            }
            return { handled: true };
        }
        catch (err) {
            return {
                handled: true,
                error: err instanceof Error ? err.message : "Failed to create teleport",
            };
        }
    };
    (0, slash_commands_1.registerSlashCommand)({
        name: "teleport",
        description: "Start a teleport to another streamer",
        usage: "/teleport @handle.bsky.social [duration_seconds]",
        handler: teleportHandler,
    });
    (0, slash_commands_1.registerSlashCommand)({
        name: "tp",
        description: "Start a teleport to another streamer (alias for /teleport)",
        usage: "/tp @handle.bsky.social [duration_seconds]",
        handler: teleportHandler,
    });
}
//# sourceMappingURL=teleport.js.map