"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 AbstractTransformation_1 = require("@golemio/core/dist/helpers/transformation/AbstractTransformation");
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");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
let CommonRunsMessagesTransformation = class CommonRunsMessagesTransformation extends AbstractTransformation_1.AbstractTransformation {
    constructor() {
        super();
        this.name = "CommonRunsMessagesTransformation";
        this.transform = ({ data, timestamp }) => {
            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 = this.transformInternal({ data: element, timestamp: now });
                    res.push(elementTransformed);
                }
                catch (err) {
                    helpers_1.log.verbose(`${this.name}: invalid run parameters: ${JSON.stringify(element).substring(0, 2000)}`);
                }
            }
            return res;
        };
        this.transformInternal = (element) => {
            const attributes = element.data.$;
            const identifiers = this.commonRunHelper.getRunIdentifiers(attributes, element.timestamp);
            const realStopTimestampDate = this.commonRunHelper.parseDateFromRunInput(attributes.tm);
            const messageTimestampDate = this.commonRunHelper.parseDateFromRunInput(attributes.takt);
            if (!identifiers || !realStopTimestampDate || !messageTimestampDate) {
                throw new golemio_errors_1.GeneralError("Missing identifiers or timestamps", this.constructor.name);
            }
            const scheduledStopTimestampDate = attributes.tjr ? this.commonRunHelper.parseDateFromRunInput(attributes.tjr) : null;
            return {
                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,
                },
            };
        };
        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