"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDomindsRuntimeStatus = getDomindsRuntimeStatus;
exports.createDomindsRuntimeStatusMessage = createDomindsRuntimeStatusMessage;
const time_1 = require("@longrun-ai/kernel/utils/time");
const node_crypto_1 = require("node:crypto");
const node_fs_1 = require("node:fs");
const node_path_1 = __importDefault(require("node:path"));
const log_1 = require("../log");
const dominds_running_version_1 = require("./dominds-running-version");
const dominds_self_update_1 = require("./dominds-self-update");
const log = (0, log_1.createLogger)('dominds-runtime-status');
function resolveDomindsInstallRoot() {
    return node_path_1.default.resolve(__dirname, '..', '..');
}
async function readSpaHash() {
    const indexPath = node_path_1.default.join(resolveDomindsInstallRoot(), 'webapp', 'dist', 'index.html');
    try {
        const indexHtml = await node_fs_1.promises.readFile(indexPath, 'utf8');
        return (0, node_crypto_1.createHash)('sha256').update(indexHtml).digest('hex');
    }
    catch (error) {
        log.warn('Failed to read SPA hash from dist index.html', error, { indexPath });
        return null;
    }
}
async function getDomindsRuntimeStatus(mode) {
    return {
        workspace: process.cwd(),
        version: dominds_running_version_1.DOMINDS_RUNNING_VERSION,
        spaHash: await readSpaHash(),
        mode,
        selfUpdate: await (0, dominds_self_update_1.getDomindsSelfUpdateStatus)(),
    };
}
async function createDomindsRuntimeStatusMessage(mode) {
    return {
        type: 'dominds_runtime_status',
        runtimeStatus: await getDomindsRuntimeStatus(mode),
        timestamp: (0, time_1.formatUnifiedTimestamp)(new Date()),
    };
}
