"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.PresetLogTransformation = void 0;
const transformations_1 = require("@golemio/core/dist/integration-engine/transformations");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
let PresetLogTransformation = class PresetLogTransformation extends transformations_1.BaseTransformation {
    constructor() {
        super(...arguments);
        this.name = "PresetLogTransformation";
        this.transform = (data) => {
            const dto = [];
            for (const dataItem of data) {
                const dtoItem = this.transformElement(dataItem);
                if (dtoItem) {
                    dto.push(dtoItem);
                }
            }
            return Promise.resolve(dto);
        };
        this.transformElement = (dataItem) => {
            // Use dummy hostname to parse relative URLs
            // we want to normalize the relative URL without query params, leading slashes and other irregularities
            const url = new URL(dataItem.stream.req_url, "http://golemio");
            const logTimestampInNanoseconds = BigInt(dataItem.values[0][0]);
            const receivedAtDateObject = new Date(Number(logTimestampInNanoseconds / BigInt(1000000)));
            return {
                received_at: receivedAtDateObject,
                device_alias: url.pathname.split("/pid/").at(-1).replace("/", ""),
                is_processed: false,
                request_url: dataItem.stream.req_url,
                request_method: dataItem.stream.req_method,
                request_user_agent: dataItem.stream.req_userAgent ?? "",
                response_status: Number.parseInt(dataItem.stream.res_status),
                response_time_ms: Number.parseInt(dataItem.stream.responseTime),
            };
        };
    }
};
exports.PresetLogTransformation = PresetLogTransformation;
exports.PresetLogTransformation = PresetLogTransformation = __decorate([
    (0, tsyringe_1.injectable)()
], PresetLogTransformation);
//# sourceMappingURL=PresetLogTransformation.js.map