"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImpliedBundleBlockRanges = exports.getBlockRangeForChain = exports.getBlockForChain = void 0;
function getBlockForChain(bundleEvaluationBlockNumbers, chain, chainIdListForBundleEvaluationBlockNumbers) {
    var indexForChain = chainIdListForBundleEvaluationBlockNumbers.indexOf(chain);
    if (indexForChain === -1) {
        throw new Error("Could not find chain ".concat(chain, " in chain ID list ").concat(chainIdListForBundleEvaluationBlockNumbers));
    }
    var blockForChain = bundleEvaluationBlockNumbers[indexForChain];
    if (blockForChain === undefined) {
        throw new Error("Invalid block range for chain ".concat(chain));
    }
    return blockForChain;
}
exports.getBlockForChain = getBlockForChain;
function getBlockRangeForChain(blockRanges, chain, chainIdListForBundleEvaluationBlockNumbers) {
    var indexForChain = chainIdListForBundleEvaluationBlockNumbers.indexOf(chain);
    if (indexForChain === -1) {
        throw new Error("Could not find chain ".concat(chain, " in chain ID list ").concat(chainIdListForBundleEvaluationBlockNumbers));
    }
    var blockRangeForChain = blockRanges[indexForChain];
    if (!blockRangeForChain || blockRangeForChain.length !== 2) {
        throw new Error("Invalid block range for chain ".concat(chain));
    }
    return blockRangeForChain;
}
exports.getBlockRangeForChain = getBlockRangeForChain;
function getImpliedBundleBlockRanges(hubPoolClient, configStoreClient, rootBundle) {
    var _a;
    var prevRootBundle = hubPoolClient.getLatestFullyExecutedRootBundle(rootBundle.blockNumber);
    var mainnetStartBlock = (_a = prevRootBundle === null || prevRootBundle === void 0 ? void 0 : prevRootBundle.bundleEvaluationBlockNumbers[0].toNumber()) !== null && _a !== void 0 ? _a : 0;
    var enabledChainsAtMainnetStartBlock = configStoreClient.getEnabledChains(mainnetStartBlock);
    var chainIdIndices = configStoreClient.getChainIdIndicesForBlock(rootBundle.blockNumber);
    var result = rootBundle.bundleEvaluationBlockNumbers.map(function (_endBlock, i) {
        var _a, _b, _c;
        var endBlock = _endBlock.toNumber();
        var bundleEndBlock = (_c = (_b = (_a = prevRootBundle === null || prevRootBundle === void 0 ? void 0 : prevRootBundle.bundleEvaluationBlockNumbers) === null || _a === void 0 ? void 0 : _a[i]) === null || _b === void 0 ? void 0 : _b.toNumber()) !== null && _c !== void 0 ? _c : 0;
        var fromBlock = bundleEndBlock === endBlock ? endBlock : bundleEndBlock + 1;
        var chainId = chainIdIndices[i];
        if (!enabledChainsAtMainnetStartBlock.includes(chainId)) {
            return [endBlock, endBlock];
        }
        return [fromBlock, endBlock];
    });
    result.forEach(function (_a, i) {
        var start = _a[0], end = _a[1];
        var chainId = chainIdIndices[i];
        if (enabledChainsAtMainnetStartBlock.includes(chainId)) {
            if (start > end) {
                throw new Error("Invalid block range for enabled chain ".concat(chainId, ": start block ").concat(start, " is greater than end block ").concat(end));
            }
        }
        else {
            if (start !== end) {
                throw new Error("Invalid block range for disabled chain ".concat(chainId, ": start block ").concat(start, " is not equal to end block ").concat(end));
            }
        }
    });
    return result;
}
exports.getImpliedBundleBlockRanges = getImpliedBundleBlockRanges;
//# sourceMappingURL=BundleUtils.js.map