"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WRITE_INSTRUCTION_PARAMS_FRAGMENT_DISCRIMINATOR = void 0;
exports.getWriteInstructionParamsFragmentDiscriminatorBytes = getWriteInstructionParamsFragmentDiscriminatorBytes;
exports.getWriteInstructionParamsFragmentInstructionDataEncoder = getWriteInstructionParamsFragmentInstructionDataEncoder;
exports.getWriteInstructionParamsFragmentInstructionDataDecoder = getWriteInstructionParamsFragmentInstructionDataDecoder;
exports.getWriteInstructionParamsFragmentInstructionDataCodec = getWriteInstructionParamsFragmentInstructionDataCodec;
exports.getWriteInstructionParamsFragmentInstructionAsync = getWriteInstructionParamsFragmentInstructionAsync;
exports.getWriteInstructionParamsFragmentInstruction = getWriteInstructionParamsFragmentInstruction;
exports.parseWriteInstructionParamsFragmentInstruction = parseWriteInstructionParamsFragmentInstruction;
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.WRITE_INSTRUCTION_PARAMS_FRAGMENT_DISCRIMINATOR = new Uint8Array([
    238, 182, 109, 113, 124, 255, 72, 18,
]);
function getWriteInstructionParamsFragmentDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.WRITE_INSTRUCTION_PARAMS_FRAGMENT_DISCRIMINATOR);
}
function getWriteInstructionParamsFragmentInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["offset", (0, kit_1.getU32Encoder)()],
        ["fragment", (0, kit_1.addEncoderSizePrefix)((0, kit_1.getBytesEncoder)(), (0, kit_1.getU32Encoder)())],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.WRITE_INSTRUCTION_PARAMS_FRAGMENT_DISCRIMINATOR })); });
}
function getWriteInstructionParamsFragmentInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["offset", (0, kit_1.getU32Decoder)()],
        ["fragment", (0, kit_1.addDecoderSizePrefix)((0, kit_1.getBytesDecoder)(), (0, kit_1.getU32Decoder)())],
    ]);
}
function getWriteInstructionParamsFragmentInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getWriteInstructionParamsFragmentInstructionDataEncoder(), getWriteInstructionParamsFragmentInstructionDataDecoder());
}
function getWriteInstructionParamsFragmentInstructionAsync(input, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, getAccountMeta;
        var _b, _c, _d, _e;
        return tslib_1.__generator(this, function (_f) {
            switch (_f.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 },
                        instructionParams: {
                            value: (_d = input.instructionParams) !== null && _d !== void 0 ? _d : null,
                            isWritable: true,
                        },
                        systemProgram: { value: (_e = input.systemProgram) !== null && _e !== void 0 ? _e : 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 = _f.sent();
                    _f.label = 2;
                case 2:
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.signer),
                                getAccountMeta(accounts.instructionParams),
                                getAccountMeta(accounts.systemProgram),
                            ],
                            data: getWriteInstructionParamsFragmentInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getWriteInstructionParamsFragmentInstruction(input, config) {
    var _a, _b, _c, _d;
    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 },
        instructionParams: {
            value: (_c = input.instructionParams) !== null && _c !== void 0 ? _c : null,
            isWritable: true,
        },
        systemProgram: { value: (_d = input.systemProgram) !== null && _d !== void 0 ? _d : 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.systemProgram),
        ],
        data: getWriteInstructionParamsFragmentInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseWriteInstructionParamsFragmentInstruction(instruction) {
    if (instruction.accounts.length < 3) {
        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(),
            instructionParams: getNextAccount(),
            systemProgram: getNextAccount(),
        },
        data: getWriteInstructionParamsFragmentInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=writeInstructionParamsFragment.js.map