import { Program } from "@coral-xyz/anchor";
import { getDeployedAddress } from "@across-protocol/contracts";
import { MessageTransmitterIdl, MulticallHandlerIdl, SvmSpokeIdl, TokenMessengerMinterIdl, MessageTransmitterV2Idl, TokenMessengerMinterV2Idl, SponsoredCctpSrcPeripheryIdl, } from "../assets";
import { getSolanaChainId, isSolanaDevnet } from "./helpers";
export function getConnectedProgram(idl, provider, programId) {
    idl.address = programId;
    return new Program(idl, provider);
}
// Resolves the program ID from options or defaults to the deployed address. Prioritizes programId, falls back to
// network, and if network is not defined, determines the network from the provider's RPC URL. Throws an error if
// the program ID cannot be resolved.
function resolveProgramId(programName, provider, options) {
    var _a = options !== null && options !== void 0 ? options : {}, network = _a.network, programId = _a.programId;
    if (programId) {
        return programId; // Prioritize explicitly provided programId
    }
    var resolvedNetwork = network !== null && network !== void 0 ? network : (isSolanaDevnet(provider) ? "devnet" : "mainnet");
    var deployedAddress = getDeployedAddress(programName, getSolanaChainId(resolvedNetwork).toString());
    if (!deployedAddress) {
        throw new Error("".concat(programName, " Program ID not found for ").concat(resolvedNetwork));
    }
    return deployedAddress;
}
export function getSpokePoolProgram(provider, options) {
    var id = resolveProgramId("SvmSpoke", provider, options);
    return getConnectedProgram(SvmSpokeIdl, provider, id);
}
export function getMessageTransmitterProgram(provider, options) {
    var id = resolveProgramId("MessageTransmitter", provider, options);
    return getConnectedProgram(MessageTransmitterIdl, provider, id);
}
export function getTokenMessengerMinterProgram(provider, options) {
    var id = resolveProgramId("TokenMessengerMinter", provider, options);
    return getConnectedProgram(TokenMessengerMinterIdl, provider, id);
}
export function getMulticallHandlerProgram(provider, options) {
    var id = resolveProgramId("MulticallHandler", provider, options);
    return getConnectedProgram(MulticallHandlerIdl, provider, id);
}
export function getMessageTransmitterV2Program(provider, options) {
    var id = resolveProgramId("MessageTransmitterV2", provider, options);
    return getConnectedProgram(MessageTransmitterV2Idl, provider, id);
}
export function getTokenMessengerMinterV2Program(provider, options) {
    var id = resolveProgramId("TokenMessengerMinterV2", provider, options);
    return getConnectedProgram(TokenMessengerMinterV2Idl, provider, id);
}
export function getSponsoredCctpSrcPeripheryProgram(provider, options) {
    var id = resolveProgramId("SponsoredCctpSrcPeriphery", provider, options);
    return getConnectedProgram(SponsoredCctpSrcPeripheryIdl, provider, id);
}
//# sourceMappingURL=programConnectors.js.map