"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TripStopsManager = void 0;
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
const VPUtils_1 = require("../helpers/VPUtils");
class TripStopsManager {
    constructor(gtfsStopTimesRepository, blockStopsRedisRepository) {
        this.gtfsStopTimesRepository = gtfsStopTimesRepository;
        this.blockStopsRedisRepository = blockStopsRedisRepository;
        /**
         * Get trip stops schedule by block id
         *
         * @param {string} blockId - Target gtfs block id
         * @param {string} tripIds - List of inserted gtfs trip ids
         */
        this.setAndReturnTripStopsSchedule = async (blockId, tripIds) => {
            const schedule = await this.blockStopsRedisRepository.get(blockId);
            if (schedule) {
                return schedule;
            }
            try {
                const tripStops = await this.gtfsStopTimesRepository.findTripStops(tripIds);
                const tripStopsSchedule = tripStops.reduce((acc, curr) => {
                    acc[curr.trip_id] = { trip_stops: curr.trip_stops };
                    return acc;
                }, {});
                await this.blockStopsRedisRepository.set(blockId, tripStopsSchedule, undefined, VPUtils_1.VPUtils.getNextExpireTimestamp());
                return tripStopsSchedule;
            }
            catch (err) {
                helpers_1.log.warn("Unable to retrieve trip stops", err);
                return {};
            }
        };
    }
}
exports.TripStopsManager = TripStopsManager;
//# sourceMappingURL=TripStopsManager.js.map