import { IsmType } from '@hyperlane-xyz/provider-sdk/altvm';
import { decodeMultisigIsmAccessControlAccount, decodeMultisigIsmDomainDataAccount, } from '../accounts/multisig-ism-message-id.js';
import { decodeTestIsmStorageAccount, } from '../accounts/test-ism.js';
import { decodeInterchainSecurityModuleInterfaceInstruction, decodeMultisigIsmInterfaceInstruction, } from '../instructions/interfaces.js';
import { decodeMultisigIsmMessageIdProgramInstruction } from '../instructions/multisig-ism-message-id.js';
import { deriveMultisigIsmAccessControlPda, deriveMultisigIsmDomainDataPda, deriveTestIsmStoragePda, } from '../pda.js';
import { fetchAccountDataRaw } from '../rpc.js';
export const decodeIsmInstruction = {
    interchainSecurityModule: decodeInterchainSecurityModuleInterfaceInstruction,
    multisigInterface: decodeMultisigIsmInterfaceInstruction,
    multisigProgram: decodeMultisigIsmMessageIdProgramInstruction,
};
export const decodeIsmAccount = {
    multisigAccessControl: decodeMultisigIsmAccessControlAccount,
    multisigDomainData: decodeMultisigIsmDomainDataAccount,
    testIsmStorage: decodeTestIsmStorageAccount,
};
export async function fetchTestIsmStorageAccount(rpc, programId) {
    const { address: storagePda } = await deriveTestIsmStoragePda(programId);
    const raw = await fetchAccountDataRaw(rpc, storagePda);
    if (!raw || raw.length === 0)
        return null;
    return decodeTestIsmStorageAccount(raw);
}
export async function fetchMultisigIsmAccessControl(rpc, programId) {
    const { address: accessControlPda } = await deriveMultisigIsmAccessControlPda(programId);
    const raw = await fetchAccountDataRaw(rpc, accessControlPda);
    if (!raw || raw.length === 0)
        return null;
    return decodeMultisigIsmAccessControlAccount(raw);
}
export async function fetchMultisigIsmDomainData(rpc, programId, domain) {
    const { address: domainPda } = await deriveMultisigIsmDomainDataPda(programId, domain);
    const raw = await fetchAccountDataRaw(rpc, domainPda);
    if (!raw || raw.length === 0)
        return null;
    return decodeMultisigIsmDomainDataAccount(raw);
}
export async function detectIsmType(rpc, programId) {
    const testIsmStorage = await fetchTestIsmStorageAccount(rpc, programId);
    if (testIsmStorage !== null) {
        return IsmType.TEST_ISM;
    }
    const accessControl = await fetchMultisigIsmAccessControl(rpc, programId);
    if (accessControl !== null) {
        return IsmType.MESSAGE_ID_MULTISIG;
    }
    throw new Error(`Unable to detect ISM type for program: ${programId}`);
}
export function validatorBytesToHex(validators) {
    return validators.map((v) => '0x' + Buffer.from(v).toString('hex'));
}
//# sourceMappingURL=ism-query.js.map