"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GTFSTripMetroRunManager = void 0;
const moment_timezone_1 = __importDefault(require("@golemio/core/dist/shared/moment-timezone"));
const AbstractGTFSTripRunManager_1 = require("./AbstractGTFSTripRunManager");
class GTFSTripMetroRunManager extends AbstractGTFSTripRunManager_1.AbstractGTFSTripRunManager {
    constructor() {
        super(...arguments);
        this.CURRENT_TRIP_START_TIME_WINDOW_IN_MS = 4 * 60 * 60 * 1000; // 4 hours
    }
    async generateDelayMsg(schedule, input) {
        const timestampScheduled = (0, moment_timezone_1.default)(input.timestampScheduled);
        const currentTrip = this.determineCurrentTripFromTripId(schedule, input.tripId);
        let delayMsg = {
            updatedTrips: [],
            positions: [],
            schedule,
        };
        if (currentTrip?.trip_id) {
            delayMsg = await this.generateDelayMsgFromCurrentTrip(delayMsg, currentTrip, input);
        }
        else {
            this.logger.verbose(`generateDelayMsg: could not find current gtfsTrip for metro run: ${JSON.stringify(input)}`);
            return delayMsg;
        }
        // last/next gtfs trip for run
        return this.updateLastTripsAndPositions(schedule, timestampScheduled, currentTrip?.trip_id, input, delayMsg);
    }
    getAdjacentTripTimeWindow() {
        return {
            startTimeWindowInMinutes: this.config.getValue("old.datasources.pid.vehicle-positions.adjacentTrips.metro.startTimeWindowInMinutes", 15),
            endTimeWindowInMinutes: this.config.getValue("old.datasources.pid.vehicle-positions.adjacentTrips.metro.endTimeWindowInMinutes", 2),
        };
    }
    async generateDelayMsgFromCurrentTrip(delayMsg, gtfsCurrentTrip, input) {
        const isCurrentTripTracking = true;
        const { trip, position } = await this.updateTrip(input, gtfsCurrentTrip, isCurrentTripTracking);
        delayMsg.updatedTrips.push(trip);
        if (this.testMode)
            delayMsg.positions.push(position);
        return delayMsg;
    }
    async updateLastTripsAndPositions(schedule, timestampScheduled, currentTripId, input, delayMsg) {
        const gtfsAdjacentTrips = this.getAdjacentTrips(schedule, timestampScheduled, currentTripId);
        for (const gtfsTrip of gtfsAdjacentTrips) {
            const { trip, position } = await this.updateTrip(input, gtfsTrip, false);
            delayMsg.updatedTrips.push(trip);
            if (this.testMode) {
                delayMsg.positions.push(position);
            }
        }
        return delayMsg;
    }
    async updateTrip(input, gtfsTrip, isTripTracking) {
        const trip = await this.tripsRepository.upsertMetroRunTrip(input, gtfsTrip);
        const position = await this.positionsRepository.upsertMetroRunPosition(input, gtfsTrip, isTripTracking);
        return { trip, position };
    }
    determineCurrentTripFromTripId(schedule, gtfsTripId) {
        let currentTrip = undefined;
        if (!gtfsTripId) {
            return currentTrip;
        }
        for (const tripCache of schedule) {
            if (tripCache.trip_id !== gtfsTripId) {
                continue;
            }
            const currTime = new Date().getTime();
            if (new Date(tripCache.start_timestamp).getTime() - this.CURRENT_TRIP_START_TIME_WINDOW_IN_MS < currTime &&
                new Date(tripCache.end_timestamp).getTime() + this.CURRENT_TRIP_START_TIME_WINDOW_IN_MS > currTime) {
                currentTrip = tripCache;
                break;
            }
        }
        return currentTrip;
    }
}
exports.GTFSTripMetroRunManager = GTFSTripMetroRunManager;
//# sourceMappingURL=GTFSTripMetroRunManager.js.map