"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.INITIALIZE_DISCRIMINATOR = void 0;
exports.getInitializeDiscriminatorBytes = getInitializeDiscriminatorBytes;
exports.getInitializeInstructionDataEncoder = getInitializeInstructionDataEncoder;
exports.getInitializeInstructionDataDecoder = getInitializeInstructionDataDecoder;
exports.getInitializeInstructionDataCodec = getInitializeInstructionDataCodec;
exports.getInitializeInstruction = getInitializeInstruction;
exports.parseInitializeInstruction = parseInitializeInstruction;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
var programs_1 = require("../programs");
var shared_1 = require("../shared");
exports.INITIALIZE_DISCRIMINATOR = new Uint8Array([
    175, 175, 109, 31, 13, 152, 155, 237,
]);
function getInitializeDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.INITIALIZE_DISCRIMINATOR);
}
function getInitializeInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["localDomain", (0, kit_1.getU32Encoder)()],
        ["attester", (0, kit_1.getAddressEncoder)()],
        ["maxMessageBodySize", (0, kit_1.getU64Encoder)()],
        ["version", (0, kit_1.getU32Encoder)()],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.INITIALIZE_DISCRIMINATOR })); });
}
function getInitializeInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["localDomain", (0, kit_1.getU32Decoder)()],
        ["attester", (0, kit_1.getAddressDecoder)()],
        ["maxMessageBodySize", (0, kit_1.getU64Decoder)()],
        ["version", (0, kit_1.getU32Decoder)()],
    ]);
}
function getInitializeInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getInitializeInstructionDataEncoder(), getInitializeInstructionDataDecoder());
}
function getInitializeInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.MESSAGE_TRANSMITTER_PROGRAM_ADDRESS;
    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;
    var args = tslib_1.__assign({}, input);
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            "11111111111111111111111111111111";
    }
    var getAccountMeta = (0, shared_1.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,
    });
}
function parseInitializeInstruction(instruction) {
    if (instruction.accounts.length < 8) {
        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