"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PublicCacheTripsFilter = void 0;
const VehicleIdGenerator_1 = require("../../../gtfs-rt/helpers/VehicleIdGenerator");
const const_1 = require("../../../../../../const");
class PublicCacheTripsFilter {
    static getDistinctLatestTrips(trips) {
        const filteredTrips = [];
        const canceledTrips = [];
        const futureTrips = [];
        const tripsByVehicleId = new Map();
        const futureTripsByTripAndVehicle = new Map();
        const canceledTripsByTripId = new Map();
        for (let i = 0; i < trips.length; i++) {
            const trip = trips[i];
            if (trip.state_position === const_1.StatePositionEnum.CANCELED) {
                canceledTripsByTripId.set(trip.gtfs_trip_id, trip);
                continue;
            }
            const vehicleId = VehicleIdGenerator_1.VehicleIdGenerator.getVehicleId(trip.gtfs_trip_id, trip.gtfs_route_type, trip.provider_source_type, trip.gtfs_route_short_name, trip.cis_trip_number, trip.vehicle_registration_number, trip.run_number, trip.internal_run_number);
            const knownTrip = tripsByVehicleId.get(vehicleId);
            if (knownTrip) {
                if (trip.created_at < knownTrip.created_at) {
                    tripsByVehicleId.set(vehicleId, trip);
                }
                else if (trip.state_position === const_1.StatePositionEnum.BEFORE_TRACK ||
                    trip.state_position === const_1.StatePositionEnum.BEFORE_TRACK_DELAYED) {
                    futureTripsByTripAndVehicle.set(trip.gtfs_trip_id + vehicleId, trip);
                }
            }
            else {
                tripsByVehicleId.set(vehicleId, trip);
            }
        }
        for (const entity of tripsByVehicleId.values())
            filteredTrips.push(entity);
        for (const entity of futureTripsByTripAndVehicle.values())
            futureTrips.push(entity);
        for (const entity of canceledTripsByTripId.values())
            canceledTrips.push(entity);
        return { filteredTrips, futureTrips, canceledTrips };
    }
}
exports.PublicCacheTripsFilter = PublicCacheTripsFilter;
//# sourceMappingURL=PublicCacheTripsFilter.js.map