'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 () {
    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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readTransactionLog = readTransactionLog;
exports.deleteTransactionLogsBefore = deleteTransactionLogsBefore;
const hdbUtils = __importStar(require("../common_utils.js"));
const harper_logger_ts_1 = __importDefault(require("./harper_logger.js"));
const hdbError_ts_1 = require("../errors/hdbError.js");
const commonErrors_ts_1 = require("../errors/commonErrors.js");
const transactionLogValidator_ts_1 = require("../../validation/transactionLogValidator.js");
const harperBridge = require('../../dataLayer/harperBridge/harperBridge').default;
async function readTransactionLog(req) {
    const validation = (0, transactionLogValidator_ts_1.readTransactionLogValidator)(req);
    if (validation) {
        throw (0, hdbError_ts_1.handleHDBError)(validation, validation.message, commonErrors_ts_1.HTTP_STATUS_CODES.BAD_REQUEST, undefined, undefined, true);
    }
    req.database = req.database ?? req.schema ?? 'data';
    const invalidSchemaTableMsg = hdbUtils.checkSchemaTableExist(req.database, req.table);
    if (invalidSchemaTableMsg) {
        throw (0, hdbError_ts_1.handleHDBError)(new Error(), invalidSchemaTableMsg, commonErrors_ts_1.HTTP_STATUS_CODES.NOT_FOUND, undefined, undefined, true);
    }
    harper_logger_ts_1.default.info('Reading Harper logs used by Plexus');
    if (req.from || req.to) {
        req.search_type = 'timestamp';
        req.search_values = [req.from ?? 0];
        if (req.to)
            req.search_values[1] = req.to;
    }
    return harperBridge.readAuditLog(req);
}
/**
 * Deletes messages from a tables local stream (persistence layer),
 * where all transactions against that table are stored.
 * @param req - {schema, table, timestamp}
 * @returns {Promise<string>}
 */
async function deleteTransactionLogsBefore(req) {
    const validation = (0, transactionLogValidator_ts_1.deleteTransactionLogsBeforeValidator)(req);
    if (validation.error) {
        const err = new Error(validation.error.message);
        throw (0, hdbError_ts_1.handleHDBError)(err, err.message, commonErrors_ts_1.HTTP_STATUS_CODES.BAD_REQUEST, undefined, undefined, true);
    }
    const { value } = validation;
    value.database = value.database ?? value.schema ?? 'data';
    harper_logger_ts_1.default.info('Delete transaction logs called for Plexus');
    return harperBridge.deleteTransactionLogsBefore(value);
}
//# sourceMappingURL=transactionLog.js.map