import { JsonParser } from "jackson-js";
import { CommandMessage } from "./command-message";
import { MessageType } from "./constants";
import { EventMessage } from "./event-message";
import { TMN } from "./message";
import { serialize } from "../jack-x";
export const Deserialize = (payload) => {
    const tmnParser = new JsonParser();
    let tmn;
    try {
        tmn = tmnParser.parse(payload, {
            mainCreator: () => [TMN],
            features: {
                deserialization: {
                    FAIL_ON_UNKNOWN_PROPERTIES: false
                }
            }
        });
    }
    catch (_a) {
        throw new TypeError("malformed payload");
    }
    if (tmn.getType() == MessageType.MESSAGE_TYPE_COMMAND) {
        const mapper = new JsonParser();
        return mapper.parse(payload, {
            mainCreator: () => [CommandMessage],
            features: {
                deserialization: {
                    FAIL_ON_UNKNOWN_PROPERTIES: false
                }
            }
        });
    }
    const mapper = new JsonParser();
    return mapper.parse(payload, {
        mainCreator: () => [EventMessage],
        features: {
            deserialization: {
                FAIL_ON_UNKNOWN_PROPERTIES: false
            }
        }
    });
};
export const Serialize = (message) => {
    return serialize(message);
};
//# sourceMappingURL=serializer.js.map