"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEPLOYMENTS = void 0;
exports.getDeployedAddress = getDeployedAddress;
exports.getAllDeployedAddresses = getAllDeployedAddresses;
exports.getDeployedBlockNumber = getDeployedBlockNumber;
exports.getContractInfoFromAddress = getContractInfoFromAddress;
const deployments_ = __importStar(require("../broadcast/deployed-addresses.json"));
exports.DEPLOYMENTS = deployments_.chains;
/**
 * Returns the deployed address of any contract on any network.
 */
function getDeployedAddress(contractName, networkId, throwOnError = true) {
    const address = exports.DEPLOYMENTS[networkId.toString()]?.contracts[contractName]?.address;
    if (!address && throwOnError) {
        throw new Error(`Contract ${contractName} not found on ${networkId} in deployments.json`);
    }
    return address;
}
/**
 * Returns all active deployments for a given contract name across all chains.
 * Each result contains chainId, address, and blockNumber.
 */
function getAllDeployedAddresses(contractName) {
    const results = [];
    Object.keys(exports.DEPLOYMENTS).forEach((_chainId) => {
        const info = exports.DEPLOYMENTS[_chainId]?.contracts[contractName];
        if (info?.address) {
            results.push({ chainId: Number(_chainId), address: info.address, blockNumber: info.block_number });
        }
    });
    return results;
}
/**
 * Returns the deployment block number of any contract on any network.
 */
function getDeployedBlockNumber(contractName, networkId) {
    try {
        return exports.DEPLOYMENTS[networkId.toString()]?.contracts[contractName]?.block_number;
    }
    catch (_) {
        throw new Error(`Contract ${contractName} not found on ${networkId} in deployments.json`);
    }
}
/**
 * Returns the chainId and contract name for a given contract address.
 */
function getContractInfoFromAddress(contractAddress) {
    const returnValue = [];
    Object.keys(exports.DEPLOYMENTS).forEach((_chainId) => Object.keys(exports.DEPLOYMENTS[_chainId]?.contracts).forEach((_contractName) => {
        if (exports.DEPLOYMENTS[_chainId]?.contracts[_contractName]?.address === contractAddress)
            returnValue.push({ chainId: Number(_chainId), contractName: _contractName });
    }));
    if (returnValue.length === 0)
        throw new Error(`Contract ${contractAddress} not found in deployments.json`);
    if (returnValue.length > 1)
        throw new Error(`Multiple deployments found for ${contractAddress}`);
    return returnValue[0];
}
