"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetroRunsMessageProcessingTransformation = void 0;
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
const transformations_1 = require("@golemio/core/dist/integration-engine/transformations");
const MetroRouteHelper_1 = require("../helpers/MetroRouteHelper");
class MetroRunsMessageProcessingTransformation extends transformations_1.BaseTransformation {
    constructor() {
        super(...arguments);
        this.name = "MetroRunMessageProcessing";
        this.transform = async ({ message, gpsData }) => {
            const runSchedule = this.parseRunScheduleFromMessage(message);
            if (!runSchedule) {
                return;
            }
            const runInput = this.parseRunInputFromMessage(message, runSchedule, gpsData);
            if (runInput) {
                return {
                    runSchedule,
                    runInput,
                };
            }
        };
        this.transformElement = async () => { };
    }
    parseRunScheduleFromMessage(message) {
        const routeId = MetroRouteHelper_1.MetroRouteHelper.getRouteId(message.route_name);
        if (!routeId) {
            helpers_1.log.warn(`parseRunScheduleFromMessage: invalid route name in the message: ${JSON.stringify(message)}`);
            return;
        }
        let runNumber;
        if (Number.parseInt(message.train_set_number_real) > 0) {
            runNumber = message.train_set_number_real;
        }
        else if (Number.parseInt(message.train_set_number_scheduled) > 0) {
            runNumber = message.train_set_number_scheduled;
        }
        else {
            helpers_1.log.warn(`parseRunScheduleFromMessage: run number cannot be determined from the message: ${JSON.stringify(message)}`);
            return;
        }
        return {
            route_id: routeId,
            run_number: Number.parseInt(runNumber),
            msg_last_timestamp: message.message_timestamp,
        };
    }
    parseRunInputFromMessage(message, runSchedule, gpsData) {
        const coordinates = gpsData?.coordinates?.[message.track_id];
        if (!coordinates) {
            helpers_1.log.verbose(`parseRunInputFromMessage: no GPS coordinates for the message: ${JSON.stringify(message)}`);
            return;
        }
        return {
            messageTimestamp: message.message_timestamp,
            timestampScheduled: message.actual_position_timestamp_scheduled,
            routeId: runSchedule.route_id,
            runNumber: runSchedule.run_number,
            trainSetNumberScheduled: message.train_set_number_scheduled,
            trainSetNumberReal: message.train_set_number_real,
            trainNumber: message.train_number,
            coordinates,
        };
    }
}
exports.MetroRunsMessageProcessingTransformation = MetroRunsMessageProcessingTransformation;
//# sourceMappingURL=MetroRunsMessageProcessingTransformation.js.map