"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaveBusRunsToDBTask = void 0;
const workers_1 = require("@golemio/core/dist/integration-engine/workers");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const CommonRunsRepository_1 = require("../data-access/CommonRunsRepository");
const CommonMessageProcessor_1 = require("../helpers/CommonMessageProcessor");
const CommonRunsSchema_1 = require("../schema/CommonRunsSchema");
const Di_1 = require("../../../ioc/Di");
const VPContainerToken_1 = require("../../../ioc/VPContainerToken");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const constants_1 = require("../constants");
let SaveBusRunsToDBTask = class SaveBusRunsToDBTask extends workers_1.AbstractTask {
    constructor() {
        super(constants_1.WORKER_NAME);
        this.queueName = "saveBusRunsToDB";
        this.queueTtl = 2 * 60 * 1000; // 2 minutes
        this.schema = CommonRunsSchema_1.CommonRunsValidationSchema;
        this.messagesTransformation = Di_1.VPContainer.resolve(VPContainerToken_1.VPContainerToken.CommonRunsMessagesTransformation);
        this.runsRepository = new CommonRunsRepository_1.CommonRunsRepository();
        this.messageProcessor = new CommonMessageProcessor_1.CommonMessageProcessor(this.runsRepository);
        this.messageFilter = Di_1.VPContainer.resolve(VPContainerToken_1.VPContainerToken.BusMessageFilter);
    }
    async execute(data, msgProperties) {
        const timestamp = msgProperties?.timestamp;
        if (!timestamp) {
            throw new golemio_errors_1.GeneralError(`Missing bus run message timestamp: ${JSON.stringify(data)}`, this.constructor.name);
        }
        const messages = await this.messagesTransformation.transform({ data: data.M.V, timestamp });
        const filteredMessages = this.messageFilter.yieldFilteredMessages(messages, timestamp);
        for (const runMessage of filteredMessages) {
            await this.messageProcessor.processTransformedRun(runMessage, timestamp);
        }
    }
};
exports.SaveBusRunsToDBTask = SaveBusRunsToDBTask;
exports.SaveBusRunsToDBTask = SaveBusRunsToDBTask = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], SaveBusRunsToDBTask);
//# sourceMappingURL=SaveBusRunsToDBTask.js.map