"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Status = exports.DEFAULT_STATUS_ID = exports.STATUS_IDS = void 0;
exports.clear = clearStatus;
exports.get = getStatus;
exports.set = setStatus;
const databases_ts_1 = require("../../resources/databases.js");
const hdbError_js_1 = require("../../utility/errors/hdbError.js");
const logger_ts_1 = require("../../utility/logging/logger.js");
const statusValidator_ts_1 = require("../../validation/statusValidator.js");
const definitions_ts_1 = require("./definitions.js");
const index_ts_1 = require("../../components/status/index.js");
const requestRestart_ts_1 = require("../../components/requestRestart.js");
var definitions_ts_2 = require("./definitions.js");
Object.defineProperty(exports, "STATUS_IDS", { enumerable: true, get: function () { return definitions_ts_2.STATUS_IDS; } });
Object.defineProperty(exports, "DEFAULT_STATUS_ID", { enumerable: true, get: function () { return definitions_ts_2.DEFAULT_STATUS_ID; } });
const { HTTP_STATUS_CODES } = hdbError_js_1.hdbErrors;
// Lazy-initialize the Status table to avoid initialization issues during module import
let _statusTable;
function getStatusTable() {
    if (!_statusTable) {
        _statusTable = (0, databases_ts_1.table)({
            database: 'system',
            table: 'hdb_status',
            replicate: false,
            attributes: [
                {
                    name: 'id',
                    isPrimaryKey: true,
                },
                {
                    name: 'status',
                },
                {
                    name: '__createdtime__',
                },
                {
                    name: '__updatedtime__',
                },
            ],
        });
    }
    return _statusTable;
}
// Export Status as a getter for compatibility with modules that need direct table access
exports.Status = {
    get primaryStore() {
        return getStatusTable().primaryStore;
    },
};
const statusLogger = (0, logger_ts_1.loggerWithTag)('status');
function clearStatus({ id }) {
    statusLogger.debug?.('clearStatus', id);
    return getStatusTable().delete(id);
}
async function getAllStatus() {
    statusLogger.debug?.('getAllStatus');
    const statusRecords = getStatusTable().search([]);
    // Get aggregated component statuses from all threads
    const aggregatedStatuses = await index_ts_1.internal.query.allThreads();
    const componentStatusArray = Array.from(aggregatedStatuses.entries()).map(([name, status]) => ({
        name,
        ...status,
    }));
    // Get restart flag status
    const restartRequired = (0, requestRestart_ts_1.restartNeeded)();
    return {
        systemStatus: statusRecords,
        componentStatus: componentStatusArray,
        restartRequired,
    };
}
function getStatus({ id }) {
    if (!id) {
        statusLogger.debug?.('getStatus', 'all');
        return getAllStatus();
    }
    statusLogger.debug?.('getStatus', id);
    return getStatusTable().get(id);
}
function setStatus({ status, id = definitions_ts_1.DEFAULT_STATUS_ID, }) {
    const validation = (0, statusValidator_ts_1.validateStatus)({ status, id });
    if (validation) {
        throw (0, hdbError_js_1.handleHDBError)(validation, validation.message, HTTP_STATUS_CODES.BAD_REQUEST);
    }
    statusLogger.debug?.('setStatus', id, status);
    return getStatusTable().put(id, { status });
}
//# sourceMappingURL=index.js.map