import { getAddressCodec } from '@solana/kit';
import { assert } from '@hyperlane-xyz/utils';
import { decodeAccountData } from '../codecs/account-data.js';
import { deriveValidatorAnnouncePda, deriveValidatorStorageLocationsPda, } from '../pda.js';
import { fetchAccountDataRaw } from '../rpc.js';
const ADDRESS_CODEC = getAddressCodec();
export function decodeValidatorAnnounceAccount(raw) {
    const { data } = decodeAccountData(raw, (cursor) => ({
        bumpSeed: cursor.readU8(),
        mailbox: cursor.readWithDecoder(ADDRESS_CODEC),
        localDomain: cursor.readU32LE(),
    }));
    return data;
}
export function decodeValidatorStorageLocationsAccount(raw) {
    const { data } = decodeAccountData(raw, (cursor) => {
        const bumpSeed = cursor.readU8();
        const count = cursor.readU32LE();
        const storageLocations = [];
        for (let i = 0; i < count; i++) {
            storageLocations.push(cursor.readString());
        }
        return { bumpSeed, storageLocations };
    });
    return data;
}
export async function fetchValidatorAnnounceAccount(rpc, programId) {
    const { address: announcePda } = await deriveValidatorAnnouncePda(programId);
    const raw = await fetchAccountDataRaw(rpc, announcePda);
    if (!raw)
        return null;
    return decodeValidatorAnnounceAccount(raw);
}
export async function fetchValidatorStorageLocations(rpc, programId, validatorH160) {
    assert(validatorH160.length === 20, `Validator address must be 20 bytes (H160), got ${validatorH160.length}`);
    const { address: storageLocationsPda } = await deriveValidatorStorageLocationsPda(programId, validatorH160);
    const raw = await fetchAccountDataRaw(rpc, storageLocationsPda);
    if (!raw)
        return null;
    return decodeValidatorStorageLocationsAccount(raw);
}
//# sourceMappingURL=validator-announce-query.js.map