"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const AccessibilityEnums_1 = require("../../../helpers/AccessibilityEnums");
const RouteTypeEnums_1 = require("../../../helpers/RouteTypeEnums");
const shared_1 = require("../../shared");
const output_gateway_1 = require("@golemio/core/dist/output-gateway");
const DepartureMode_1 = require("./enums/DepartureMode");
class DepartureBoardMapper {
    static mapDepartures(data, preferredTimezone, mode) {
        const result = data.departures.map((d) => this.transformResponseData(d, preferredTimezone, mode));
        return result;
    }
    static resolveAccessibility(data) {
        return _a.determineWheelchairAccessibility(data);
    }
    static calculateDepartureMinutes(predicted) {
        if (!predicted)
            return null;
        if (!(predicted instanceof Date)) {
            output_gateway_1.log.warn(`Unknown type of predicted date: ${predicted}`);
            return null;
        }
        const departure_minutes = shared_1.DepartureCalculator.getDepartureMinutes(predicted);
        if (departure_minutes < 1) {
            return "<1";
        }
        return departure_minutes.toString();
    }
    static determineWheelchairAccessibility(departure) {
        const { wheelchair_accessible, wheelchair_boarding, real_wheelchair_accessible } = departure;
        if (departure["route_type"] === RouteTypeEnums_1.GTFSRouteTypeEnum.METRO) {
            switch (wheelchair_boarding) {
                case AccessibilityEnums_1.GtfsStopWheelchairBoardingEnum.AccessibleStation:
                    return true;
                case AccessibilityEnums_1.GtfsStopWheelchairBoardingEnum.InaccessibleStation:
                case AccessibilityEnums_1.GtfsStopWheelchairBoardingEnum.NoInformation:
                default:
                    return false;
            }
        }
        else {
            return typeof real_wheelchair_accessible === "boolean"
                ? real_wheelchair_accessible
                : wheelchair_accessible === AccessibilityEnums_1.GtfsTripWheelchairAccessEnum.AccessibleVehicle;
        }
    }
}
_a = DepartureBoardMapper;
/**
 * Initiates all routes. Should respond with correct data to a HTTP requests to all routes.
 */
DepartureBoardMapper.transformResponseData = (x, preferredTimezone, mode) => {
    let isAtStop = x.is_delay_available ? x.stop_sequence === x["trip.last_position.this_stop_sequence"] : false;
    const arrivalTimestamp = {
        // with added delay
        predicted: shared_1.RopidRouterUtils.formatTimestamp(x.arrival_datetime_real, preferredTimezone),
        // according to trip plan
        scheduled: shared_1.RopidRouterUtils.formatTimestamp(x.arrival_datetime, preferredTimezone),
    };
    const departureTimestamp = {
        // with added delay
        predicted: shared_1.RopidRouterUtils.formatTimestamp(x.departure_datetime_real, preferredTimezone),
        // according to trip plan
        scheduled: shared_1.RopidRouterUtils.formatTimestamp(x.departure_datetime, preferredTimezone),
        // minutes till departure. if less than 30 sec return "<1"
        minutes: _a.calculateDepartureMinutes(x.departure_datetime_real),
    };
    // default value
    mode = mode || DepartureMode_1.DepartureMode.DEPARTURES;
    // set null timestamps for first stop of the trip for departures and mixed mode
    if ((mode === DepartureMode_1.DepartureMode.DEPARTURES || mode === DepartureMode_1.DepartureMode.MIXED) && x.stop_sequence === x.min_stop_sequence) {
        arrivalTimestamp.predicted = null;
        arrivalTimestamp.scheduled = null;
    }
    // set null timestamps for last stop of the trip for arrivals and mixed mode
    if ((mode === DepartureMode_1.DepartureMode.ARRIVALS || mode === DepartureMode_1.DepartureMode.MIXED) && x.stop_sequence === x.max_stop_sequence) {
        departureTimestamp.predicted = null;
        departureTimestamp.scheduled = null;
    }
    return {
        arrival_timestamp: arrivalTimestamp,
        delay: {
            is_available: x.is_delay_available,
            minutes: x.delay_minutes, // TBD - now it could be negative!
            seconds: x.delay_seconds, // TBD - now it could be negative!
        },
        departure_timestamp: departureTimestamp,
        last_stop: {
            id: x["trip.last_position.last_stop_id"],
            name: x["trip.last_position.last_stop_name"],
        },
        route: {
            short_name: x.route_short_name,
            type: x.route_type,
            is_night: x.is_night === "1",
            is_regional: x.is_regional === "1",
            is_substitute_transport: x.is_substitute_transport === "1",
        },
        stop: {
            id: x.stop_id,
            platform_code: shared_1.PlatformCodeResolver.resolveDirect(x, x["trip.cis_stop_platform_code"]),
        },
        trip: {
            direction: x.direction,
            headsign: x.stop_headsign ?? x.trip_headsign,
            id: x.trip_id,
            is_at_stop: isAtStop,
            is_canceled: x.is_canceled || false,
            is_wheelchair_accessible: _a.determineWheelchairAccessibility(x),
            is_air_conditioned: x["trip.vehicle_descriptor.is_air_conditioned"] ?? null,
            short_name: x.trip_short_name || null,
        },
    };
};
exports.default = DepartureBoardMapper;
//# sourceMappingURL=DepartureBoardMapper.js.map