"use strict";
/**
 * Environment Health Check - OAuth readiness diagnostics
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.runEnvironmentCheck = exports.EnvironmentChecker = void 0;
const environment_diagnostics_1 = require("../environment-diagnostics");
const ui_1 = require("../../utils/ui");
const config_manager_1 = require("../../utils/config-manager");
const types_1 = require("./types");
const config_loader_facade_1 = require("../../config/config-loader-facade");
const ora = (0, types_1.createSpinner)();
/**
 * Check environment for OAuth readiness
 * Helps diagnose Windows headless false positives
 */
class EnvironmentChecker {
    constructor() {
        this.name = 'Environment';
    }
    run(results) {
        const spinner = ora('Checking environment').start();
        const diag = (0, environment_diagnostics_1.getEnvironmentDiagnostics)();
        // Determine overall environment health
        let envStatus = 'OK';
        let envMessage = 'Browser available';
        // Check for potential issues
        if (diag.detectedHeadless) {
            if (diag.platform === 'win32' && diag.ttyStatus === 'undefined') {
                // Windows false positive - this is actually a warning
                envStatus = 'WARN';
                envMessage = 'Headless detected (may be false positive on Windows)';
            }
            else if (diag.sshSession) {
                envMessage = 'SSH session (headless mode)';
            }
            else {
                envMessage = 'Headless environment';
            }
        }
        if (envStatus === 'WARN') {
            spinner.warn();
            console.log(`  ${(0, ui_1.warn)('Environment'.padEnd(22))}  ${envMessage}`);
        }
        else {
            spinner.succeed();
            console.log(`  ${(0, ui_1.ok)('Environment'.padEnd(22))}  ${envMessage}`);
        }
        // Show key environment details
        console.log(`  ${''.padEnd(24)}  Platform: ${diag.platformName}`);
        if (diag.sshSession) {
            console.log(`  ${''.padEnd(24)}  SSH: Yes (${diag.sshReason})`);
        }
        if (diag.ttyStatus === 'undefined') {
            console.log(`  ${''.padEnd(24)}  TTY: undefined [!]`);
        }
        console.log(`  ${''.padEnd(24)}  Browser: ${diag.browserReason}`);
        // Show CCS directory and source
        const ccsDir = (0, config_loader_facade_1.getCcsDir)();
        const [dirSource] = (0, config_manager_1.getCcsDirSource)();
        const sourceLabel = dirSource === 'default' ? '' : ` (via ${dirSource})`;
        console.log(`  ${''.padEnd(24)}  CCS Dir: ${ccsDir}${sourceLabel}`);
        results.addCheck('Environment', envStatus === 'OK' ? 'success' : 'warning', envMessage, envStatus === 'WARN' ? 'If browser opens correctly, this warning can be ignored' : undefined, {
            status: envStatus,
            info: envMessage,
        });
    }
}
exports.EnvironmentChecker = EnvironmentChecker;
/**
 * Run environment check
 */
function runEnvironmentCheck(results) {
    const checker = new EnvironmentChecker();
    checker.run(results);
}
exports.runEnvironmentCheck = runEnvironmentCheck;
//# sourceMappingURL=env-check.js.map