"use strict";
/**
 * Symlink Health Checks
 *
 * Check CCS symlinks and settings symlinks.
 */
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.checkSettingsSymlinks = exports.checkCcsSymlinks = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const instance_directory_1 = require("../../management/instance-directory");
/**
 * Check CCS symlinks health
 */
function checkCcsSymlinks() {
    try {
        const { ClaudeSymlinkManager } = require('../../utils/claude-symlink-manager');
        const manager = new ClaudeSymlinkManager();
        const health = manager.checkHealth();
        if (health.healthy) {
            const itemCount = manager.ccsItems.length;
            return {
                id: 'ccs-symlinks',
                name: 'CCS Symlinks',
                status: 'ok',
                message: `${itemCount}/${itemCount} items linked`,
            };
        }
        return {
            id: 'ccs-symlinks',
            name: 'CCS Symlinks',
            status: 'warning',
            message: `${health.issues.length} issues found`,
            fix: 'Run: ccs sync',
        };
    }
    catch (e) {
        return {
            id: 'ccs-symlinks',
            name: 'CCS Symlinks',
            status: 'warning',
            message: 'Could not check',
            details: e.message,
            fix: 'Run: ccs sync',
        };
    }
}
exports.checkCcsSymlinks = checkCcsSymlinks;
/**
 * Check settings symlinks
 */
function checkSettingsSymlinks(ccsDir, claudeDir) {
    try {
        const sharedDir = `${ccsDir}/shared`;
        const sharedSettings = `${sharedDir}/settings.json`;
        const claudeSettings = `${claudeDir}/settings.json`;
        if (!fs.existsSync(sharedSettings)) {
            return {
                id: 'settings-symlinks',
                name: 'settings.json',
                status: 'warning',
                message: 'Shared not found',
                fix: 'Run: ccs sync',
            };
        }
        const sharedStats = fs.lstatSync(sharedSettings);
        if (!sharedStats.isSymbolicLink()) {
            return {
                id: 'settings-symlinks',
                name: 'settings.json',
                status: 'warning',
                message: 'Not a symlink',
                fix: 'Run: ccs sync',
            };
        }
        const sharedTarget = fs.readlinkSync(sharedSettings);
        const resolvedShared = path.resolve(path.dirname(sharedSettings), sharedTarget);
        if (resolvedShared !== claudeSettings) {
            return {
                id: 'settings-symlinks',
                name: 'settings.json',
                status: 'warning',
                message: 'Wrong target',
                fix: 'Run: ccs sync',
            };
        }
        // Check instances
        const instancesDir = `${ccsDir}/instances`;
        if (!fs.existsSync(instancesDir)) {
            return {
                id: 'settings-symlinks',
                name: 'settings.json',
                status: 'ok',
                message: 'Shared symlink valid',
            };
        }
        const instances = (0, instance_directory_1.listAccountInstanceNames)(instancesDir);
        let broken = 0;
        for (const instance of instances) {
            const instanceSettings = `${instancesDir}/${instance}/settings.json`;
            if (!fs.existsSync(instanceSettings)) {
                broken++;
                continue;
            }
            try {
                const stats = fs.lstatSync(instanceSettings);
                if (!stats.isSymbolicLink()) {
                    broken++;
                    continue;
                }
                const target = fs.readlinkSync(instanceSettings);
                const resolved = path.resolve(path.dirname(instanceSettings), target);
                if (resolved !== sharedSettings) {
                    broken++;
                }
            }
            catch {
                broken++;
            }
        }
        if (broken > 0) {
            return {
                id: 'settings-symlinks',
                name: 'settings.json',
                status: 'warning',
                message: `${broken} broken instance(s)`,
                fix: 'Run: ccs sync',
            };
        }
        return {
            id: 'settings-symlinks',
            name: 'settings.json',
            status: 'ok',
            message: `${instances.length} instance(s) valid`,
        };
    }
    catch (e) {
        return {
            id: 'settings-symlinks',
            name: 'settings.json',
            status: 'warning',
            message: 'Check failed',
            details: e.message,
            fix: 'Run: ccs sync',
        };
    }
}
exports.checkSettingsSymlinks = checkSettingsSymlinks;
//# sourceMappingURL=symlink-checks.js.map