"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheType = exports.compareRpcResults = exports.createSendErrorWithMessage = exports.formatProviderError = exports.compareArrayResultsWithIgnoredKeys = exports.compareResultsAndFilterIgnoredKeys = exports.getURL = exports.isSupportedProvider = void 0;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var lodash_1 = require("lodash");
var utils_1 = require("../utils");
var alchemy = tslib_1.__importStar(require("./alchemy"));
var infura = tslib_1.__importStar(require("./infura"));
var drpc = tslib_1.__importStar(require("./drpc"));
var PROVIDERS = {
    ALCHEMY: alchemy.getURL,
    INFURA: infura.getURL,
    INFURA_DIN: infura.getURL,
    DRPC: drpc.getURL,
};
function isSupportedProvider(provider) {
    return ["ALCHEMY", "INFURA", "INFURA_DIN", "DRPC"].includes(provider);
}
exports.isSupportedProvider = isSupportedProvider;
function getURL(provider, chainId, apiKey, transport) {
    if (transport === void 0) { transport = "https"; }
    var getURL = PROVIDERS[provider];
    (0, assert_1.default)(getURL, "Unsupported RPC provider (".concat(provider, ")"));
    return getURL(chainId, apiKey, transport);
}
exports.getURL = getURL;
function deleteIgnoredKeys(ignoredKeys, obj) {
    if (!(0, utils_1.isDefined)(obj)) {
        return;
    }
    var newObj = tslib_1.__assign({}, obj);
    for (var _i = 0, ignoredKeys_1 = ignoredKeys; _i < ignoredKeys_1.length; _i++) {
        var key = ignoredKeys_1[_i];
        delete newObj[key];
    }
    return newObj;
}
function compareResultsAndFilterIgnoredKeys(ignoredKeys, _objA, _objB) {
    var filteredA = deleteIgnoredKeys(ignoredKeys, _objA);
    var filteredB = deleteIgnoredKeys(ignoredKeys, _objB);
    return (0, lodash_1.isEqual)(filteredA, filteredB);
}
exports.compareResultsAndFilterIgnoredKeys = compareResultsAndFilterIgnoredKeys;
function compareArrayResultsWithIgnoredKeys(ignoredKeys, objA, objB) {
    var filteredA = objA === null || objA === void 0 ? void 0 : objA.map(function (obj) { return deleteIgnoredKeys(ignoredKeys, obj); });
    var filteredB = objB === null || objB === void 0 ? void 0 : objB.map(function (obj) { return deleteIgnoredKeys(ignoredKeys, obj); });
    return (0, utils_1.isDefined)(filteredA) && (0, utils_1.isDefined)(filteredB) && (0, lodash_1.isEqual)(filteredA, filteredB);
}
exports.compareArrayResultsWithIgnoredKeys = compareArrayResultsWithIgnoredKeys;
var IGNORED_FIELDS = {
    eth_getBlockByNumber: [
        "miner",
        "l1BatchNumber",
        "l1BatchTimestamp",
        "size",
        "totalDifficulty",
        "logsBloom",
        "transactions",
    ],
    eth_getLogs: ["blockTimestamp", "transactionLogIndex", "l1BatchNumber", "logType"],
};
function formatProviderError(provider, rawErrorText) {
    return "Provider ".concat(provider.connection.url, " failed with error: ").concat(rawErrorText);
}
exports.formatProviderError = formatProviderError;
function createSendErrorWithMessage(message, sendError) {
    var error = new Error(message);
    return tslib_1.__assign(tslib_1.__assign({}, sendError), error);
}
exports.createSendErrorWithMessage = createSendErrorWithMessage;
function compareRpcResults(method, rpcResultA, rpcResultB) {
    if (method === "eth_getBlockByNumber") {
        return compareResultsAndFilterIgnoredKeys(IGNORED_FIELDS.eth_getBlockByNumber, rpcResultA, rpcResultB);
    }
    else if (method === "eth_getLogs") {
        return compareArrayResultsWithIgnoredKeys(IGNORED_FIELDS.eth_getLogs, rpcResultA, rpcResultB);
    }
    else {
        return (0, lodash_1.isEqual)(rpcResultA, rpcResultB);
    }
}
exports.compareRpcResults = compareRpcResults;
var CacheType;
(function (CacheType) {
    CacheType[CacheType["NONE"] = 0] = "NONE";
    CacheType[CacheType["WITH_TTL"] = 1] = "WITH_TTL";
    CacheType[CacheType["NO_TTL"] = 2] = "NO_TTL";
    CacheType[CacheType["DECIDE_TTL_POST_SEND"] = 3] = "DECIDE_TTL_POST_SEND";
})(CacheType || (exports.CacheType = CacheType = {}));
//# sourceMappingURL=utils.js.map