"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GtfsTripRegionalBusRunManager = void 0;
const cache_1 = require("../../../../../../ropid-gtfs/data-access/cache");
const CommonRunsRepository_1 = require("../../../../runs/data-access/CommonRunsRepository");
const moment_timezone_1 = __importDefault(require("@golemio/core/dist/shared/moment-timezone"));
const PositionsMapper_1 = __importDefault(require("../../../data-access/helpers/PositionsMapper"));
const TripsMapper_1 = __importDefault(require("../../../data-access/helpers/TripsMapper"));
const PositionsRepository_1 = require("../../../data-access/PositionsRepository");
const TripsRepository_1 = require("../../../data-access/TripsRepository");
const AbstractGTFSTripRunManager_1 = require("../../gtfs-trip-run/AbstractGTFSTripRunManager");
class GtfsTripRegionalBusRunManager extends AbstractGTFSTripRunManager_1.AbstractGTFSTripRunManager {
    constructor(isTestMode = false) {
        const positionRepository = new PositionsRepository_1.PositionsRepository();
        const tripRepository = new TripsRepository_1.TripsRepository();
        const runRepository = new CommonRunsRepository_1.CommonRunsRepository();
        const runTripsRedisRepository = new cache_1.RunTripsRedisRepository();
        super(positionRepository, tripRepository, runRepository, runTripsRedisRepository, isTestMode);
    }
    async generateDelayMsg(schedule, input) {
        const timestampScheduled = (0, moment_timezone_1.default)(input.runMessage.vehicle_timestamp);
        const currentTrip = this.determineCurrentTripFromTripId(schedule, input.gtfsTripId);
        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)}`);
        }
        // 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.regionalBus.startTimeWindowInMinutes", 60),
            endTimeWindowInMinutes: this.config.getValue("old.datasources.pid.vehicle-positions.adjacentTrips.regionalBus.endTimeWindowInMinutes", 15),
        };
    }
    async generateDelayMsgFromCurrentTrip(delayMsg, gtfsCurrentTrip, input) {
        const isCurrentTripTracking = true;
        const { trip } = await this.updateTripAndPosition(input, gtfsCurrentTrip, isCurrentTripTracking);
        delayMsg.updatedTrips.push(trip);
        return delayMsg;
    }
    async updateLastTripsAndPositions(schedule, timestampScheduled, currentTripId, input, delayMsg) {
        const gtfsAdjacentTrips = this.getAdjacentTrips(schedule, timestampScheduled, currentTripId);
        for (const gtfsTrip of gtfsAdjacentTrips) {
            const { trip } = await this.updateTripAndPosition(input, gtfsTrip, false);
            delayMsg.updatedTrips.push(trip);
        }
        return delayMsg;
    }
    async updateTripAndPosition(input, gtfsTrip, isTripTracking) {
        //#region position
        const positionDto = PositionsMapper_1.default.mapRegionalBusRunToDto(isTripTracking, gtfsTrip, input.runMessage);
        await this.positionsRepository.createEntity(positionDto);
        //#endregion
        //#region trip
        const tripDto = TripsMapper_1.default.mapRegionalBusRunToDto(gtfsTrip, input);
        const trip = await this.tripsRepository.upsertEntity(tripDto);
        //#endregion
        return { trip };
    }
}
exports.GtfsTripRegionalBusRunManager = GtfsTripRegionalBusRunManager;
//# sourceMappingURL=GtfsTripRegionalBusRunManager.js.map