"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseHostedDevPlayerSlot = parseHostedDevPlayerSlot;
exports.parseHostedDevAuthSelection = parseHostedDevAuthSelection;
exports.parseResetDevPlayerMode = parseResetDevPlayerMode;
exports.applyHostedDevAuthSelectionToUrl = applyHostedDevAuthSelectionToUrl;
function parseHostedDevPlayerSlot(value) {
    if (value === undefined) {
        return null;
    }
    const normalized = String(value).trim();
    if (normalized === '1' || normalized === '2' || normalized === '3' || normalized === '4') {
        return Number.parseInt(normalized, 10);
    }
    return null;
}
function parseHostedDevAuthSelection(input) {
    const normalizedMode = (input.devAuth?.trim().toLowerCase() || input.defaultMode);
    if (!input.allowPrompt && normalizedMode === 'prompt') {
        throw new Error('dev_auth_prompt_not_supported');
    }
    if (normalizedMode !== 'prompt'
        && normalizedMode !== 'anonymous'
        && normalizedMode !== 'viewer'
        && normalizedMode !== 'player') {
        throw new Error('dev_auth_invalid');
    }
    if (normalizedMode !== 'player') {
        return {
            devAuth: normalizedMode,
            player: null,
        };
    }
    const player = parseHostedDevPlayerSlot(input.player);
    if (player === null) {
        throw new Error('dev_auth_player_slot_required');
    }
    return {
        devAuth: normalizedMode,
        player,
    };
}
function parseResetDevPlayerMode(value) {
    const normalized = value?.trim().toLowerCase() || 'never';
    if (normalized === 'before' || normalized === 'after' || normalized === 'before-and-after' || normalized === 'never') {
        return normalized;
    }
    throw new Error('reset_dev_player_invalid');
}
function applyHostedDevAuthSelectionToUrl(url, selection) {
    const nextUrl = new URL(url);
    nextUrl.searchParams.set('devAuth', selection.devAuth);
    if (selection.devAuth === 'player' && selection.player) {
        nextUrl.searchParams.set('player', String(selection.player));
    }
    else {
        nextUrl.searchParams.delete('player');
    }
    return nextUrl.toString();
}
