"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommonMessageProcessor = void 0;
const Di_1 = require("../../../../ioc/Di");
const vehicle_positions_1 = require("../../../../../schema-definitions/vehicle-positions");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const config_1 = require("@golemio/core/dist/integration-engine/config");
const queueprocessors_1 = require("@golemio/core/dist/integration-engine/queueprocessors");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const const_1 = require("../../../../../const");
const ONE_HOUR_IN_MILLIS = 60 * 60 * 1000;
const MIN_HOURS_DIFF_TO_LOG = 22;
class CommonMessageProcessor {
    constructor(runsRepository) {
        this.runsRepository = runsRepository;
        /**
         * Create/update and process transformed run
         */
        this.processTransformedRun = async (element, firstMessageCreatedAt) => {
            this.logElementIssues(element);
            const record = await this.runsRepository.getRunRecordForUpdate(element.run);
            let outputMsg;
            if (record) {
                if (!element.run_message.actual_stop_timestamp_scheduled &&
                    !const_1.ArrayNotPublicRegistrationNumbers.includes(element.run.registration_number)) {
                    const lastRecordMessage = await this.runsRepository["runsMessagesRepository"].getLastMessage(record.id);
                    if (!lastRecordMessage || !lastRecordMessage.actual_stop_timestamp_scheduled)
                        return;
                    element.run_message.actual_stop_timestamp_scheduled = lastRecordMessage.actual_stop_timestamp_scheduled;
                }
                outputMsg = await this.runsRepository.updateAndAssociate(element, record.id);
            }
            else {
                outputMsg = await this.runsRepository.createAndAssociate(element);
            }
            await queueprocessors_1.QueueManager.sendMessageToExchange(`${config_1.config.RABBIT_EXCHANGE_NAME}.${vehicle_positions_1.VehiclePositions.name.toLowerCase()}`, "updateRunsGTFSTripId", outputMsg, { timestamp: firstMessageCreatedAt });
        };
        this.logger = Di_1.PidContainer.resolve(CoreToken_1.CoreToken.Logger);
    }
    logElementIssues(element) {
        if (element.run_message.actual_stop_timestamp_scheduled instanceof Date &&
            element.run_message.actual_stop_timestamp_scheduled.getTime() - Date.now() >=
                MIN_HOURS_DIFF_TO_LOG * ONE_HOUR_IN_MILLIS) {
            this.logger.error(new golemio_errors_1.GeneralError(`Message timestamp 'tjr' of value '${element.run_message.actual_stop_timestamp_scheduled.toISOString()}'` +
                ` is ${MIN_HOURS_DIFF_TO_LOG}+ hrs ahead (line ${element.run.line_short_name}, run` +
                ` ${element.run.run_number})`, this.constructor.name, undefined, undefined, "pid"));
        }
    }
}
exports.CommonMessageProcessor = CommonMessageProcessor;
//# sourceMappingURL=CommonMessageProcessor.js.map