"use strict";
// To parse this data:
//
//   import { Convert, Payload, Envelope, PayloadT, EnvelopeT, PayloadT1 } from "./file";
//
//   const payload = Convert.toPayload(json);
//   const envelope = Convert.toEnvelope(json);
//   const payloadT = Convert.toPayloadT(json);
//   const t = Convert.toT(json);
//   const envelopeT = Convert.toEnvelopeT(json);
//   const payloadT1 = Convert.toPayloadT1(json);
//   const t1 = Convert.toT1(json);
//
// These functions will throw an error if the JSON doesn't
// match the expected interface, even if the JSON is valid.
exports.__esModule = true;
exports.Convert = exports.V = void 0;
var V;
(function (V) {
    V["A"] = "A";
})(V = exports.V || (exports.V = {}));
// Converts JSON strings to/from your types
// and asserts the results of JSON.parse at runtime
var Convert = /** @class */ (function () {
    function Convert() {
    }
    Convert.toPayload = function (json) {
        return cast(JSON.parse(json), r("PayloadT"));
    };
    Convert.payloadToJson = function (value) {
        return JSON.stringify(uncast(value, r("PayloadT")), null, 2);
    };
    Convert.toEnvelope = function (json) {
        return cast(JSON.parse(json), r("EnvelopeT"));
    };
    Convert.envelopeToJson = function (value) {
        return JSON.stringify(uncast(value, r("EnvelopeT")), null, 2);
    };
    Convert.toPayloadT = function (json) {
        return cast(JSON.parse(json), r("PayloadT"));
    };
    Convert.payloadTToJson = function (value) {
        return JSON.stringify(uncast(value, r("PayloadT")), null, 2);
    };
    Convert.toT = function (json) {
        return cast(JSON.parse(json), m("any"));
    };
    Convert.tToJson = function (value) {
        return JSON.stringify(uncast(value, m("any")), null, 2);
    };
    Convert.toEnvelopeT = function (json) {
        return cast(JSON.parse(json), r("EnvelopeT"));
    };
    Convert.envelopeTToJson = function (value) {
        return JSON.stringify(uncast(value, r("EnvelopeT")), null, 2);
    };
    Convert.toPayloadT1 = function (json) {
        return cast(JSON.parse(json), r("PayloadT1"));
    };
    Convert.payloadT1ToJson = function (value) {
        return JSON.stringify(uncast(value, r("PayloadT1")), null, 2);
    };
    Convert.toT1 = function (json) {
        return cast(JSON.parse(json), m("any"));
    };
    Convert.t1ToJson = function (value) {
        return JSON.stringify(uncast(value, m("any")), null, 2);
    };
    return Convert;
}());
exports.Convert = Convert;
function invalidValue(typ, val, key) {
    if (key === void 0) { key = ''; }
    if (key) {
        throw Error("Invalid value for key \"" + key + "\". Expected type " + JSON.stringify(typ) + " but got " + JSON.stringify(val));
    }
    throw Error("Invalid value " + JSON.stringify(val) + " for type " + JSON.stringify(typ));
}
function jsonToJSProps(typ) {
    if (typ.jsonToJS === undefined) {
        var map_1 = {};
        typ.props.forEach(function (p) { return map_1[p.json] = { key: p.js, typ: p.typ }; });
        typ.jsonToJS = map_1;
    }
    return typ.jsonToJS;
}
function jsToJSONProps(typ) {
    if (typ.jsToJSON === undefined) {
        var map_2 = {};
        typ.props.forEach(function (p) { return map_2[p.js] = { key: p.json, typ: p.typ }; });
        typ.jsToJSON = map_2;
    }
    return typ.jsToJSON;
}
function transform(val, typ, getProps, key) {
    if (key === void 0) { key = ''; }
    function transformPrimitive(typ, val) {
        if (typeof typ === typeof val)
            return val;
        return invalidValue(typ, val, key);
    }
    function transformUnion(typs, val) {
        // val must validate against one typ in typs
        var l = typs.length;
        for (var i = 0; i < l; i++) {
            var typ_1 = typs[i];
            try {
                return transform(val, typ_1, getProps);
            }
            catch (_) { }
        }
        return invalidValue(typs, val);
    }
    function transformEnum(cases, val) {
        if (cases.indexOf(val) !== -1)
            return val;
        return invalidValue(cases, val);
    }
    function transformArray(typ, val) {
        // val must be an array with no invalid elements
        if (!Array.isArray(val))
            return invalidValue("array", val);
        return val.map(function (el) { return transform(el, typ, getProps); });
    }
    function transformDate(val) {
        if (val === null) {
            return null;
        }
        var d = new Date(val);
        if (isNaN(d.valueOf())) {
            return invalidValue("Date", val);
        }
        return d;
    }
    function transformObject(props, additional, val) {
        if (val === null || typeof val !== "object" || Array.isArray(val)) {
            return invalidValue("object", val);
        }
        var result = {};
        Object.getOwnPropertyNames(props).forEach(function (key) {
            var prop = props[key];
            var v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
            result[prop.key] = transform(v, prop.typ, getProps, prop.key);
        });
        Object.getOwnPropertyNames(val).forEach(function (key) {
            if (!Object.prototype.hasOwnProperty.call(props, key)) {
                result[key] = transform(val[key], additional, getProps, key);
            }
        });
        return result;
    }
    if (typ === "any")
        return val;
    if (typ === null) {
        if (val === null)
            return val;
        return invalidValue(typ, val);
    }
    if (typ === false)
        return invalidValue(typ, val);
    while (typeof typ === "object" && typ.ref !== undefined) {
        typ = typeMap[typ.ref];
    }
    if (Array.isArray(typ))
        return transformEnum(typ, val);
    if (typeof typ === "object") {
        return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
            : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val)
                : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val)
                    : invalidValue(typ, val);
    }
    // Numbers can be parsed by Date but shouldn't be.
    if (typ === Date && typeof val !== "number")
        return transformDate(val);
    return transformPrimitive(typ, val);
}
function cast(val, typ) {
    return transform(val, typ, jsonToJSProps);
}
function uncast(val, typ) {
    return transform(val, typ, jsToJSONProps);
}
function a(typ) {
    return { arrayItems: typ };
}
function u() {
    var typs = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        typs[_i] = arguments[_i];
    }
    return { unionMembers: typs };
}
function o(props, additional) {
    return { props: props, additional: additional };
}
function m(additional) {
    return { props: [], additional: additional };
}
function r(name) {
    return { ref: name };
}
var typeMap = {
    "PayloadT": o([
        { json: "data", js: "data", typ: m("any") },
        { json: "kind", js: "kind", typ: "" },
    ], "any"),
    "EnvelopeT": o([
        { json: "data", js: "data", typ: r("PayloadT1") },
        { json: "dst", js: "dst", typ: a("") },
        { json: "id", js: "id", typ: "" },
        { json: "src", js: "src", typ: "" },
        { json: "t", js: "t", typ: 3.14 },
        { json: "ttl", js: "ttl", typ: 3.14 },
        { json: "v", js: "v", typ: r("V") },
    ], "any"),
    "PayloadT1": o([
        { json: "data", js: "data", typ: m("any") },
        { json: "kind", js: "kind", typ: "" },
    ], "any"),
    "V": [
        "A",
    ]
};
