"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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CisLookupManager = void 0;
const VPContainerToken_1 = require("../../../../ioc/VPContainerToken");
const VPUtils_1 = require("../../../vehicle-positions/helpers/VPUtils");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const RegionalBusCisCacheRepository_1 = require("../../data-access/cache/RegionalBusCisCacheRepository");
let CisLookupManager = class CisLookupManager {
    constructor(cisLookupCacheRepository) {
        this.cisLookupCacheRepository = cisLookupCacheRepository;
    }
    async enrichMessagesWithCisData(messages) {
        const nextExpireTimestamp = VPUtils_1.VPUtils.getNextExpireTimestamp();
        const cisCacheData = await this.cisLookupCacheRepository.mget(messages.map((message) => message.external_trip_id));
        for (let i = 0; i < messages.length; i++) {
            const message = messages[i];
            const cisCacheItem = cisCacheData[i];
            if (message.cis_line_id && message.cis_trip_number && message.registration_number) {
                await this.cisLookupCacheRepository.set(message.external_trip_id, {
                    cis_line_id: message.cis_line_id,
                    cis_trip_number: message.cis_trip_number,
                    registration_number: message.registration_number,
                }, undefined, nextExpireTimestamp);
                continue;
            }
            if (!cisCacheItem) {
                continue;
            }
            // Fill in the missing data from Redis cache
            message.cis_line_id = cisCacheItem.cis_line_id;
            message.cis_trip_number = cisCacheItem.cis_trip_number;
            message.registration_number = cisCacheItem.registration_number;
        }
        return messages;
    }
};
exports.CisLookupManager = CisLookupManager;
exports.CisLookupManager = CisLookupManager = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(VPContainerToken_1.VPContainerToken.RegionalBusCisCacheRepository)),
    __metadata("design:paramtypes", [RegionalBusCisCacheRepository_1.RegionalBusCisCacheRepository])
], CisLookupManager);
//# sourceMappingURL=CisLookupManager.js.map