"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.MetroRunsMessageProcessingTransformation = void 0;
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
const AbstractTransformation_1 = require("@golemio/core/dist/helpers/transformation/AbstractTransformation");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const MetroRouteHelper_1 = require("../helpers/MetroRouteHelper");
let MetroRunsMessageProcessingTransformation = class MetroRunsMessageProcessingTransformation extends AbstractTransformation_1.AbstractTransformation {
    constructor() {
        super(...arguments);
        this.name = "MetroRunMessageProcessing";
        this.transformInternal = ({ message, gpsData }) => {
            const runSchedule = this.parseRunScheduleFromMessage(message);
            if (!runSchedule) {
                return;
            }
            const runInput = this.parseRunInputFromMessage(message, runSchedule, gpsData);
            if (runInput) {
                return {
                    runSchedule,
                    runInput,
                };
            }
        };
    }
    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;
exports.MetroRunsMessageProcessingTransformation = MetroRunsMessageProcessingTransformation = __decorate([
    (0, tsyringe_1.injectable)()
], MetroRunsMessageProcessingTransformation);
//# sourceMappingURL=MetroRunsMessageProcessingTransformation.js.map