/**
 * This code was AUTOGENERATED using the Codama library.
 * Please DO NOT EDIT THIS FILE, instead use visitors
 * to add features, then rerun Codama to update it.
 *
 * @see https://github.com/codama-idl/codama
 */
import { __assign, __awaiter, __generator } from "tslib";
import { combineCodec, fixDecoderSize, fixEncoderSize, getAddressDecoder, getAddressEncoder, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, getU32Decoder, getU32Encoder, getU64Decoder, getU64Encoder, transformEncoder, } from "@solana/kit";
import { findEventAuthorityPda, findMessageTransmitterPda } from "../pdas";
import { MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory } from "../shared";
export var INITIALIZE_DISCRIMINATOR = new Uint8Array([
    175, 175, 109, 31, 13, 152, 155, 237,
]);
export function getInitializeDiscriminatorBytes() {
    return fixEncoderSize(getBytesEncoder(), 8).encode(INITIALIZE_DISCRIMINATOR);
}
export function getInitializeInstructionDataEncoder() {
    return transformEncoder(getStructEncoder([
        ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
        ["localDomain", getU32Encoder()],
        ["attester", getAddressEncoder()],
        ["maxMessageBodySize", getU64Encoder()],
        ["version", getU32Encoder()],
    ]), function (value) { return (__assign(__assign({}, value), { discriminator: INITIALIZE_DISCRIMINATOR })); });
}
export function getInitializeInstructionDataDecoder() {
    return getStructDecoder([
        ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
        ["localDomain", getU32Decoder()],
        ["attester", getAddressDecoder()],
        ["maxMessageBodySize", getU64Decoder()],
        ["version", getU32Decoder()],
    ]);
}
export function getInitializeInstructionDataCodec() {
    return combineCodec(getInitializeInstructionDataEncoder(), getInitializeInstructionDataDecoder());
}
export function getInitializeInstructionAsync(input, config) {
    return __awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, _b, getAccountMeta;
        var _c, _d, _e, _f, _g, _h, _j, _k, _l;
        return __generator(this, function (_m) {
            switch (_m.label) {
                case 0:
                    programAddress = (_c = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _c !== void 0 ? _c : MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;
                    originalAccounts = {
                        payer: { value: (_d = input.payer) !== null && _d !== void 0 ? _d : null, isWritable: true },
                        upgradeAuthority: {
                            value: (_e = input.upgradeAuthority) !== null && _e !== void 0 ? _e : null,
                            isWritable: false,
                        },
                        messageTransmitter: {
                            value: (_f = input.messageTransmitter) !== null && _f !== void 0 ? _f : null,
                            isWritable: true,
                        },
                        messageTransmitterProgramData: {
                            value: (_g = input.messageTransmitterProgramData) !== null && _g !== void 0 ? _g : null,
                            isWritable: false,
                        },
                        messageTransmitterProgram: {
                            value: (_h = input.messageTransmitterProgram) !== null && _h !== void 0 ? _h : null,
                            isWritable: false,
                        },
                        systemProgram: { value: (_j = input.systemProgram) !== null && _j !== void 0 ? _j : null, isWritable: false },
                        eventAuthority: { value: (_k = input.eventAuthority) !== null && _k !== void 0 ? _k : null, isWritable: false },
                        program: { value: (_l = input.program) !== null && _l !== void 0 ? _l : null, isWritable: false },
                    };
                    accounts = originalAccounts;
                    args = __assign({}, input);
                    if (!!accounts.messageTransmitter.value) return [3 /*break*/, 2];
                    _a = accounts.messageTransmitter;
                    return [4 /*yield*/, findMessageTransmitterPda()];
                case 1:
                    _a.value = _m.sent();
                    _m.label = 2;
                case 2:
                    if (!accounts.messageTransmitterProgram.value) {
                        accounts.messageTransmitterProgram.value =
                            "CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC";
                    }
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3 /*break*/, 4];
                    _b = accounts.eventAuthority;
                    return [4 /*yield*/, findEventAuthorityPda()];
                case 3:
                    _b.value = _m.sent();
                    _m.label = 4;
                case 4:
                    getAccountMeta = getAccountMetaFactory(programAddress, "programId");
                    return [2 /*return*/, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.payer),
                                getAccountMeta(accounts.upgradeAuthority),
                                getAccountMeta(accounts.messageTransmitter),
                                getAccountMeta(accounts.messageTransmitterProgramData),
                                getAccountMeta(accounts.messageTransmitterProgram),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getInitializeInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
export function getInitializeInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    // Program address.
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;
    // Original accounts.
    var originalAccounts = {
        payer: { value: (_b = input.payer) !== null && _b !== void 0 ? _b : null, isWritable: true },
        upgradeAuthority: {
            value: (_c = input.upgradeAuthority) !== null && _c !== void 0 ? _c : null,
            isWritable: false,
        },
        messageTransmitter: {
            value: (_d = input.messageTransmitter) !== null && _d !== void 0 ? _d : null,
            isWritable: true,
        },
        messageTransmitterProgramData: {
            value: (_e = input.messageTransmitterProgramData) !== null && _e !== void 0 ? _e : null,
            isWritable: false,
        },
        messageTransmitterProgram: {
            value: (_f = input.messageTransmitterProgram) !== null && _f !== void 0 ? _f : null,
            isWritable: false,
        },
        systemProgram: { value: (_g = input.systemProgram) !== null && _g !== void 0 ? _g : null, isWritable: false },
        eventAuthority: { value: (_h = input.eventAuthority) !== null && _h !== void 0 ? _h : null, isWritable: false },
        program: { value: (_j = input.program) !== null && _j !== void 0 ? _j : null, isWritable: false },
    };
    var accounts = originalAccounts;
    // Original args.
    var args = __assign({}, input);
    // Resolve default values.
    if (!accounts.messageTransmitterProgram.value) {
        accounts.messageTransmitterProgram.value =
            "CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC";
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            "11111111111111111111111111111111";
    }
    var getAccountMeta = getAccountMetaFactory(programAddress, "programId");
    return Object.freeze({
        accounts: [
            getAccountMeta(accounts.payer),
            getAccountMeta(accounts.upgradeAuthority),
            getAccountMeta(accounts.messageTransmitter),
            getAccountMeta(accounts.messageTransmitterProgramData),
            getAccountMeta(accounts.messageTransmitterProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getInitializeInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
export function parseInitializeInstruction(instruction) {
    if (instruction.accounts.length < 8) {
        // TODO: Coded error.
        throw new Error("Not enough accounts");
    }
    var accountIndex = 0;
    var getNextAccount = function () {
        var accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: {
            payer: getNextAccount(),
            upgradeAuthority: getNextAccount(),
            messageTransmitter: getNextAccount(),
            messageTransmitterProgramData: getNextAccount(),
            messageTransmitterProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getInitializeInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=initialize.js.map