"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransferDepartureCacheTransformation = void 0;
const RouteTypeEnums_1 = require("../../../../helpers/RouteTypeEnums");
const shared_1 = require("../../../shared");
const AbstractTransformation_1 = require("@golemio/core/dist/helpers/transformation/AbstractTransformation");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const HeadsignFormatter_1 = require("../helpers/HeadsignFormatter");
let TransferDepartureCacheTransformation = class TransferDepartureCacheTransformation extends AbstractTransformation_1.AbstractTransformation {
    constructor() {
        super(...arguments);
        this.name = "TransferDepartureCacheTransformation";
        this.transformArray = (data) => {
            const departures = new Map();
            const routeDirectionCount = new Map();
            const directionIdCache = new Map();
            const res = [];
            for (const { departure, position, stopTime } of data) {
                if (departure.route_type === RouteTypeEnums_1.GTFSRouteTypeEnum.METRO) {
                    const departureRouteIdentifier = `${departure.route_short_name}-${departure.stop_id}`;
                    const dep = departures.get(departureRouteIdentifier);
                    if (dep) {
                        if (dep.departure_timestamp.minutes.length >= 2) {
                            continue;
                        }
                        dep.departure_timestamp.minutes.push(this.calculateDepartureMinutes(departure.predictedDepartureDate));
                        departures.set(departureRouteIdentifier, dep);
                    }
                    else {
                        departures.set(departureRouteIdentifier, this.transformInternal({ departure, position, stopTime }));
                    }
                }
                else {
                    const routeDirectionKey = this.getRouteDirectionKey(departure, position, directionIdCache);
                    const currentCount = routeDirectionCount.get(routeDirectionKey) ?? 0;
                    if (currentCount >= 2) {
                        continue;
                    }
                    res.push(this.transformInternal({ departure, position, stopTime }));
                    routeDirectionCount.set(routeDirectionKey, currentCount + 1);
                }
            }
            return [...Array.from(departures.values()), ...res];
        };
        this.transformInternal = (data) => {
            const { departure } = data;
            return {
                departure_timestamp: {
                    minutes: [this.calculateDepartureMinutes(departure.predictedDepartureDate)],
                },
                route: {
                    short_name: departure.route_short_name,
                    type: departure.route_type,
                },
                stop: {
                    platform_code: shared_1.PlatformCodeResolver.fromStopTimeCisShort(data.departure, data.stopTime),
                },
                trip: {
                    headsign: HeadsignFormatter_1.HeadsignFormatter.format(departure.trip_headsign),
                    id: departure.trip_id,
                    is_wheelchair_accessible: departure.is_wheelchair_accessible ?? null,
                },
                icons: [],
                substitution_text: null,
            };
        };
    }
    calculateDepartureMinutes(departureTime) {
        const departure_minutes = shared_1.DepartureCalculator.getDepartureMinutes(departureTime);
        if (departure_minutes < 1 && departure_minutes >= 0) {
            return "<1";
        }
        return departure_minutes.toString();
    }
    getRouteDirectionKey(departure, position, directionIdCache) {
        const cacheKey = `${departure.route_short_name}_${departure.trip_headsign}`;
        let directionId = position?.detailed_info?.direction_id;
        if (directionId !== undefined && directionId !== null) {
            directionIdCache.set(cacheKey, directionId);
        }
        else {
            directionId = directionIdCache.get(cacheKey) ?? 0;
        }
        return `${departure.route_short_name}-${directionId}`;
    }
};
exports.TransferDepartureCacheTransformation = TransferDepartureCacheTransformation;
exports.TransferDepartureCacheTransformation = TransferDepartureCacheTransformation = __decorate([
    (0, tsyringe_1.injectable)()
], TransferDepartureCacheTransformation);
//# sourceMappingURL=TransferDepartureCacheTransformation.js.map