"use strict";
// Runtime registry — tracks which optional jarvis adapters have been wired up.
//
// Voice STT/TTS/wake-word and inbound channel pollers all live behind null
// adapters by default. This registry lets the status endpoint and UI report
// "Voice ready" vs "Install Whisper recipe" without hardcoding which
// modules look at the registry directly. Persists to
// `.harness/jarvis/runtime.json` so registrations survive server restart.
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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.markRuntimeInstalled = markRuntimeInstalled;
exports.clearRuntimeRegistry = clearRuntimeRegistry;
exports.isFeatureReady = isFeatureReady;
exports.getRuntimeRegistryStatus = getRuntimeRegistryStatus;
exports.saveRuntimeRegistry = saveRuntimeRegistry;
exports.loadRuntimeRegistry = loadRuntimeRegistry;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const atomicFile_1 = require("../persistence/atomicFile");
const installed = new Map();
function markRuntimeInstalled(feature, adapterName) {
    installed.set(feature, { adapterName, installedAt: new Date().toISOString() });
}
function clearRuntimeRegistry() {
    installed.clear();
}
function isFeatureReady(feature) {
    return installed.has(feature);
}
function getRuntimeRegistryStatus() {
    return {
        voice: {
            stt: isFeatureReady('voice_stt'),
            tts: isFeatureReady('voice_tts'),
            wake: isFeatureReady('voice_wake'),
            sttAdapter: installed.get('voice_stt')?.adapterName,
            ttsAdapter: installed.get('voice_tts')?.adapterName,
            wakeAdapter: installed.get('voice_wake')?.adapterName,
        },
        inbound: {
            slack: isFeatureReady('inbound_slack'),
            telegram: isFeatureReady('inbound_telegram'),
            email: isFeatureReady('inbound_email'),
        },
    };
}
function registryPath(projectDir) {
    return path.join(projectDir, '.harness', 'jarvis', 'runtime.json');
}
async function saveRuntimeRegistry(projectDir) {
    const filePath = registryPath(projectDir);
    await (0, atomicFile_1.withFileLock)(filePath, async () => {
        // Snapshot inside the lock so a concurrent markRuntimeInstalled call
        // can't shift entries between the snapshot read and the write.
        const snapshot = {};
        for (const [k, v] of installed.entries())
            snapshot[k] = v;
        await (0, atomicFile_1.atomicWriteFile)(filePath, JSON.stringify(snapshot, null, 2));
    });
}
async function loadRuntimeRegistry(projectDir) {
    try {
        const raw = await fs.readFile(registryPath(projectDir), 'utf-8');
        const data = JSON.parse(raw);
        installed.clear();
        for (const [k, v] of Object.entries(data)) {
            installed.set(k, v);
        }
    }
    catch {
        // No file yet — fine.
    }
}
//# sourceMappingURL=runtimeRegistry.js.map