"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateRunsGtfsTripIdTask = void 0;
const cache_1 = require("../../../../ropid-gtfs/data-access/cache");
const CommonRunsRepository_1 = require("../../runs/data-access/CommonRunsRepository");
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
const queueprocessors_1 = require("@golemio/core/dist/integration-engine/queueprocessors");
const workers_1 = require("@golemio/core/dist/integration-engine/workers");
const const_1 = require("../../../../../const");
const PositionsRepository_1 = require("../data-access/PositionsRepository");
const TripsRepository_1 = require("../data-access/TripsRepository");
const PositionsMapper_1 = __importDefault(require("../data-access/helpers/PositionsMapper"));
const TripsIdGenerator_1 = __importDefault(require("../data-access/helpers/TripsIdGenerator"));
const TripsMapper_1 = __importDefault(require("../data-access/helpers/TripsMapper"));
const GTFSTripRunManagerFactory_1 = require("../helpers/gtfs-trip-run/GTFSTripRunManagerFactory");
const UpdateRunsGtfsTripIdSchema_1 = require("../schema/UpdateRunsGtfsTripIdSchema");
class UpdateRunsGtfsTripIdTask extends workers_1.AbstractTask {
    constructor(queuePrefix) {
        super(queuePrefix);
        this.queueName = "updateRunsGTFSTripId";
        this.queueTtl = 5 * 60 * 1000; // 5 minutes
        this.schema = UpdateRunsGtfsTripIdSchema_1.UpdateRunsGtfsTripIdValidationSchema;
        /**
         * Process not public trips from runs data
         */
        this.updateNotPublicRunTrip = async (input) => {
            const tripId = TripsIdGenerator_1.default.generateFromCommonRunNotPublic(input.run);
            //#region position
            const positionDto = PositionsMapper_1.default.mapCommonRunNotPublicToDto(input.run_message, tripId);
            const createdPosition = await this.positionsRepository.createEntity(positionDto);
            //#endregion
            //#region trip
            const tripDto = TripsMapper_1.default.mapCommonRunNotPublicToDto(input.run, tripId, createdPosition.id);
            await this.tripsRepository.upsertEntity(tripDto);
            //#endregion
        };
        this.tripsRepository = new TripsRepository_1.TripsRepository();
        this.positionsRepository = new PositionsRepository_1.PositionsRepository();
        this.commonRunsRepository = new CommonRunsRepository_1.CommonRunsRepository();
        this.runTripsRedisRepository = new cache_1.RunTripsRedisRepository();
        this.gtfsTripRunManager = GTFSTripRunManagerFactory_1.GTFSTripRunManagerFactory.create(GTFSTripRunManagerFactory_1.GTFSTripRunType.Common, this.positionsRepository, this.tripsRepository, this.commonRunsRepository, this.runTripsRedisRepository);
    }
    /**
     * Process trips from runs data
     */
    async execute(data, messageProperties) {
        const schedule = await this.gtfsTripRunManager.setAndReturnScheduledTrips(data.run);
        if (schedule.length === 0) {
            if (const_1.ArrayNotPublicRegistrationNumbers.includes(data.run.registration_number)) {
                await this.updateNotPublicRunTrip(data);
            }
            else {
                helpers_1.log.info(`updateRunsGTFSTripId: no schedule for run: ${JSON.stringify(data.run)}`);
            }
            return;
        }
        if (!data.run_message.actual_stop_timestamp_scheduled) {
            helpers_1.log.info(`updateRunsGTFSTripId: actualStopTimestampScheduled is not provided for current run: ${JSON.stringify(data.run)}`);
            return;
        }
        const delayMsg = await this.gtfsTripRunManager.generateDelayMsg(schedule, data);
        // send to updateDelay only if there are some trips updated
        if (delayMsg.updatedTrips.length > 0) {
            await queueprocessors_1.QueueManager.sendMessageToExchange(this.queuePrefix, "updateDelay", delayMsg, {
                timestamp: messageProperties?.timestamp,
            });
        }
    }
}
exports.UpdateRunsGtfsTripIdTask = UpdateRunsGtfsTripIdTask;
//# sourceMappingURL=UpdateRunsGtfsTripIdTask.js.map