"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const DateTimeUtils_1 = require("../../../../../../helpers/DateTimeUtils");
const DPPUtils_1 = __importDefault(require("../../helpers/DPPUtils"));
const ProviderSourceTypeEnum_1 = require("../../../../../../schema-definitions/vehicle-positions/models/enums/ProviderSourceTypeEnum");
const RouteTypeEnums_1 = require("../../../../../../helpers/RouteTypeEnums");
const TripsIdGenerator_1 = __importDefault(require("./TripsIdGenerator"));
class TripsMapper {
    static mapCommonRunToDto(gtfsTrip, runInput) {
        return {
            ...this.getGTFSAttributes(gtfsTrip),
            id: TripsIdGenerator_1.default.generateFromCommonRun(gtfsTrip, runInput),
            origin_route_name: runInput.route_id,
            internal_route_name: runInput.route_id,
            run_number: runInput.run_number,
            internal_run_number: runInput.run_number,
            vehicle_registration_number: parseInt(runInput.registration_number),
            vehicle_type_id: this.getVehicleTypeId(+gtfsTrip.route_type, !!+gtfsTrip.is_night, !!+gtfsTrip.is_regional, !!+gtfsTrip.is_substitute_transport),
            wheelchair_accessible: runInput.wheelchair_accessible,
            provider_source_type: ProviderSourceTypeEnum_1.ProviderSourceTypeEnum.TcpCommon,
        };
    }
    static mapCommonRunNotPublicToDto(runInput, tripId, lastPositionId) {
        return {
            agency_name_real: DPPUtils_1.default.DPP_AGENCY_NAME,
            agency_name_scheduled: DPPUtils_1.default.DPP_AGENCY_NAME,
            gtfs_route_type: RouteTypeEnums_1.GTFSRouteTypeEnum.TRAM,
            origin_route_name: runInput.route_id,
            internal_route_name: runInput.route_id,
            run_number: runInput.run_number,
            internal_run_number: runInput.run_number,
            vehicle_registration_number: parseInt(runInput.registration_number),
            vehicle_type_id: RouteTypeEnums_1.MPVRouteTypesEnum.TRAM,
            wheelchair_accessible: runInput.wheelchair_accessible,
            provider_source_type: ProviderSourceTypeEnum_1.ProviderSourceTypeEnum.TcpCommon,
            id: tripId,
            last_position_id: lastPositionId,
        };
    }
    static mapMetroRunToDto(gtfsTrip, runInput) {
        return {
            ...this.getGTFSAttributes(gtfsTrip),
            id: TripsIdGenerator_1.default.generateFromMetroRun(gtfsTrip, runInput),
            origin_route_name: runInput.routeId,
            internal_route_name: runInput.routeId,
            run_number: runInput.runNumber,
            internal_run_number: runInput.internalRunNumber ?? runInput.runNumber,
            vehicle_type_id: RouteTypeEnums_1.MPVRouteTypesEnum.METRO,
            wheelchair_accessible: true, // metro vehicles themselves are always wheelchair accessible
            provider_source_type: ProviderSourceTypeEnum_1.ProviderSourceTypeEnum.TcpMetro,
        };
    }
    static mapRegionalBusRunToDto(gtfsTrip, runInput) {
        return {
            ...this.getGTFSAttributes(gtfsTrip),
            id: TripsIdGenerator_1.default.generateFromRegionalBusRun(gtfsTrip, runInput.runMessage),
            agency_name_real: runInput.agencyName,
            agency_name_scheduled: runInput.agencyName,
            cis_line_id: runInput.runMessage.cis_line_id,
            cis_trip_number: runInput.runMessage.cis_trip_number,
            origin_route_name: gtfsTrip.origin_route_name,
            internal_route_name: gtfsTrip.origin_route_name,
            run_number: gtfsTrip.run_number,
            vehicle_registration_number: runInput.runMessage.registration_number,
            vehicle_type_id: this.getVehicleTypeId(+gtfsTrip.route_type, !!+gtfsTrip.is_night, !!+gtfsTrip.is_regional, !!+gtfsTrip.is_substitute_transport),
            wheelchair_accessible: runInput.isWheelchairAccessible,
            provider_source_type: ProviderSourceTypeEnum_1.ProviderSourceTypeEnum.TcpRegionalBus,
        };
    }
    static mapTripEntitiesToUpdateDto(tripEntities, taskStartTimestamp) {
        const output = {
            inserted: [],
            updated: [],
        };
        for (const tripEntity of tripEntities) {
            const tripCreatedTimestamp = new Date(tripEntity.get("created_at")).getTime();
            if (tripCreatedTimestamp > taskStartTimestamp) {
                output.inserted.push({
                    cis_line_short_name: tripEntity.cis_line_short_name,
                    id: tripEntity.id,
                    start_asw_stop_id: tripEntity.start_asw_stop_id,
                    start_cis_stop_id: tripEntity.start_cis_stop_id,
                    start_cis_stop_platform_code: tripEntity.start_cis_stop_platform_code,
                    start_timestamp: tripEntity.start_timestamp?.toISOString() ?? null,
                    agency_name_real: tripEntity.agency_name_real,
                    agency_name_scheduled: tripEntity.agency_name_scheduled,
                    cis_line_id: tripEntity.cis_line_id,
                    cis_trip_number: tripEntity.cis_trip_number,
                    origin_route_name: tripEntity.origin_route_name,
                    run_number: tripEntity.run_number,
                    start_time: tripEntity.start_time,
                    vehicle_registration_number: tripEntity.vehicle_registration_number,
                    vehicle_type_id: tripEntity.vehicle_type_id,
                    wheelchair_accessible: tripEntity.wheelchair_accessible,
                });
            }
            else {
                output.updated.push({
                    id: tripEntity.id,
                    origin_route_name: tripEntity.origin_route_name,
                    run_number: tripEntity.run_number,
                    start_timestamp: tripEntity.start_timestamp,
                });
            }
        }
        return output;
    }
    static getGTFSAttributes(gtfsTrip) {
        return {
            agency_name_real: DPPUtils_1.default.DPP_AGENCY_NAME,
            agency_name_scheduled: DPPUtils_1.default.DPP_AGENCY_NAME,
            gtfs_block_id: gtfsTrip.block_id,
            gtfs_route_id: gtfsTrip.route_id,
            gtfs_route_short_name: gtfsTrip.route_short_name,
            gtfs_route_type: gtfsTrip.route_type,
            gtfs_trip_headsign: gtfsTrip.trip_headsign,
            gtfs_trip_id: gtfsTrip.trip_id,
            gtfs_date: gtfsTrip.date,
            gtfs_direction_id: gtfsTrip.direction_id,
            gtfs_shape_id: gtfsTrip.shape_id,
            start_time: DateTimeUtils_1.DateTimeUtils.parseUTCTimeFromISO(gtfsTrip.start_timestamp),
            start_timestamp: new Date(gtfsTrip.start_timestamp),
            end_timestamp: new Date(gtfsTrip.end_timestamp),
        };
    }
}
TripsMapper.getVehicleTypeId = (routeType, isNight, isRegional, isSubstitute) => {
    let id;
    switch (routeType) {
        case RouteTypeEnums_1.GTFSRouteTypeEnum.TRAM:
            if (isNight) {
                id = RouteTypeEnums_1.MPVRouteTypesEnum.TRAM_NIGHT;
            }
            else if (isSubstitute) {
                id = RouteTypeEnums_1.MPVRouteTypesEnum.TRAM_SUBSTITUTE;
            }
            else {
                id = RouteTypeEnums_1.MPVRouteTypesEnum.TRAM;
            }
            break;
        case RouteTypeEnums_1.GTFSRouteTypeEnum.METRO:
            id = RouteTypeEnums_1.MPVRouteTypesEnum.METRO;
            break;
        case RouteTypeEnums_1.GTFSRouteTypeEnum.TRAIN:
            id = RouteTypeEnums_1.MPVRouteTypesEnum.TRAIN;
            break;
        case RouteTypeEnums_1.GTFSRouteTypeEnum.BUS:
            if (isNight && isRegional) {
                id = RouteTypeEnums_1.MPVRouteTypesEnum.BUS_NIGHT_REGIONAL;
            }
            else if (isNight) {
                id = RouteTypeEnums_1.MPVRouteTypesEnum.BUS_NIGHT;
            }
            else if (isRegional) {
                id = RouteTypeEnums_1.MPVRouteTypesEnum.BUS_REGIONAL;
            }
            else if (isSubstitute) {
                id = RouteTypeEnums_1.MPVRouteTypesEnum.BUS_SUBSTITUTE;
            }
            else {
                id = RouteTypeEnums_1.MPVRouteTypesEnum.BUS_CITY;
            }
            break;
        case RouteTypeEnums_1.GTFSRouteTypeEnum.FERRY:
            id = RouteTypeEnums_1.MPVRouteTypesEnum.FERRY;
            break;
        case RouteTypeEnums_1.GTFSRouteTypeEnum.FUNICULAR:
            id = RouteTypeEnums_1.MPVRouteTypesEnum.FUNICULAR;
            break;
        case RouteTypeEnums_1.GTFSRouteTypeEnum.TROLLEYBUS:
            id = RouteTypeEnums_1.MPVRouteTypesEnum.TROLLEYBUS;
            break;
        default:
            throw new Error("Unexpected routeType");
    }
    return id;
};
exports.default = TripsMapper;
//# sourceMappingURL=TripsMapper.js.map