"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.repairImageAnalysisRuntimeState = exports.syncManagedImageAnalysisInstances = exports.cleanupManagedImageAnalysisHooks = exports.countManagedImageAnalysisHookFiles = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const instance_manager_1 = __importDefault(require("../../management/instance-manager"));
const config_manager_1 = require("../config-manager");
const hooks_1 = require("../hooks");
const image_analyzer_hook_utils_1 = require("../hooks/image-analyzer-hook-utils");
const mcp_installer_1 = require("./mcp-installer");
function visitManagedImageAnalysisSettings(callback, baseDir = (0, config_manager_1.getCcsDir)()) {
    if (!fs.existsSync(baseDir)) {
        return;
    }
    for (const entry of fs.readdirSync(baseDir)) {
        if (!entry.endsWith('.settings.json')) {
            continue;
        }
        const settingsPath = path.join(baseDir, entry);
        try {
            const stat = fs.statSync(settingsPath);
            if (!stat.isFile()) {
                continue;
            }
            const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
            callback(settings, settingsPath);
        }
        catch {
            // Best-effort cleanup; preserve malformed files for manual recovery.
        }
    }
}
function countManagedImageAnalysisHookFiles(baseDir = (0, config_manager_1.getCcsDir)()) {
    let count = 0;
    visitManagedImageAnalysisSettings((settings) => {
        if ((0, image_analyzer_hook_utils_1.removeCcsImageAnalyzerHooks)(JSON.parse(JSON.stringify(settings)))) {
            count += 1;
        }
    }, baseDir);
    return count;
}
exports.countManagedImageAnalysisHookFiles = countManagedImageAnalysisHookFiles;
function cleanupManagedImageAnalysisHooks(baseDir = (0, config_manager_1.getCcsDir)()) {
    let cleaned = 0;
    visitManagedImageAnalysisSettings((settings, settingsPath) => {
        if (!(0, image_analyzer_hook_utils_1.removeCcsImageAnalyzerHooks)(settings)) {
            return;
        }
        fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf8');
        cleaned += 1;
    }, baseDir);
    return cleaned;
}
exports.cleanupManagedImageAnalysisHooks = cleanupManagedImageAnalysisHooks;
function syncManagedImageAnalysisInstances(instanceManager = new instance_manager_1.default()) {
    let synced = 0;
    for (const instanceName of instanceManager.listInstances()) {
        const instancePath = instanceManager.getInstancePath(instanceName);
        if (instanceManager.syncMcpServers(instancePath)) {
            synced += 1;
        }
    }
    return synced;
}
exports.syncManagedImageAnalysisInstances = syncManagedImageAnalysisInstances;
function repairImageAnalysisRuntimeState() {
    const managedToolReady = (0, mcp_installer_1.ensureImageAnalysisMcpOrThrow)();
    const sharedHookReady = (0, hooks_1.prepareImageAnalysisFallbackHook)();
    const cleanedSettingsFiles = cleanupManagedImageAnalysisHooks();
    const syncedInstances = managedToolReady ? syncManagedImageAnalysisInstances() : 0;
    return {
        cleanedSettingsFiles,
        syncedInstances,
        managedToolReady,
        sharedHookReady,
    };
}
exports.repairImageAnalysisRuntimeState = repairImageAnalysisRuntimeState;
//# sourceMappingURL=repair.js.map