"use strict";
/**
 * Configuration Health Checks
 *
 * Check config.json, config.yaml, settings files, and Claude settings.
 * 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.checkClaudeSettings = exports.checkSettingsFiles = exports.checkConfigFile = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const config_manager_1 = require("../../utils/config-manager");
const config_loader_facade_1 = require("../../config/config-loader-facade");
/**
 * Check config file (config.json or config.yaml based on mode)
 */
function checkConfigFile() {
    // In unified mode, check config.yaml
    if ((0, config_loader_facade_1.isUnifiedMode)() || (0, config_loader_facade_1.hasUnifiedConfig)()) {
        const ccsDir = (0, config_loader_facade_1.getCcsDir)();
        const yamlPath = path.join(ccsDir, 'config.yaml');
        if (!fs.existsSync(yamlPath)) {
            return {
                id: 'config-file',
                name: 'config.yaml',
                status: 'warning',
                message: 'Not found (unified mode)',
                details: yamlPath,
                fixable: true,
            };
        }
        try {
            fs.readFileSync(yamlPath, 'utf8');
            return {
                id: 'config-file',
                name: 'config.yaml',
                status: 'ok',
                message: 'Valid (unified mode)',
                details: yamlPath,
            };
        }
        catch {
            return {
                id: 'config-file',
                name: 'config.yaml',
                status: 'error',
                message: 'Cannot read file',
                details: yamlPath,
            };
        }
    }
    // Legacy mode: check config.json
    const configPath = (0, config_manager_1.getConfigPath)();
    if (!fs.existsSync(configPath)) {
        return {
            id: 'config-file',
            name: 'config.json',
            status: 'warning',
            message: 'Not found',
            details: configPath,
            fixable: true,
        };
    }
    try {
        const content = fs.readFileSync(configPath, 'utf8');
        JSON.parse(content);
        return {
            id: 'config-file',
            name: 'config.json',
            status: 'ok',
            message: 'Valid',
            details: configPath,
        };
    }
    catch {
        return {
            id: 'config-file',
            name: 'config.json',
            status: 'error',
            message: 'Invalid JSON',
            details: configPath,
        };
    }
}
exports.checkConfigFile = checkConfigFile;
/**
 * Check settings files (glm, km with legacy kimi fallback)
 */
function checkSettingsFiles(ccsDir) {
    const checks = [];
    const profiles = ['glm', 'km'];
    const { DelegationValidator } = require('../../utils/delegation-validator');
    for (const profile of profiles) {
        const fileName = `${profile}.settings.json`;
        const filePath = path.join(ccsDir, fileName);
        const validation = DelegationValidator.validate(profile);
        if (!validation.valid && validation.error?.includes('Profile not found')) {
            checks.push({
                id: `settings-${profile}`,
                name: fileName,
                status: 'info',
                message: 'Not configured',
                details: filePath,
            });
            continue;
        }
        const resolvedPath = validation.settingsPath || filePath;
        const resolvedName = path.basename(resolvedPath);
        if (validation.valid) {
            checks.push({
                id: `settings-${profile}`,
                name: resolvedName,
                status: 'ok',
                message: 'Key configured',
                details: resolvedPath,
            });
            continue;
        }
        if (validation.error?.includes('placeholder')) {
            checks.push({
                id: `settings-${profile}`,
                name: resolvedName,
                status: 'warning',
                message: 'Placeholder key',
                details: resolvedPath,
            });
            continue;
        }
        if (validation.error?.includes('Failed to parse settings.json')) {
            checks.push({
                id: `settings-${profile}`,
                name: resolvedName,
                status: 'error',
                message: 'Invalid JSON',
                details: resolvedPath,
            });
            continue;
        }
        // Keep prior behavior for non-placeholder validation issues (e.g., missing key).
        checks.push({
            id: `settings-${profile}`,
            name: resolvedName,
            status: 'ok',
            message: 'Valid JSON',
            details: resolvedPath,
        });
    }
    return checks;
}
exports.checkSettingsFiles = checkSettingsFiles;
/**
 * Check Claude settings file
 */
function checkClaudeSettings(claudeDir) {
    const settingsPath = path.join(claudeDir, 'settings.json');
    if (!fs.existsSync(settingsPath)) {
        return {
            id: 'claude-settings',
            name: '~/.claude/settings.json',
            status: 'warning',
            message: 'Not found',
            fix: 'Run: claude /login',
        };
    }
    try {
        const content = fs.readFileSync(settingsPath, 'utf8');
        JSON.parse(content);
        return {
            id: 'claude-settings',
            name: '~/.claude/settings.json',
            status: 'ok',
            message: 'Valid',
        };
    }
    catch {
        return {
            id: 'claude-settings',
            name: '~/.claude/settings.json',
            status: 'warning',
            message: 'Invalid JSON',
            fix: 'Run: claude /login',
        };
    }
}
exports.checkClaudeSettings = checkClaudeSettings;
//# sourceMappingURL=config-checks.js.map