"use strict";
/**
 * OAuth Port Health Checks - Pre-flight check for OAuth authentication
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.runOAuthChecks = exports.OAuthPortsChecker = void 0;
const ui_1 = require("../../utils/ui");
const oauth_port_diagnostics_1 = require("../oauth-port-diagnostics");
const types_1 = require("./types");
const ora = (0, types_1.createSpinner)();
/**
 * Check OAuth callback ports availability
 */
class OAuthPortsChecker {
    constructor() {
        this.name = 'OAuth Ports';
    }
    async run(results) {
        const spinner = ora('Checking OAuth callback ports').start();
        const portDiagnostics = await (0, oauth_port_diagnostics_1.checkAuthCodePorts)();
        // Count issues
        const conflicts = portDiagnostics.filter((d) => d.status === 'occupied');
        if (conflicts.length === 0) {
            spinner.succeed();
            console.log(`  ${(0, ui_1.ok)('OAuth Ports'.padEnd(22))}  All callback ports available`);
            results.addCheck('OAuth Ports', 'success', undefined, undefined, {
                status: 'OK',
                info: 'All callback ports available',
            });
        }
        else {
            spinner.warn();
            console.log(`  ${(0, ui_1.warn)('OAuth Ports'.padEnd(22))}  ${conflicts.length} port conflict(s)`);
            results.addCheck('OAuth Ports', 'warning', `${conflicts.length} port conflict(s)`, 'Close conflicting applications before OAuth', { status: 'WARN', info: `${conflicts.length} conflict(s)` });
        }
        // Show individual port status
        for (const diag of portDiagnostics) {
            const providerName = diag.provider.charAt(0).toUpperCase() + diag.provider.slice(1);
            const portStr = diag.port !== null ? `(${diag.port})` : '';
            let statusIcon;
            switch (diag.status) {
                case 'free':
                case 'cliproxy':
                    statusIcon = (0, ui_1.ok)(`${providerName} ${portStr}`.padEnd(20));
                    break;
                case 'occupied':
                    statusIcon = (0, ui_1.warn)(`${providerName} ${portStr}`.padEnd(20));
                    break;
                default:
                    statusIcon = (0, ui_1.info)(`${providerName} ${portStr}`.padEnd(20));
            }
            console.log(`  ${statusIcon}  ${diag.message}`);
            if (diag.recommendation && diag.status === 'occupied') {
                console.log(`  ${''.padEnd(24)}  Fix: ${diag.recommendation}`);
            }
        }
    }
}
exports.OAuthPortsChecker = OAuthPortsChecker;
/**
 * Run OAuth port checks
 */
async function runOAuthChecks(results) {
    const checker = new OAuthPortsChecker();
    await checker.run(results);
}
exports.runOAuthChecks = runOAuthChecks;
//# sourceMappingURL=oauth-check.js.map