"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 EventMessage_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventMessage = exports.MessagePayload = exports.ExtendedMessageLocation = void 0;
const jackson_js_1 = require("jackson-js");
const constants_1 = require("./constants");
const message_1 = require("./message");
let ExtendedMessageLocation = class ExtendedMessageLocation {
    constructor(bucket, key) {
        this.Bucket = bucket;
        this.MetadataKey = key;
    }
};
__decorate([
    (0, jackson_js_1.JsonProperty)()
], ExtendedMessageLocation.prototype, "Bucket", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)()
], ExtendedMessageLocation.prototype, "MetadataKey", void 0);
ExtendedMessageLocation = __decorate([
    (0, jackson_js_1.JsonNaming)({ strategy: jackson_js_1.PropertyNamingStrategy.KEBAB_CASE })
], ExtendedMessageLocation);
exports.ExtendedMessageLocation = ExtendedMessageLocation;
let MessagePayload = class MessagePayload {
    constructor(compression, body) {
        this.Body = body;
        this.Compression = compression;
    }
};
__decorate([
    (0, jackson_js_1.JsonProperty)()
], MessagePayload.prototype, "Body", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)()
], MessagePayload.prototype, "Compression", void 0);
MessagePayload = __decorate([
    (0, jackson_js_1.JsonNaming)({ strategy: jackson_js_1.PropertyNamingStrategy.KEBAB_CASE })
], MessagePayload);
exports.MessagePayload = MessagePayload;
let EventMessage = EventMessage_1 = class EventMessage extends message_1.Message {
    static construct(tmn, trn, ServiceTrn, config) {
        var _a;
        if (!((_a = config.Verb) === null || _a === void 0 ? void 0 : _a.length)) {
            throw ("event message without verb");
        }
        const evnt = new EventMessage_1(tmn, trn, ServiceTrn, config);
        return evnt;
    }
    constructor(tmn, trn, ServiceTrn, config = undefined) {
        super(tmn, ServiceTrn, config);
        this.Verb = (config === null || config === void 0 ? void 0 : config.Verb) || "";
        this.Trn = trn;
        this.Attributes = (config === null || config === void 0 ? void 0 : config.Attributes) || {};
        this.ExtendedLocation = (config === null || config === void 0 ? void 0 : config.ExtendedLocation) || undefined;
        if (config === null || config === void 0 ? void 0 : config.Payload) {
            this.setPayload(config === null || config === void 0 ? void 0 : config.Payload);
        }
        else {
            this.Payload = undefined;
        }
    }
    getVerb() {
        return this.Verb;
    }
    setPayload(payload) {
        switch (payload.Compression) {
            case constants_1.MessagePayloadCompression.COMPRESSION_PLAIN: {
                this.Payload = payload;
                break;
            }
        }
    }
    getPayloadBody() {
        var _a;
        switch ((_a = this.Payload) === null || _a === void 0 ? void 0 : _a.Compression) {
            case constants_1.MessagePayloadCompression.COMPRESSION_PLAIN: {
                return this.Payload.Body;
            }
        }
    }
    hasPayload() {
        return this.Payload != undefined;
    }
    GetPayload(creatorClass, isArray = false) {
        const parser = new jackson_js_1.JsonParser();
        return parser.parse(JSON.stringify(this.getPayloadBody()), {
            mainCreator: () => isArray ? [Array, [creatorClass]] : [creatorClass],
            features: {
                deserialization: {
                    FAIL_ON_UNKNOWN_PROPERTIES: false
                }
            }
        });
    }
    addAttribute(key, value) {
        this.Attributes[key] = value;
    }
    isExtended() {
        return this.ExtendedLocation != undefined;
    }
    getExtendedMessageMetadataBucket() {
        var _a;
        return (_a = this.ExtendedLocation) === null || _a === void 0 ? void 0 : _a.Bucket;
    }
    getExtendedMessageMetadataReference() {
        var _a;
        return (_a = this.ExtendedLocation) === null || _a === void 0 ? void 0 : _a.MetadataKey;
    }
};
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonSerialize)({ using: message_1.TRNSerializer.serialize }),
    (0, jackson_js_1.JsonDeserialize)({ using: message_1.TRNSerializer.deserialize }),
    (0, jackson_js_1.JsonClassType)({ type: () => [message_1.TRN] })
], EventMessage.prototype, "Trn", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Object] })
], EventMessage.prototype, "Attributes", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [ExtendedMessageLocation] })
], EventMessage.prototype, "ExtendedLocation", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [MessagePayload] })
], EventMessage.prototype, "Payload", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)()
], EventMessage.prototype, "Verb", void 0);
EventMessage = EventMessage_1 = __decorate([
    (0, jackson_js_1.JsonNaming)({ strategy: jackson_js_1.PropertyNamingStrategy.KEBAB_CASE })
], EventMessage);
exports.EventMessage = EventMessage;
//# sourceMappingURL=event-message.js.map