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