"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    UnsafeJsonStruct: function() {
        return UnsafeJsonStruct;
    },
    JsonStruct: function() {
        return JsonStruct;
    },
    isValidJson: function() {
        return isValidJson;
    },
    getSafeJson: function() {
        return getSafeJson;
    },
    getJsonSize: function() {
        return getJsonSize;
    },
    jsonrpc2: function() {
        return jsonrpc2;
    },
    JsonRpcVersionStruct: function() {
        return JsonRpcVersionStruct;
    },
    JsonRpcIdStruct: function() {
        return JsonRpcIdStruct;
    },
    JsonRpcErrorStruct: function() {
        return JsonRpcErrorStruct;
    },
    JsonRpcParamsStruct: function() {
        return JsonRpcParamsStruct;
    },
    JsonRpcRequestStruct: function() {
        return JsonRpcRequestStruct;
    },
    JsonRpcNotificationStruct: function() {
        return JsonRpcNotificationStruct;
    },
    isJsonRpcNotification: function() {
        return isJsonRpcNotification;
    },
    assertIsJsonRpcNotification: function() {
        return assertIsJsonRpcNotification;
    },
    isJsonRpcRequest: function() {
        return isJsonRpcRequest;
    },
    assertIsJsonRpcRequest: function() {
        return assertIsJsonRpcRequest;
    },
    PendingJsonRpcResponseStruct: function() {
        return PendingJsonRpcResponseStruct;
    },
    JsonRpcSuccessStruct: function() {
        return JsonRpcSuccessStruct;
    },
    JsonRpcFailureStruct: function() {
        return JsonRpcFailureStruct;
    },
    JsonRpcResponseStruct: function() {
        return JsonRpcResponseStruct;
    },
    isPendingJsonRpcResponse: function() {
        return isPendingJsonRpcResponse;
    },
    assertIsPendingJsonRpcResponse: function() {
        return assertIsPendingJsonRpcResponse;
    },
    isJsonRpcResponse: function() {
        return isJsonRpcResponse;
    },
    assertIsJsonRpcResponse: function() {
        return assertIsJsonRpcResponse;
    },
    isJsonRpcSuccess: function() {
        return isJsonRpcSuccess;
    },
    assertIsJsonRpcSuccess: function() {
        return assertIsJsonRpcSuccess;
    },
    isJsonRpcFailure: function() {
        return isJsonRpcFailure;
    },
    assertIsJsonRpcFailure: function() {
        return assertIsJsonRpcFailure;
    },
    isJsonRpcError: function() {
        return isJsonRpcError;
    },
    assertIsJsonRpcError: function() {
        return assertIsJsonRpcError;
    },
    getJsonRpcIdValidator: function() {
        return getJsonRpcIdValidator;
    }
});
const _superstruct = require("superstruct");
const _assert = require("./assert");
/**
 * A struct to check if the given value is finite number. Superstruct's
 * `number()` struct does not check if the value is finite.
 *
 * @returns A struct to check if the given value is finite number.
 */ const finiteNumber = ()=>(0, _superstruct.define)('finite number', (value)=>{
        return (0, _superstruct.is)(value, (0, _superstruct.number)()) && Number.isFinite(value);
    });
const UnsafeJsonStruct = (0, _superstruct.union)([
    (0, _superstruct.literal)(null),
    (0, _superstruct.boolean)(),
    finiteNumber(),
    (0, _superstruct.string)(),
    (0, _superstruct.array)((0, _superstruct.lazy)(()=>UnsafeJsonStruct)),
    (0, _superstruct.record)((0, _superstruct.string)(), (0, _superstruct.lazy)(()=>UnsafeJsonStruct))
]);
const JsonStruct = (0, _superstruct.coerce)(UnsafeJsonStruct, (0, _superstruct.any)(), (value)=>{
    (0, _assert.assertStruct)(value, UnsafeJsonStruct);
    return JSON.parse(JSON.stringify(value, (propKey, propValue)=>{
        // Strip __proto__ and constructor properties to prevent prototype pollution.
        if (propKey === '__proto__' || propKey === 'constructor') {
            return undefined;
        }
        return propValue;
    }));
});
function isValidJson(value) {
    try {
        getSafeJson(value);
        return true;
    } catch  {
        return false;
    }
}
function getSafeJson(value) {
    return (0, _superstruct.create)(value, JsonStruct);
}
function getJsonSize(value) {
    (0, _assert.assertStruct)(value, JsonStruct, 'Invalid JSON value');
    const json = JSON.stringify(value);
    return new TextEncoder().encode(json).byteLength;
}
const jsonrpc2 = '2.0';
const JsonRpcVersionStruct = (0, _superstruct.literal)(jsonrpc2);
const JsonRpcIdStruct = (0, _superstruct.nullable)((0, _superstruct.union)([
    (0, _superstruct.number)(),
    (0, _superstruct.string)()
]));
const JsonRpcErrorStruct = (0, _superstruct.object)({
    code: (0, _superstruct.integer)(),
    message: (0, _superstruct.string)(),
    data: (0, _superstruct.optional)(JsonStruct),
    stack: (0, _superstruct.optional)((0, _superstruct.string)())
});
const JsonRpcParamsStruct = (0, _superstruct.union)([
    (0, _superstruct.record)((0, _superstruct.string)(), JsonStruct),
    (0, _superstruct.array)(JsonStruct)
]);
const JsonRpcRequestStruct = (0, _superstruct.object)({
    id: JsonRpcIdStruct,
    jsonrpc: JsonRpcVersionStruct,
    method: (0, _superstruct.string)(),
    params: (0, _superstruct.optional)(JsonRpcParamsStruct)
});
const JsonRpcNotificationStruct = (0, _superstruct.object)({
    jsonrpc: JsonRpcVersionStruct,
    method: (0, _superstruct.string)(),
    params: (0, _superstruct.optional)(JsonRpcParamsStruct)
});
function isJsonRpcNotification(value) {
    return (0, _superstruct.is)(value, JsonRpcNotificationStruct);
}
function assertIsJsonRpcNotification(value, // eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper) {
    (0, _assert.assertStruct)(value, JsonRpcNotificationStruct, 'Invalid JSON-RPC notification', ErrorWrapper);
}
function isJsonRpcRequest(value) {
    return (0, _superstruct.is)(value, JsonRpcRequestStruct);
}
function assertIsJsonRpcRequest(value, // eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper) {
    (0, _assert.assertStruct)(value, JsonRpcRequestStruct, 'Invalid JSON-RPC request', ErrorWrapper);
}
const PendingJsonRpcResponseStruct = (0, _superstruct.object)({
    id: JsonRpcIdStruct,
    jsonrpc: JsonRpcVersionStruct,
    result: (0, _superstruct.optional)((0, _superstruct.unknown)()),
    error: (0, _superstruct.optional)(JsonRpcErrorStruct)
});
const JsonRpcSuccessStruct = (0, _superstruct.object)({
    id: JsonRpcIdStruct,
    jsonrpc: JsonRpcVersionStruct,
    result: JsonStruct
});
const JsonRpcFailureStruct = (0, _superstruct.object)({
    id: JsonRpcIdStruct,
    jsonrpc: JsonRpcVersionStruct,
    error: JsonRpcErrorStruct
});
const JsonRpcResponseStruct = (0, _superstruct.union)([
    JsonRpcSuccessStruct,
    JsonRpcFailureStruct
]);
function isPendingJsonRpcResponse(response) {
    return (0, _superstruct.is)(response, PendingJsonRpcResponseStruct);
}
function assertIsPendingJsonRpcResponse(response, // eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper) {
    (0, _assert.assertStruct)(response, PendingJsonRpcResponseStruct, 'Invalid pending JSON-RPC response', ErrorWrapper);
}
function isJsonRpcResponse(response) {
    return (0, _superstruct.is)(response, JsonRpcResponseStruct);
}
function assertIsJsonRpcResponse(value, // eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper) {
    (0, _assert.assertStruct)(value, JsonRpcResponseStruct, 'Invalid JSON-RPC response', ErrorWrapper);
}
function isJsonRpcSuccess(value) {
    return (0, _superstruct.is)(value, JsonRpcSuccessStruct);
}
function assertIsJsonRpcSuccess(value, // eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper) {
    (0, _assert.assertStruct)(value, JsonRpcSuccessStruct, 'Invalid JSON-RPC success response', ErrorWrapper);
}
function isJsonRpcFailure(value) {
    return (0, _superstruct.is)(value, JsonRpcFailureStruct);
}
function assertIsJsonRpcFailure(value, // eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper) {
    (0, _assert.assertStruct)(value, JsonRpcFailureStruct, 'Invalid JSON-RPC failure response', ErrorWrapper);
}
function isJsonRpcError(value) {
    return (0, _superstruct.is)(value, JsonRpcErrorStruct);
}
function assertIsJsonRpcError(value, // eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper) {
    (0, _assert.assertStruct)(value, JsonRpcErrorStruct, 'Invalid JSON-RPC error', ErrorWrapper);
}
function getJsonRpcIdValidator(options) {
    const { permitEmptyString, permitFractions, permitNull } = {
        permitEmptyString: true,
        permitFractions: false,
        permitNull: true,
        ...options
    };
    /**
   * Type guard for {@link JsonRpcId}.
   *
   * @param id - The JSON-RPC ID value to check.
   * @returns Whether the given ID is valid per the options given to the
   * factory.
   */ const isValidJsonRpcId = (id)=>{
        return Boolean(typeof id === 'number' && (permitFractions || Number.isInteger(id)) || typeof id === 'string' && (permitEmptyString || id.length > 0) || permitNull && id === null);
    };
    return isValidJsonRpcId;
}

//# sourceMappingURL=json.js.map