"use strict";
/**
 * System Health Checks
 *
 * Checks for Claude CLI, CCS directory, and permissions.
 */
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.checkPermissions = exports.checkCcsDirectory = exports.checkClaudeCli = void 0;
const fs = __importStar(require("fs"));
const child_process_1 = require("child_process");
const claude_detector_1 = require("../../utils/claude-detector");
/**
 * Check Claude CLI installation and version
 */
async function checkClaudeCli() {
    const cliInfo = (0, claude_detector_1.getClaudeCliInfo)();
    if (!cliInfo) {
        return {
            id: 'claude-cli',
            name: 'Claude CLI',
            status: 'error',
            message: 'Not found in PATH',
            fix: 'Install: npm install -g @anthropic-ai/claude-code',
        };
    }
    try {
        const version = (0, child_process_1.execSync)('claude --version', {
            encoding: 'utf8',
            timeout: 5000,
            stdio: ['pipe', 'pipe', 'pipe'],
        }).trim();
        const versionMatch = version.match(/(\d+\.\d+\.\d+)/);
        const versionStr = versionMatch ? versionMatch[1] : 'unknown';
        return {
            id: 'claude-cli',
            name: 'Claude CLI',
            status: 'ok',
            message: `v${versionStr}`,
            details: cliInfo.path,
        };
    }
    catch {
        return {
            id: 'claude-cli',
            name: 'Claude CLI',
            status: 'error',
            message: 'Not working',
            details: cliInfo.path,
            fix: 'Reinstall Claude CLI',
        };
    }
}
exports.checkClaudeCli = checkClaudeCli;
/**
 * Check CCS directory existence
 */
function checkCcsDirectory(ccsDir) {
    if (fs.existsSync(ccsDir)) {
        return {
            id: 'ccs-dir',
            name: 'CCS Directory',
            status: 'ok',
            message: 'Exists',
            details: '~/.ccs/',
        };
    }
    return {
        id: 'ccs-dir',
        name: 'CCS Directory',
        status: 'error',
        message: 'Not found',
        details: ccsDir,
        fix: 'Run: npm install -g @kaitranntt/ccs --force',
        fixable: true,
    };
}
exports.checkCcsDirectory = checkCcsDirectory;
/**
 * Check permissions on CCS directory
 */
function checkPermissions(ccsDir) {
    const testFile = `${ccsDir}/.permission-test`;
    try {
        fs.writeFileSync(testFile, 'test', 'utf8');
        fs.unlinkSync(testFile);
        return {
            id: 'permissions',
            name: 'Permissions',
            status: 'ok',
            message: 'Write access verified',
        };
    }
    catch {
        return {
            id: 'permissions',
            name: 'Permissions',
            status: 'error',
            message: 'Cannot write to ~/.ccs/',
            fix: 'sudo chown -R $USER ~/.ccs ~/.claude && chmod 755 ~/.ccs ~/.claude',
        };
    }
}
exports.checkPermissions = checkPermissions;
//# sourceMappingURL=system-checks.js.map