"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RELAY_ROOT_BUNDLE_DISCRIMINATOR = void 0;
exports.getRelayRootBundleDiscriminatorBytes = getRelayRootBundleDiscriminatorBytes;
exports.getRelayRootBundleInstructionDataEncoder = getRelayRootBundleInstructionDataEncoder;
exports.getRelayRootBundleInstructionDataDecoder = getRelayRootBundleInstructionDataDecoder;
exports.getRelayRootBundleInstructionDataCodec = getRelayRootBundleInstructionDataCodec;
exports.getRelayRootBundleInstructionAsync = getRelayRootBundleInstructionAsync;
exports.getRelayRootBundleInstruction = getRelayRootBundleInstruction;
exports.parseRelayRootBundleInstruction = parseRelayRootBundleInstruction;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
var pdas_1 = require("../pdas");
var programs_1 = require("../programs");
var shared_1 = require("../shared");
exports.RELAY_ROOT_BUNDLE_DISCRIMINATOR = new Uint8Array([
    69, 13, 223, 204, 251, 61, 105, 6,
]);
function getRelayRootBundleDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.RELAY_ROOT_BUNDLE_DISCRIMINATOR);
}
function getRelayRootBundleInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["relayerRefundRoot", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 32)],
        ["slowRelayRoot", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 32)],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.RELAY_ROOT_BUNDLE_DISCRIMINATOR })); });
}
function getRelayRootBundleInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["relayerRefundRoot", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 32)],
        ["slowRelayRoot", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 32)],
    ]);
}
function getRelayRootBundleInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getRelayRootBundleInstructionDataEncoder(), getRelayRootBundleInstructionDataDecoder());
}
function getRelayRootBundleInstructionAsync(input, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, getAccountMeta;
        var _b, _c, _d, _e, _f, _g, _h, _j;
        return tslib_1.__generator(this, function (_k) {
            switch (_k.label) {
                case 0:
                    programAddress = (_b = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _b !== void 0 ? _b : programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
                    originalAccounts = {
                        signer: { value: (_c = input.signer) !== null && _c !== void 0 ? _c : null, isWritable: false },
                        payer: { value: (_d = input.payer) !== null && _d !== void 0 ? _d : null, isWritable: true },
                        state: { value: (_e = input.state) !== null && _e !== void 0 ? _e : null, isWritable: true },
                        rootBundle: { value: (_f = input.rootBundle) !== null && _f !== void 0 ? _f : null, isWritable: true },
                        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 },
                    };
                    accounts = originalAccounts;
                    args = tslib_1.__assign({}, input);
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3, 2];
                    _a = accounts.eventAuthority;
                    return [4, (0, pdas_1.findEventAuthorityPda)()];
                case 1:
                    _a.value = _k.sent();
                    _k.label = 2;
                case 2:
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.signer),
                                getAccountMeta(accounts.payer),
                                getAccountMeta(accounts.state),
                                getAccountMeta(accounts.rootBundle),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getRelayRootBundleInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getRelayRootBundleInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    var originalAccounts = {
        signer: { value: (_b = input.signer) !== null && _b !== void 0 ? _b : null, isWritable: false },
        payer: { value: (_c = input.payer) !== null && _c !== void 0 ? _c : null, isWritable: true },
        state: { value: (_d = input.state) !== null && _d !== void 0 ? _d : null, isWritable: true },
        rootBundle: { value: (_e = input.rootBundle) !== null && _e !== void 0 ? _e : null, isWritable: true },
        systemProgram: { value: (_f = input.systemProgram) !== null && _f !== void 0 ? _f : null, isWritable: false },
        eventAuthority: { value: (_g = input.eventAuthority) !== null && _g !== void 0 ? _g : null, isWritable: false },
        program: { value: (_h = input.program) !== null && _h !== void 0 ? _h : 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.signer),
            getAccountMeta(accounts.payer),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.rootBundle),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getRelayRootBundleInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseRelayRootBundleInstruction(instruction) {
    if (instruction.accounts.length < 7) {
        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: {
            signer: getNextAccount(),
            payer: getNextAccount(),
            state: getNextAccount(),
            rootBundle: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getRelayRootBundleInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=relayRootBundle.js.map