import { address as parseAddress } from '@solana/kit';
import { IsmType } from '@hyperlane-xyz/provider-sdk/altvm';
import { ArtifactState, } from '@hyperlane-xyz/provider-sdk/artifact';
import { resolveProgram } from '../deploy/resolve-program.js';
import { getInitTestIsmInstruction } from '../instructions/test-ism.js';
import { fetchTestIsmStorageAccount } from './ism-query.js';
export class SvmTestIsmReader {
    rpc;
    constructor(rpc) {
        this.rpc = rpc;
    }
    async read(address) {
        const programId = parseAddress(address);
        const storage = await fetchTestIsmStorageAccount(this.rpc, programId);
        if (storage === null) {
            throw new Error(`Test ISM not initialized at program: ${programId}`);
        }
        return {
            artifactState: ArtifactState.DEPLOYED,
            config: { type: IsmType.TEST_ISM },
            deployed: { address: programId, programId },
        };
    }
}
export class SvmTestIsmWriter extends SvmTestIsmReader {
    config;
    svmSigner;
    constructor(config, rpc, svmSigner) {
        super(rpc);
        this.config = config;
        this.svmSigner = svmSigner;
    }
    async create(_artifact) {
        const { programAddress, receipts } = await resolveProgram(this.config.program, this.svmSigner, this.rpc);
        const storage = await fetchTestIsmStorageAccount(this.rpc, programAddress);
        if (storage === null) {
            const instruction = await getInitTestIsmInstruction(programAddress, this.svmSigner.signer);
            const initReceipt = await this.svmSigner.send({
                instructions: [instruction],
                skipPreflight: true,
            });
            receipts.push(initReceipt);
        }
        return [
            {
                artifactState: ArtifactState.DEPLOYED,
                config: { type: IsmType.TEST_ISM },
                deployed: { address: programAddress, programId: programAddress },
            },
            receipts,
        ];
    }
    async update(_artifact) {
        return [];
    }
}
//# sourceMappingURL=test-ism.js.map