import { address as parseAddress } from '@solana/kit';
import { IsmType } from '@hyperlane-xyz/provider-sdk/altvm';
import { HYPERLANE_SVM_PROGRAM_BYTES } from '../hyperlane/program-bytes.js';
import { detectIsmType } from './ism-query.js';
import { SvmTestIsmReader, SvmTestIsmWriter } from './test-ism.js';
export class SvmIsmArtifactManager {
    rpc;
    constructor(rpc) {
        this.rpc = rpc;
    }
    async readIsm(address) {
        const programId = parseAddress(address);
        const ismType = await detectIsmType(this.rpc, programId);
        const typeKey = this.altVmToTypeKey(ismType);
        const reader = this.createReader(typeKey);
        return reader.read(address);
    }
    createReader(type) {
        const readers = {
            testIsm: () => new SvmTestIsmReader(this.rpc),
            // FIXME: SVM multisig ISM has a completely different shape from other msig ISMs
            messageIdMultisigIsm: () => {
                throw new Error('Multisig ISM reading not supported via artifact manager on SVM (different config shape). Use SvmMessageIdMultisigIsmReader directly.');
            },
        };
        const factory = readers[type];
        if (!factory)
            throw new Error(`Unsupported ISM type: ${type}`);
        return factory();
    }
    createWriter(type, signer) {
        const writers = {
            testIsm: () => new SvmTestIsmWriter({ program: { programBytes: HYPERLANE_SVM_PROGRAM_BYTES.testIsm } }, this.rpc, signer),
            // FIXME: SVM multisig ISM has a completely different shape from other msig ISMs
            messageIdMultisigIsm: () => {
                throw new Error('Multisig ISM deployment not supported via artifact manager on SVM (different config shape). Use SvmMessageIdMultisigIsmWriter directly.');
            },
        };
        const factory = writers[type];
        if (!factory)
            throw new Error(`Unsupported ISM type: ${type}`);
        return factory();
    }
    altVmToTypeKey(ismType) {
        switch (ismType) {
            case IsmType.TEST_ISM:
                return 'testIsm';
            case IsmType.MESSAGE_ID_MULTISIG:
                return 'messageIdMultisigIsm';
            default:
                throw new Error(`Unsupported ISM type on Solana: ${ismType}`);
        }
    }
}
//# sourceMappingURL=ism-artifact-manager.js.map