"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); }
};
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 VPContainerToken_1 = require("../../../../../ioc/VPContainerToken");
const CommonRunsRepository_1 = require("../../../../runs/data-access/CommonRunsRepository");
const moment_timezone_1 = __importDefault(require("@golemio/core/dist/shared/moment-timezone"));
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
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");
let GtfsTripRegionalBusRunManager = class GtfsTripRegionalBusRunManager extends AbstractGTFSTripRunManager_1.AbstractGTFSTripRunManager {
    constructor(positionsRepository, tripsRepository, runsRepository, runTripsRedisRepository) {
        super(positionsRepository, tripsRepository, runsRepository, runTripsRedisRepository);
    }
    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;
exports.GtfsTripRegionalBusRunManager = GtfsTripRegionalBusRunManager = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(VPContainerToken_1.VPContainerToken.PositionsRepository)),
    __param(1, (0, tsyringe_1.inject)(VPContainerToken_1.VPContainerToken.TripRepository)),
    __param(2, (0, tsyringe_1.inject)(VPContainerToken_1.VPContainerToken.CommonRunsRepository)),
    __param(3, (0, tsyringe_1.inject)(VPContainerToken_1.VPContainerToken.RunTripsRedisRepository)),
    __metadata("design:paramtypes", [PositionsRepository_1.PositionsRepository,
        TripsRepository_1.TripsRepository,
        CommonRunsRepository_1.CommonRunsRepository,
        cache_1.RunTripsRedisRepository])
], GtfsTripRegionalBusRunManager);
//# sourceMappingURL=GtfsTripRegionalBusRunManager.js.map