"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GTFSTripCommonRunManager = void 0;
const moment_timezone_1 = __importDefault(require("@golemio/core/dist/shared/moment-timezone"));
const const_1 = require("../../../../../../const");
const RouteTypeEnums_1 = require("../../../../../../helpers/RouteTypeEnums");
const AbstractGTFSTripRunManager_1 = require("./AbstractGTFSTripRunManager");
class GTFSTripCommonRunManager extends AbstractGTFSTripRunManager_1.AbstractGTFSTripRunManager {
    async generateDelayMsg(schedule, input) {
        const timestampScheduled = (0, moment_timezone_1.default)(input.run_message.actual_stop_timestamp_scheduled);
        const currentTrip = this.determineCurrentTripFromTimestamp(schedule, timestampScheduled);
        let delayMsg = {
            updatedTrips: [],
            positions: [],
            schedule,
        };
        if (currentTrip?.trip_id) {
            delayMsg = await this.generateDelayMsgFromCurrentTrip(delayMsg, currentTrip, timestampScheduled, input);
        }
        else {
            this.logger.verbose(`generateDelayMsg: could not find current gtfsTrip for common run: ${JSON.stringify(input)}`);
        }
        // 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.common.startTimeWindowInMinutes", 60),
            endTimeWindowInMinutes: this.config.getValue("old.datasources.pid.vehicle-positions.adjacentTrips.common.endTimeWindowInMinutes", 15),
        };
    }
    async generateDelayMsgFromCurrentTrip(delayMsg, gtfsCurrentTrip, timestampScheduled, input) {
        const event = input.run_message.events;
        const isCurrentTripTracking = this.isCurrentTripTracking(gtfsCurrentTrip, event, timestampScheduled);
        const { trip, position } = await this.updateTrip(input, gtfsCurrentTrip, isCurrentTripTracking);
        delayMsg.updatedTrips.push(trip);
        if (this.testMode)
            delayMsg.positions.push(position);
        return delayMsg;
    }
    isCurrentTripTracking(gtfsCurrentTrip, event, timestampScheduled) {
        return (
        // tram is always on track while pushes messages only on stops
        (gtfsCurrentTrip.route_type === RouteTypeEnums_1.GTFSRouteTypeEnum.TRAM ||
            // when bus announces DEPARTURE TCP event, it means that trip is on track
            ((gtfsCurrentTrip.route_type === RouteTypeEnums_1.GTFSRouteTypeEnum.BUS ||
                gtfsCurrentTrip.route_type === RouteTypeEnums_1.GTFSRouteTypeEnum.TROLLEYBUS) &&
                event === const_1.TCPEventEnum.DEPARTURED) ||
            // of course when trip announcing some next stop time in schedule, then set as tracking
            timestampScheduled.isSameOrAfter((0, moment_timezone_1.default)(gtfsCurrentTrip.start_timestamp))) ??
            false);
    }
    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.upsertCommonRunTrip(input, gtfsTrip);
        const position = await this.positionsRepository.upsertCommonRunPosition(input, gtfsTrip, isTripTracking);
        return { trip, position };
    }
}
exports.GTFSTripCommonRunManager = GTFSTripCommonRunManager;
//# sourceMappingURL=GTFSTripCommonRunManager.js.map