"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FILL_RELAY_DISCRIMINATOR = void 0;
exports.getFillRelayDiscriminatorBytes = getFillRelayDiscriminatorBytes;
exports.getFillRelayInstructionDataEncoder = getFillRelayInstructionDataEncoder;
exports.getFillRelayInstructionDataDecoder = getFillRelayInstructionDataDecoder;
exports.getFillRelayInstructionDataCodec = getFillRelayInstructionDataCodec;
exports.getFillRelayInstructionAsync = getFillRelayInstructionAsync;
exports.getFillRelayInstruction = getFillRelayInstruction;
exports.parseFillRelayInstruction = parseFillRelayInstruction;
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");
var types_1 = require("../types");
exports.FILL_RELAY_DISCRIMINATOR = new Uint8Array([
    100, 84, 222, 90, 106, 209, 58, 222,
]);
function getFillRelayDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.FILL_RELAY_DISCRIMINATOR);
}
function getFillRelayInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["relayHash", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 32)],
        ["relayData", (0, kit_1.getOptionEncoder)((0, types_1.getRelayDataEncoder)())],
        ["repaymentChainId", (0, kit_1.getOptionEncoder)((0, kit_1.getU64Encoder)())],
        ["repaymentAddress", (0, kit_1.getOptionEncoder)((0, kit_1.getAddressEncoder)())],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.FILL_RELAY_DISCRIMINATOR })); });
}
function getFillRelayInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["relayHash", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 32)],
        ["relayData", (0, kit_1.getOptionDecoder)((0, types_1.getRelayDataDecoder)())],
        ["repaymentChainId", (0, kit_1.getOptionDecoder)((0, kit_1.getU64Decoder)())],
        ["repaymentAddress", (0, kit_1.getOptionDecoder)((0, kit_1.getAddressDecoder)())],
    ]);
}
function getFillRelayInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getFillRelayInstructionDataEncoder(), getFillRelayInstructionDataDecoder());
}
function getFillRelayInstructionAsync(input, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, _b, _c, _d, getAccountMeta;
        var _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
        return tslib_1.__generator(this, function (_u) {
            switch (_u.label) {
                case 0:
                    programAddress = (_e = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _e !== void 0 ? _e : programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
                    originalAccounts = {
                        signer: { value: (_f = input.signer) !== null && _f !== void 0 ? _f : null, isWritable: true },
                        instructionParams: {
                            value: (_g = input.instructionParams) !== null && _g !== void 0 ? _g : null,
                            isWritable: true,
                        },
                        state: { value: (_h = input.state) !== null && _h !== void 0 ? _h : null, isWritable: false },
                        delegate: { value: (_j = input.delegate) !== null && _j !== void 0 ? _j : null, isWritable: false },
                        mint: { value: (_k = input.mint) !== null && _k !== void 0 ? _k : null, isWritable: false },
                        relayerTokenAccount: {
                            value: (_l = input.relayerTokenAccount) !== null && _l !== void 0 ? _l : null,
                            isWritable: true,
                        },
                        recipientTokenAccount: {
                            value: (_m = input.recipientTokenAccount) !== null && _m !== void 0 ? _m : null,
                            isWritable: true,
                        },
                        fillStatus: { value: (_o = input.fillStatus) !== null && _o !== void 0 ? _o : null, isWritable: true },
                        tokenProgram: { value: (_p = input.tokenProgram) !== null && _p !== void 0 ? _p : null, isWritable: false },
                        associatedTokenProgram: {
                            value: (_q = input.associatedTokenProgram) !== null && _q !== void 0 ? _q : null,
                            isWritable: false,
                        },
                        systemProgram: { value: (_r = input.systemProgram) !== null && _r !== void 0 ? _r : null, isWritable: false },
                        eventAuthority: { value: (_s = input.eventAuthority) !== null && _s !== void 0 ? _s : null, isWritable: false },
                        program: { value: (_t = input.program) !== null && _t !== void 0 ? _t : null, isWritable: false },
                    };
                    accounts = originalAccounts;
                    args = tslib_1.__assign({}, input);
                    if (!!accounts.instructionParams.value) return [3, 2];
                    _a = accounts.instructionParams;
                    return [4, (0, pdas_1.findInstructionParamsPda)({
                            signer: (0, shared_1.expectAddress)(accounts.signer.value),
                        })];
                case 1:
                    _a.value = _u.sent();
                    _u.label = 2;
                case 2:
                    if (!accounts.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!!accounts.recipientTokenAccount.value) return [3, 4];
                    _b = accounts.recipientTokenAccount;
                    return [4, (0, kit_1.getProgramDerivedAddress)({
                            programAddress: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
                            seeds: [
                                (0, kit_1.getOptionEncoder)((0, types_1.getRelayDataEncoder)()).encode((0, shared_1.expectSome)(args.relayData)),
                                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.tokenProgram.value)),
                                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.mint.value)),
                            ],
                        })];
                case 3:
                    _b.value = _u.sent();
                    _u.label = 4;
                case 4:
                    if (!!accounts.fillStatus.value) return [3, 6];
                    _c = accounts.fillStatus;
                    return [4, (0, pdas_1.findFillStatusPda)({
                            relayHash: (0, shared_1.expectSome)(args.relayHash),
                        })];
                case 5:
                    _c.value = _u.sent();
                    _u.label = 6;
                case 6:
                    if (!accounts.associatedTokenProgram.value) {
                        accounts.associatedTokenProgram.value =
                            "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
                    }
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3, 8];
                    _d = accounts.eventAuthority;
                    return [4, (0, pdas_1.findEventAuthorityPda)()];
                case 7:
                    _d.value = _u.sent();
                    _u.label = 8;
                case 8:
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.signer),
                                getAccountMeta(accounts.instructionParams),
                                getAccountMeta(accounts.state),
                                getAccountMeta(accounts.delegate),
                                getAccountMeta(accounts.mint),
                                getAccountMeta(accounts.relayerTokenAccount),
                                getAccountMeta(accounts.recipientTokenAccount),
                                getAccountMeta(accounts.fillStatus),
                                getAccountMeta(accounts.tokenProgram),
                                getAccountMeta(accounts.associatedTokenProgram),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getFillRelayInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getFillRelayInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
    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: true },
        instructionParams: {
            value: (_c = input.instructionParams) !== null && _c !== void 0 ? _c : null,
            isWritable: true,
        },
        state: { value: (_d = input.state) !== null && _d !== void 0 ? _d : null, isWritable: false },
        delegate: { value: (_e = input.delegate) !== null && _e !== void 0 ? _e : null, isWritable: false },
        mint: { value: (_f = input.mint) !== null && _f !== void 0 ? _f : null, isWritable: false },
        relayerTokenAccount: {
            value: (_g = input.relayerTokenAccount) !== null && _g !== void 0 ? _g : null,
            isWritable: true,
        },
        recipientTokenAccount: {
            value: (_h = input.recipientTokenAccount) !== null && _h !== void 0 ? _h : null,
            isWritable: true,
        },
        fillStatus: { value: (_j = input.fillStatus) !== null && _j !== void 0 ? _j : null, isWritable: true },
        tokenProgram: { value: (_k = input.tokenProgram) !== null && _k !== void 0 ? _k : null, isWritable: false },
        associatedTokenProgram: {
            value: (_l = input.associatedTokenProgram) !== null && _l !== void 0 ? _l : null,
            isWritable: false,
        },
        systemProgram: { value: (_m = input.systemProgram) !== null && _m !== void 0 ? _m : null, isWritable: false },
        eventAuthority: { value: (_o = input.eventAuthority) !== null && _o !== void 0 ? _o : null, isWritable: false },
        program: { value: (_p = input.program) !== null && _p !== void 0 ? _p : null, isWritable: false },
    };
    var accounts = originalAccounts;
    var args = tslib_1.__assign({}, input);
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
    }
    if (!accounts.associatedTokenProgram.value) {
        accounts.associatedTokenProgram.value =
            "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
    }
    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.instructionParams),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.delegate),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.relayerTokenAccount),
            getAccountMeta(accounts.recipientTokenAccount),
            getAccountMeta(accounts.fillStatus),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.associatedTokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getFillRelayInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseFillRelayInstruction(instruction) {
    if (instruction.accounts.length < 13) {
        throw new Error("Not enough accounts");
    }
    var accountIndex = 0;
    var getNextAccount = function () {
        var accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    var getNextOptionalAccount = function () {
        var accountMeta = getNextAccount();
        return accountMeta.address === programs_1.SVM_SPOKE_PROGRAM_ADDRESS
            ? undefined
            : accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: {
            signer: getNextAccount(),
            instructionParams: getNextOptionalAccount(),
            state: getNextAccount(),
            delegate: getNextAccount(),
            mint: getNextAccount(),
            relayerTokenAccount: getNextAccount(),
            recipientTokenAccount: getNextAccount(),
            fillStatus: getNextAccount(),
            tokenProgram: getNextAccount(),
            associatedTokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getFillRelayInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=fillRelay.js.map