"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.REQUEST_SLOW_FILL_DISCRIMINATOR = void 0;
exports.getRequestSlowFillDiscriminatorBytes = getRequestSlowFillDiscriminatorBytes;
exports.getRequestSlowFillInstructionDataEncoder = getRequestSlowFillInstructionDataEncoder;
exports.getRequestSlowFillInstructionDataDecoder = getRequestSlowFillInstructionDataDecoder;
exports.getRequestSlowFillInstructionDataCodec = getRequestSlowFillInstructionDataCodec;
exports.getRequestSlowFillInstructionAsync = getRequestSlowFillInstructionAsync;
exports.getRequestSlowFillInstruction = getRequestSlowFillInstruction;
exports.parseRequestSlowFillInstruction = parseRequestSlowFillInstruction;
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.REQUEST_SLOW_FILL_DISCRIMINATOR = new Uint8Array([
    39, 157, 165, 187, 88, 217, 207, 98,
]);
function getRequestSlowFillDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.REQUEST_SLOW_FILL_DISCRIMINATOR);
}
function getRequestSlowFillInstructionDataEncoder() {
    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)())],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.REQUEST_SLOW_FILL_DISCRIMINATOR })); });
}
function getRequestSlowFillInstructionDataDecoder() {
    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)())],
    ]);
}
function getRequestSlowFillInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getRequestSlowFillInstructionDataEncoder(), getRequestSlowFillInstructionDataDecoder());
}
function getRequestSlowFillInstructionAsync(input, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, _b, _c, getAccountMeta;
        var _d, _e, _f, _g, _h, _j, _k, _l;
        return tslib_1.__generator(this, function (_m) {
            switch (_m.label) {
                case 0:
                    programAddress = (_d = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _d !== void 0 ? _d : programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
                    originalAccounts = {
                        signer: { value: (_e = input.signer) !== null && _e !== void 0 ? _e : null, isWritable: true },
                        instructionParams: {
                            value: (_f = input.instructionParams) !== null && _f !== void 0 ? _f : null,
                            isWritable: true,
                        },
                        state: { value: (_g = input.state) !== null && _g !== void 0 ? _g : null, isWritable: false },
                        fillStatus: { value: (_h = input.fillStatus) !== null && _h !== void 0 ? _h : null, isWritable: true },
                        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 = 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 = _m.sent();
                    _m.label = 2;
                case 2:
                    if (!!accounts.fillStatus.value) return [3, 4];
                    _b = accounts.fillStatus;
                    return [4, (0, pdas_1.findFillStatusPda)({
                            relayHash: (0, shared_1.expectSome)(args.relayHash),
                        })];
                case 3:
                    _b.value = _m.sent();
                    _m.label = 4;
                case 4:
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3, 6];
                    _c = accounts.eventAuthority;
                    return [4, (0, pdas_1.findEventAuthorityPda)()];
                case 5:
                    _c.value = _m.sent();
                    _m.label = 6;
                case 6:
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.signer),
                                getAccountMeta(accounts.instructionParams),
                                getAccountMeta(accounts.state),
                                getAccountMeta(accounts.fillStatus),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getRequestSlowFillInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getRequestSlowFillInstruction(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: 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 },
        fillStatus: { value: (_e = input.fillStatus) !== 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.instructionParams),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.fillStatus),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getRequestSlowFillInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseRequestSlowFillInstruction(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;
    };
    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(),
            fillStatus: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getRequestSlowFillInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=requestSlowFill.js.map