"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatOAuthStartFailureForCli = exports.buildOAuthStartFailureGuidance = void 0;
const provider_capabilities_1 = require("../provider-capabilities");
const proxy_target_resolver_1 = require("../proxy/proxy-target-resolver");
function getCauseMessage(cause) {
    if (cause instanceof Error && cause.message.trim()) {
        return cause.message.trim();
    }
    if (typeof cause === 'string' && cause.trim()) {
        return cause.trim();
    }
    return null;
}
function buildAuthCommand(provider, flag, addAccount) {
    const parts = ['ccs', provider, '--auth'];
    if (addAccount) {
        parts.push('--add');
    }
    parts.push(flag);
    return parts.join(' ');
}
function buildOAuthStartFailureGuidance(provider, options) {
    const { target, startPath, cause, addAccount } = options;
    const displayName = (0, provider_capabilities_1.getProviderDisplayName)(provider);
    const endpoint = startPath ? (0, proxy_target_resolver_1.buildProxyUrl)(target, startPath) : null;
    const causeMessage = getCauseMessage(cause);
    const retryCommand = buildAuthCommand(provider, '--paste-callback', addAccount);
    const portForwardRetryCommand = buildAuthCommand(provider, '--port-forward', addAccount);
    const callbackPort = (0, provider_capabilities_1.getOAuthCallbackPort)(provider);
    const portForwardCommand = callbackPort
        ? `ssh -L ${callbackPort}:localhost:${callbackPort} <USER>@<HOST>`
        : undefined;
    const targetDescription = target.isRemote
        ? `remote CLIProxy management API at ${target.protocol}://${target.host}:${target.port}`
        : `local CLIProxy management API at ${target.protocol}://${target.host}:${target.port}`;
    const message = `${displayName} OAuth could not start through the ${targetDescription}.`;
    const details = [
        endpoint ? `Endpoint: ${endpoint}` : null,
        causeMessage ? `Cause: ${causeMessage}` : null,
    ]
        .filter(Boolean)
        .join(' ');
    const hints = target.isRemote
        ? [
            'Verify the remote CLIProxy server is running and reachable from this machine.',
            'Check cliproxy_server.remote.management_key or auth_token in CCS config.',
            `After fixing the remote proxy, retry: ${retryCommand}`,
        ]
        : [
            'Start local CLIProxy first: ccs cliproxy start',
            `Then retry paste-callback mode: ${retryCommand}`,
            ...(portForwardCommand
                ? [
                    `For SSH/VPS auth, open a tunnel from your local machine: ${portForwardCommand}`,
                    `Then run inside that SSH session: ${portForwardRetryCommand}`,
                ]
                : []),
        ];
    return {
        error: 'cliproxy_oauth_start_failed',
        provider,
        message,
        details,
        hints,
        endpoint,
        retryCommand,
        portForwardCommand,
    };
}
exports.buildOAuthStartFailureGuidance = buildOAuthStartFailureGuidance;
function formatOAuthStartFailureForCli(guidance) {
    const lines = [guidance.message];
    if (guidance.details) {
        lines.push(guidance.details);
    }
    lines.push('Next steps:');
    lines.push(...guidance.hints.map((hint) => `  - ${hint}`));
    return lines;
}
exports.formatOAuthStartFailureForCli = formatOAuthStartFailureForCli;
//# sourceMappingURL=oauth-start-failure-guidance.js.map