"use strict";
/**
 * Profile Health Checks
 *
 * Check profiles, instances, and delegation.
 * Supports both legacy (config.json) and unified (config.yaml) modes.
 */
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.checkDelegation = exports.checkInstances = exports.checkProfiles = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const config_loader_facade_1 = require("../../config/config-loader-facade");
const instance_directory_1 = require("../../management/instance-directory");
/**
 * Check profiles configuration (API profiles from config.json or config.yaml)
 */
function checkProfiles(ccsDir) {
    // Check unified config first
    if ((0, config_loader_facade_1.isUnifiedMode)()) {
        const config = (0, config_loader_facade_1.loadUnifiedConfig)();
        if (!config) {
            return {
                id: 'profiles',
                name: 'Profiles',
                status: 'info',
                message: 'config.yaml not loaded',
            };
        }
        const profileCount = Object.keys(config.profiles || {}).length;
        const profileNames = Object.keys(config.profiles || {}).join(', ');
        if (profileCount === 0) {
            return {
                id: 'profiles',
                name: 'Profiles',
                status: 'ok',
                message: 'No API profiles (unified mode)',
            };
        }
        return {
            id: 'profiles',
            name: 'Profiles',
            status: 'ok',
            message: `${profileCount} configured (unified)`,
            details: profileNames.length > 40 ? profileNames.substring(0, 37) + '...' : profileNames,
        };
    }
    // Legacy mode: check config.json
    const configPath = path.join(ccsDir, 'config.json');
    if (!fs.existsSync(configPath)) {
        return {
            id: 'profiles',
            name: 'Profiles',
            status: 'info',
            message: 'config.json not found',
        };
    }
    try {
        const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
        if (!config.profiles || typeof config.profiles !== 'object') {
            return {
                id: 'profiles',
                name: 'Profiles',
                status: 'error',
                message: 'Missing profiles object',
                fix: 'Run: npm install -g @kaitranntt/ccs --force',
            };
        }
        const profileCount = Object.keys(config.profiles).length;
        const profileNames = Object.keys(config.profiles).join(', ');
        return {
            id: 'profiles',
            name: 'Profiles',
            status: 'ok',
            message: `${profileCount} configured`,
            details: profileNames.length > 40 ? profileNames.substring(0, 37) + '...' : profileNames,
        };
    }
    catch (e) {
        return {
            id: 'profiles',
            name: 'Profiles',
            status: 'error',
            message: e.message,
        };
    }
}
exports.checkProfiles = checkProfiles;
/**
 * Check instances (account profiles)
 */
function checkInstances(ccsDir) {
    const instancesDir = path.join(ccsDir, 'instances');
    if (!fs.existsSync(instancesDir)) {
        return {
            id: 'instances',
            name: 'Instances',
            status: 'ok',
            message: 'No account profiles',
        };
    }
    const instances = (0, instance_directory_1.listAccountInstanceNames)(instancesDir);
    if (instances.length === 0) {
        return {
            id: 'instances',
            name: 'Instances',
            status: 'ok',
            message: 'No account profiles',
        };
    }
    return {
        id: 'instances',
        name: 'Instances',
        status: 'ok',
        message: `${instances.length} account profile${instances.length !== 1 ? 's' : ''}`,
    };
}
exports.checkInstances = checkInstances;
/**
 * Check delegation setup
 */
function checkDelegation(ccsDir) {
    const ccsClaudeCommandsDir = path.join(ccsDir, '.claude', 'commands');
    const hasCcsCommand = fs.existsSync(path.join(ccsClaudeCommandsDir, 'ccs.md'));
    const hasContinueCommand = fs.existsSync(path.join(ccsClaudeCommandsDir, 'ccs', 'continue.md'));
    if (!hasCcsCommand || !hasContinueCommand) {
        return {
            id: 'delegation',
            name: 'Delegation',
            status: 'warning',
            message: 'Not installed',
            fix: 'Run: npm install -g @kaitranntt/ccs --force',
        };
    }
    const { DelegationValidator } = require('../../utils/delegation-validator');
    const readyProfiles = [];
    for (const profile of ['glm', 'kimi']) {
        const validation = DelegationValidator.validate(profile);
        if (validation.valid) {
            readyProfiles.push(profile);
        }
    }
    if (readyProfiles.length === 0) {
        return {
            id: 'delegation',
            name: 'Delegation',
            status: 'warning',
            message: 'No profiles ready',
            fix: 'Configure profiles with valid API keys',
        };
    }
    return {
        id: 'delegation',
        name: 'Delegation',
        status: 'ok',
        message: `${readyProfiles.length} profiles ready`,
        details: readyProfiles.join(', '),
    };
}
exports.checkDelegation = checkDelegation;
//# sourceMappingURL=profile-checks.js.map