"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Serialize = exports.Deserialize = void 0;
const jackson_js_1 = require("jackson-js");
const command_message_1 = require("./command-message");
const constants_1 = require("./constants");
const event_message_1 = require("./event-message");
const message_1 = require("./message");
const jack_x_1 = require("../jack-x");
const Deserialize = (payload) => {
    const tmnParser = new jackson_js_1.JsonParser();
    let tmn;
    try {
        tmn = tmnParser.parse(payload, {
            mainCreator: () => [message_1.TMN],
            features: {
                deserialization: {
                    FAIL_ON_UNKNOWN_PROPERTIES: false
                }
            }
        });
    }
    catch (_a) {
        throw new TypeError("malformed payload");
    }
    if (tmn.getType() == constants_1.MessageType.MESSAGE_TYPE_COMMAND) {
        const mapper = new jackson_js_1.JsonParser();
        return mapper.parse(payload, {
            mainCreator: () => [command_message_1.CommandMessage],
            features: {
                deserialization: {
                    FAIL_ON_UNKNOWN_PROPERTIES: false
                }
            }
        });
    }
    const mapper = new jackson_js_1.JsonParser();
    return mapper.parse(payload, {
        mainCreator: () => [event_message_1.EventMessage],
        features: {
            deserialization: {
                FAIL_ON_UNKNOWN_PROPERTIES: false
            }
        }
    });
};
exports.Deserialize = Deserialize;
const Serialize = (message) => {
    return (0, jack_x_1.serialize)(message);
};
exports.Serialize = Serialize;
//# sourceMappingURL=serializer.js.map