"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetroRunsMessagesTransformation = void 0;
const transformations_1 = require("@golemio/core/dist/integration-engine/transformations");
class MetroRunsMessagesTransformation extends transformations_1.BaseTransformation {
    constructor() {
        super(...arguments);
        this.name = "MetroRunMessages";
        this.transform = ({ data }) => {
            let { $: messageData, vlak: trainData } = data.m;
            if (!trainData) {
                return Promise.resolve([]);
            }
            if (!Array.isArray(trainData)) {
                trainData = [trainData];
            }
            const dto = [];
            for (const trainDataItem of trainData) {
                const dtoItem = this.transformElement({ messageData, trainData: trainDataItem });
                dto.push(dtoItem);
            }
            return Promise.resolve(dto);
        };
        this.transformElement = ({ messageData, trainData }) => {
            const delayOriginSeconds = parseInt(trainData.$.odch);
            const messageTimestamp = new Date(messageData.tm);
            const scheduledTimestamp = new Date(messageTimestamp);
            // delay is in seconds, positive number means the train is ahead of time
            scheduledTimestamp.setSeconds(scheduledTimestamp.getSeconds() + delayOriginSeconds);
            return {
                route_name: messageData.linka,
                message_timestamp: messageTimestamp,
                train_set_number_scheduled: trainData.$.csp.trimStart(),
                train_set_number_real: trainData.$.csr.trimStart(),
                train_number: trainData.$.cv,
                track_id: trainData.$.ko,
                delay_origin: delayOriginSeconds,
                actual_position_timestamp_scheduled: scheduledTimestamp,
            };
        };
    }
}
exports.MetroRunsMessagesTransformation = MetroRunsMessagesTransformation;
//# sourceMappingURL=MetroRunsMessagesTransformation.js.map