function formatStorageModeLabel(mode) {
    if (mode === 'file')
        return 'File';
    if (mode === 'keychain')
        return 'Keychain';
    return 'Auto';
}
function buildStorageModeOption(mode) {
    return {
        value: 'credential-storage',
        label: `Credential storage: ${formatStorageModeLabel(mode)}`,
        description: 'Where hosted MCP OAuth credentials are saved'
    };
}
function formatCallbackPortLabel(port) {
    return port > 0 ? String(port) : 'Auto';
}
function buildCallbackPortOption(port) {
    return {
        value: 'callback-port',
        label: `Callback port: ${formatCallbackPortLabel(port)}`,
        description: 'Loopback port used during browser login'
    };
}
function formatCallbackUrlLabel(url) {
    return url.trim() ? url : 'Auto';
}
function buildCallbackUrlOption(url) {
    return {
        value: 'callback-url',
        label: `Callback URL: ${formatCallbackUrlLabel(url)}`,
        description: 'Redirect URL sent to the OAuth provider'
    };
}
function validateLoopbackCallbackUrl(raw) {
    let parsed;
    try {
        parsed = new URL(raw);
    }
    catch {
        return 'Callback URL must be a valid URL.';
    }
    if (parsed.protocol !== 'http:') {
        return 'Callback URL must use http.';
    }
    if (!(parsed.hostname === '127.0.0.1' || parsed.hostname === 'localhost')) {
        return 'Callback URL host must be 127.0.0.1 or localhost.';
    }
    if (!parsed.pathname) {
        return 'Callback URL must include a callback path.';
    }
    return undefined;
}
export async function handleMcpSettingsCommand(showStatus, select, input, settingsManager, onMcpSettingsChanged) {
    while (true) {
        const current = settingsManager.getMcpOAuthSettings();
        const choice = await select('MCP settings', [
            buildStorageModeOption(current.credentialsStoreMode),
            buildCallbackPortOption(current.callbackPort),
            buildCallbackUrlOption(current.callbackUrl),
            {
                value: 'reset-defaults',
                label: 'Reset defaults',
                description: 'Restore automatic storage and callback behavior'
            },
            { value: 'back', label: 'Back' }
        ]);
        if (!choice || choice === 'back') {
            return;
        }
        if (choice === 'credential-storage') {
            const next = await select('Hosted MCP credential storage', [
                {
                    value: 'auto',
                    label: 'Auto',
                    description: 'Use OS keychain when available, otherwise file storage'
                },
                {
                    value: 'keychain',
                    label: 'Keychain',
                    description: 'Always prefer OS-native secret storage'
                },
                {
                    value: 'file',
                    label: 'File',
                    description: 'Keep hosted MCP OAuth state in auth.json'
                },
                { value: 'back', label: 'Back' }
            ]);
            if (!next || next === 'back') {
                continue;
            }
            const credentialsStoreMode = next;
            settingsManager.setMcpOAuthSettings({
                ...current,
                credentialsStoreMode
            });
            await settingsManager.flush();
            await onMcpSettingsChanged?.();
            showStatus(`Hosted MCP credential storage set to ${formatStorageModeLabel(credentialsStoreMode)}.`);
            continue;
        }
        if (choice === 'callback-port') {
            const value = await input('Hosted MCP callback port', current.callbackPort > 0 ? String(current.callbackPort) : 'Leave blank for auto');
            if (value === undefined) {
                continue;
            }
            const trimmed = value.trim();
            if (!trimmed) {
                settingsManager.setMcpOAuthSettings({
                    ...current,
                    callbackPort: 0
                });
                await settingsManager.flush();
                await onMcpSettingsChanged?.();
                showStatus('Hosted MCP callback port reset to auto.');
                continue;
            }
            const parsed = Number(trimmed);
            if (!Number.isInteger(parsed) || parsed <= 0 || parsed > 65535) {
                showStatus('Hosted MCP callback port must be an integer between 1 and 65535.');
                continue;
            }
            settingsManager.setMcpOAuthSettings({
                ...current,
                callbackPort: parsed
            });
            await settingsManager.flush();
            await onMcpSettingsChanged?.();
            showStatus(`Hosted MCP callback port set to ${parsed}.`);
            continue;
        }
        if (choice === 'callback-url') {
            const value = await input('Hosted MCP callback URL', current.callbackUrl || 'Leave blank for auto');
            if (value === undefined) {
                continue;
            }
            const trimmed = value.trim();
            if (!trimmed) {
                settingsManager.setMcpOAuthSettings({
                    ...current,
                    callbackUrl: ''
                });
                await settingsManager.flush();
                await onMcpSettingsChanged?.();
                showStatus('Hosted MCP callback URL reset to auto.');
                continue;
            }
            const validationError = validateLoopbackCallbackUrl(trimmed);
            if (validationError) {
                showStatus(validationError);
                continue;
            }
            settingsManager.setMcpOAuthSettings({
                ...current,
                callbackUrl: trimmed
            });
            await settingsManager.flush();
            await onMcpSettingsChanged?.();
            showStatus(`Hosted MCP callback URL set to ${trimmed}.`);
            continue;
        }
        if (choice === 'reset-defaults') {
            settingsManager.setMcpOAuthSettings({
                credentialsStoreMode: 'auto',
                callbackPort: 0,
                callbackUrl: ''
            });
            await settingsManager.flush();
            await onMcpSettingsChanged?.();
            showStatus('Hosted MCP settings reset to defaults.');
        }
    }
}
//# sourceMappingURL=mcp-settings-command.js.map