"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 const_1 = require("../../../../../../const");
const TripsIdGenerator_1 = __importDefault(require("./TripsIdGenerator"));
class PositionsMapper {
    static mapCommonRunToDto(runMessage, isCurrent, gtfsTrip, runInput) {
        const scheduledTimestamp = runMessage.actual_stop_timestamp_scheduled
            ? new Date(runMessage.actual_stop_timestamp_scheduled)
            : null;
        const isTerminated = runMessage.events === const_1.TCPEventEnum.TERMINATED ||
            (scheduledTimestamp ? scheduledTimestamp >= new Date(gtfsTrip.end_timestamp) : false);
        return {
            asw_last_stop_id: runMessage.last_stop_asw_id,
            lat: runMessage.lat,
            lng: runMessage.lng,
            origin_time: DateTimeUtils_1.DateTimeUtils.parseUTCTimeFromISO(runMessage.actual_stop_timestamp_real),
            origin_timestamp: new Date(runMessage.actual_stop_timestamp_real),
            scheduled_timestamp: scheduledTimestamp,
            state_position: const_1.StatePositionEnum.UNKNOWN,
            state_process: const_1.StateProcessEnum.TCP_INPUT,
            is_tracked: isCurrent && !isTerminated,
            trips_id: TripsIdGenerator_1.default.generateFromCommonRun(gtfsTrip, runInput),
            tcp_event: runMessage.events,
        };
    }
    static mapCommonRunNotPublicToDto(runMessage, tripId) {
        return {
            asw_last_stop_id: runMessage.last_stop_asw_id,
            lat: runMessage.lat,
            lng: runMessage.lng,
            origin_time: DateTimeUtils_1.DateTimeUtils.parseUTCTimeFromISO(runMessage.actual_stop_timestamp_real),
            origin_timestamp: new Date(runMessage.actual_stop_timestamp_real),
            state_position: const_1.StatePositionEnum.NOT_PUBLIC,
            state_process: const_1.StateProcessEnum.PROCESSED,
            is_tracked: true,
            trips_id: tripId,
            tcp_event: runMessage.events,
        };
    }
    static mapMetroRunToDto(isCurrent, gtfsTrip, runInput) {
        const scheduledTimestamp = new Date(runInput.timestampScheduled);
        const isTerminated = scheduledTimestamp >= new Date(gtfsTrip.end_timestamp);
        const beforeTrack = scheduledTimestamp < new Date(gtfsTrip.start_timestamp);
        const is_tracked = beforeTrack ? false : isCurrent && !isTerminated;
        return {
            lat: runInput.coordinates.lat,
            lng: runInput.coordinates.lon,
            origin_time: DateTimeUtils_1.DateTimeUtils.parseUTCTimeFromISO(runInput.messageTimestamp),
            origin_timestamp: new Date(runInput.messageTimestamp),
            scheduled_timestamp: scheduledTimestamp,
            state_position: const_1.StatePositionEnum.UNKNOWN,
            state_process: const_1.StateProcessEnum.TCP_INPUT,
            this_stop_id: runInput.coordinates.gtfs_stop_id,
            is_tracked,
            trips_id: TripsIdGenerator_1.default.generateFromMetroRun(gtfsTrip, runInput),
        };
    }
    static mapRegionalBusRunToDto(isCurrent, gtfsTrip, runMessage) {
        const isBeforeTrack = new Date() < new Date(gtfsTrip.start_timestamp);
        const isTracked = isCurrent && !isBeforeTrack && !runMessage.is_terminated;
        return {
            trips_id: TripsIdGenerator_1.default.generateFromRegionalBusRun(gtfsTrip, runMessage),
            bearing: runMessage.bearing,
            lat: runMessage.coordinates.coordinates[1],
            lng: runMessage.coordinates.coordinates[0],
            origin_time: DateTimeUtils_1.DateTimeUtils.parseUTCTimeFromISO(runMessage.vehicle_timestamp),
            origin_timestamp: new Date(runMessage.vehicle_timestamp),
            state_position: const_1.StatePositionEnum.UNKNOWN,
            state_process: const_1.StateProcessEnum.TCP_INPUT,
            speed: runMessage.speed_kmh,
            is_tracked: isTracked,
            tcp_event: runMessage.events,
        };
    }
}
exports.default = PositionsMapper;
//# sourceMappingURL=PositionsMapper.js.map