"use strict";
/**
 * CLIProxy Health Checks
 *
 * Check CLIProxy binary, config, OAuth providers, and port.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkCliproxyPort = exports.checkOAuthProviders = exports.checkCliproxyConfig = exports.checkCliproxyBinary = void 0;
const fs = __importStar(require("fs"));
const cliproxy_1 = require("../../cliproxy");
const proxy_detector_1 = require("../../cliproxy/proxy/proxy-detector");
const platform_detector_1 = require("../../cliproxy/binary/platform-detector");
const version_checker_1 = require("../../cliproxy/binary/version-checker");
/**
 * Check CLIProxy binary installation
 */
function checkCliproxyBinary() {
    if ((0, cliproxy_1.isCLIProxyInstalled)()) {
        const version = (0, cliproxy_1.getInstalledCliproxyVersion)();
        const binaryPath = (0, cliproxy_1.getCLIProxyPath)();
        // Check if version is in faulty range (v81-85)
        const isFaulty = (0, version_checker_1.isVersionFaulty)(version);
        // Check if version exceeds stable cap
        const isUnstable = (0, version_checker_1.isNewerVersion)(version, platform_detector_1.CLIPROXY_MAX_STABLE_VERSION);
        if (isFaulty) {
            return {
                id: 'cliproxy-binary',
                name: 'CLIProxy Binary',
                status: 'warning',
                message: `v${version} (faulty)`,
                details: binaryPath,
                fix: `Upgrade: ccs cliproxy install ${platform_detector_1.CLIPROXY_MAX_STABLE_VERSION.replace(/-\d+$/, '')}`,
            };
        }
        if (isUnstable) {
            return {
                id: 'cliproxy-binary',
                name: 'CLIProxy Binary',
                status: 'warning',
                message: `v${version} (experimental)`,
                details: binaryPath,
                fix: `Stable: ccs cliproxy install ${platform_detector_1.CLIPROXY_MAX_STABLE_VERSION.replace(/-\d+$/, '')}`,
            };
        }
        return {
            id: 'cliproxy-binary',
            name: 'CLIProxy Binary',
            status: 'ok',
            message: `v${version}`,
            details: binaryPath,
        };
    }
    return {
        id: 'cliproxy-binary',
        name: 'CLIProxy Binary',
        status: 'info',
        message: 'Not installed',
        details: 'Downloads on first use',
    };
}
exports.checkCliproxyBinary = checkCliproxyBinary;
/**
 * Check CLIProxy config file
 */
function checkCliproxyConfig() {
    const configPath = (0, cliproxy_1.getCliproxyConfigPath)();
    if (fs.existsSync(configPath)) {
        return {
            id: 'cliproxy-config',
            name: 'CLIProxy Config',
            status: 'ok',
            message: 'cliproxy/config.yaml',
        };
    }
    return {
        id: 'cliproxy-config',
        name: 'CLIProxy Config',
        status: 'info',
        message: 'Not created',
        details: 'Generated on first use',
    };
}
exports.checkCliproxyConfig = checkCliproxyConfig;
/**
 * Check OAuth providers authentication status
 */
function checkOAuthProviders() {
    const authStatuses = (0, cliproxy_1.getAllAuthStatus)();
    const checks = [];
    for (const status of authStatuses) {
        const providerName = status.provider.charAt(0).toUpperCase() + status.provider.slice(1);
        if (status.authenticated) {
            const lastAuth = status.lastAuth ? status.lastAuth.toLocaleDateString() : '';
            checks.push({
                id: `oauth-${status.provider}`,
                name: `${providerName} Auth`,
                status: 'ok',
                message: 'Authenticated',
                details: lastAuth,
            });
        }
        else {
            checks.push({
                id: `oauth-${status.provider}`,
                name: `${providerName} Auth`,
                status: 'info',
                message: 'Not authenticated',
                fix: `Run: ccs ${status.provider} --auth`,
            });
        }
    }
    return checks;
}
exports.checkOAuthProviders = checkOAuthProviders;
/**
 * Check CLIProxy port status
 *
 * Uses unified proxy detection (HTTP check first, then session lock, then
 * port-process). This works reliably inside Docker containers where OS-level
 * port detection tools (lsof/ss) may be unavailable.
 */
async function checkCliproxyPort() {
    const status = await (0, proxy_detector_1.detectRunningProxy)(cliproxy_1.CLIPROXY_DEFAULT_PORT);
    if (status.running && status.verified) {
        return {
            id: 'cliproxy-port',
            name: 'CLIProxy Port',
            status: 'ok',
            message: 'CLIProxy running',
            details: status.pid ? `PID ${status.pid}` : `Detected via ${status.method}`,
        };
    }
    if (status.running) {
        return {
            id: 'cliproxy-port',
            name: 'CLIProxy Port',
            status: 'warning',
            message: 'CLIProxy starting',
            details: status.pid ? `PID ${status.pid}` : `Detected via ${status.method}`,
        };
    }
    if (status.blocked) {
        return {
            id: 'cliproxy-port',
            name: 'CLIProxy Port',
            status: 'warning',
            message: status.blocker
                ? `Occupied by ${status.blocker.processName}`
                : 'Port occupied by unknown process',
            details: status.blocker ? `PID ${status.blocker.pid}` : undefined,
            ...(status.blocker && { fix: `Kill process: kill ${status.blocker.pid}` }),
        };
    }
    return {
        id: 'cliproxy-port',
        name: 'CLIProxy Port',
        status: 'info',
        message: `${cliproxy_1.CLIPROXY_DEFAULT_PORT} free`,
        details: 'Proxy not running',
    };
}
exports.checkCliproxyPort = checkCliproxyPort;
//# sourceMappingURL=cliproxy-checks.js.map