"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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommonRunsMessagesTransformation = void 0;
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
const transformations_1 = require("@golemio/core/dist/integration-engine/transformations");
const CommonRunHelper_1 = require("../helpers/CommonRunHelper");
const moment_timezone_1 = __importDefault(require("@golemio/core/dist/shared/moment-timezone"));
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
let CommonRunsMessagesTransformation = class CommonRunsMessagesTransformation extends transformations_1.BaseTransformation {
    constructor() {
        super();
        this.name = "CommonRunsMessagesTransformation";
        /**
         * Overrides BaseTransformation::transform
         */
        this.transform = async ({ data, timestamp }, isTram = false) => {
            const now = (0, moment_timezone_1.default)(timestamp).tz("Europe/Prague").format();
            const res = [];
            if (!Array.isArray(data)) {
                data = [data];
            }
            for (const element of data) {
                try {
                    const elementTransformed = await this.transformElement({ data: element, timestamp: now }, isTram);
                    res.push(elementTransformed);
                }
                catch (err) {
                    helpers_1.log.verbose(`${this.name}: invalid run parameters: ${JSON.stringify(element).substring(0, 2000)}`);
                }
            }
            return res;
        };
        this.transformElement = async (element, isTram = false) => {
            const attributes = element.data.$;
            if (!isTram && !attributes.tjr) {
                return Promise.reject();
            }
            const identifiers = this.commonRunHelper.getRunIdentifiers(attributes, element.timestamp);
            const realStopTimestampDate = this.commonRunHelper.parseDateFromRunInput(attributes.takt);
            const messageTimestampDate = this.commonRunHelper.parseDateFromRunInput(attributes.tm);
            if (!identifiers || !realStopTimestampDate || !messageTimestampDate) {
                return Promise.reject();
            }
            const scheduledStopTimestampDate = attributes.tjr ? this.commonRunHelper.parseDateFromRunInput(attributes.tjr) : null;
            const t = {
                run: {
                    id: identifiers.runId,
                    route_id: identifiers.routeId,
                    run_number: Number.parseInt(identifiers.runNumber),
                    line_short_name: attributes.line,
                    registration_number: attributes.evc,
                    msg_start_timestamp: element.timestamp,
                    msg_last_timestamp: element.timestamp,
                    wheelchair_accessible: attributes.np === "ano",
                },
                run_message: {
                    // id - autoincrement
                    // runs_id - associated fk
                    lat: +attributes.lat,
                    lng: +attributes.lng,
                    actual_stop_asw_id: attributes.akt,
                    actual_stop_timestamp_real: realStopTimestampDate,
                    actual_stop_timestamp_scheduled: scheduledStopTimestampDate,
                    last_stop_asw_id: attributes.konc,
                    packet_number: attributes.pkt,
                    msg_timestamp: messageTimestampDate,
                    events: attributes.events,
                },
            };
            return t;
        };
        this.commonRunHelper = new CommonRunHelper_1.CommonRunHelper();
    }
};
exports.CommonRunsMessagesTransformation = CommonRunsMessagesTransformation;
exports.CommonRunsMessagesTransformation = CommonRunsMessagesTransformation = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], CommonRunsMessagesTransformation);
//# sourceMappingURL=CommonRunsMessagesTransformation.js.map