/**
 * This code was AUTOGENERATED using the Codama library.
 * Please DO NOT EDIT THIS FILE, instead use visitors
 * to add features, then rerun Codama to update it.
 *
 * @see https://github.com/codama-idl/codama
 */
import { __assign, __awaiter, __generator } from "tslib";
import { combineCodec, fixDecoderSize, fixEncoderSize, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, transformEncoder, } from "@solana/kit";
import { findCustodyTokenAccountPda, findEventAuthorityPda, findLocalTokenPda, } from "../pdas";
import { TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS } from "../programs";
import { expectAddress, getAccountMetaFactory, } from "../shared";
export var ADD_LOCAL_TOKEN_DISCRIMINATOR = new Uint8Array([
    213, 199, 205, 18, 98, 124, 73, 198,
]);
export function getAddLocalTokenDiscriminatorBytes() {
    return fixEncoderSize(getBytesEncoder(), 8).encode(ADD_LOCAL_TOKEN_DISCRIMINATOR);
}
export function getAddLocalTokenInstructionDataEncoder() {
    return transformEncoder(getStructEncoder([["discriminator", fixEncoderSize(getBytesEncoder(), 8)]]), function (value) { return (__assign(__assign({}, value), { discriminator: ADD_LOCAL_TOKEN_DISCRIMINATOR })); });
}
export function getAddLocalTokenInstructionDataDecoder() {
    return getStructDecoder([
        ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ]);
}
export function getAddLocalTokenInstructionDataCodec() {
    return combineCodec(getAddLocalTokenInstructionDataEncoder(), getAddLocalTokenInstructionDataDecoder());
}
export function getAddLocalTokenInstructionAsync(input, config) {
    return __awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, _a, _b, _c, getAccountMeta;
        var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
        return __generator(this, function (_q) {
            switch (_q.label) {
                case 0:
                    programAddress = (_d = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _d !== void 0 ? _d : TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;
                    originalAccounts = {
                        payer: { value: (_e = input.payer) !== null && _e !== void 0 ? _e : null, isWritable: true },
                        tokenController: {
                            value: (_f = input.tokenController) !== null && _f !== void 0 ? _f : null,
                            isWritable: false,
                        },
                        tokenMinter: { value: (_g = input.tokenMinter) !== null && _g !== void 0 ? _g : null, isWritable: false },
                        localToken: { value: (_h = input.localToken) !== null && _h !== void 0 ? _h : null, isWritable: true },
                        custodyTokenAccount: {
                            value: (_j = input.custodyTokenAccount) !== null && _j !== void 0 ? _j : null,
                            isWritable: true,
                        },
                        localTokenMint: { value: (_k = input.localTokenMint) !== null && _k !== void 0 ? _k : null, isWritable: false },
                        tokenProgram: { value: (_l = input.tokenProgram) !== 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 },
                    };
                    accounts = originalAccounts;
                    if (!!accounts.localToken.value) return [3 /*break*/, 2];
                    _a = accounts.localToken;
                    return [4 /*yield*/, findLocalTokenPda({
                            localTokenMint: expectAddress(accounts.localTokenMint.value),
                        })];
                case 1:
                    _a.value = _q.sent();
                    _q.label = 2;
                case 2:
                    if (!!accounts.custodyTokenAccount.value) return [3 /*break*/, 4];
                    _b = accounts.custodyTokenAccount;
                    return [4 /*yield*/, findCustodyTokenAccountPda({
                            localTokenMint: expectAddress(accounts.localTokenMint.value),
                        })];
                case 3:
                    _b.value = _q.sent();
                    _q.label = 4;
                case 4:
                    if (!accounts.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3 /*break*/, 6];
                    _c = accounts.eventAuthority;
                    return [4 /*yield*/, findEventAuthorityPda()];
                case 5:
                    _c.value = _q.sent();
                    _q.label = 6;
                case 6:
                    getAccountMeta = getAccountMetaFactory(programAddress, "programId");
                    return [2 /*return*/, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.payer),
                                getAccountMeta(accounts.tokenController),
                                getAccountMeta(accounts.tokenMinter),
                                getAccountMeta(accounts.localToken),
                                getAccountMeta(accounts.custodyTokenAccount),
                                getAccountMeta(accounts.localTokenMint),
                                getAccountMeta(accounts.tokenProgram),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getAddLocalTokenInstructionDataEncoder().encode({}),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
export function getAddLocalTokenInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    // Program address.
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;
    // Original accounts.
    var originalAccounts = {
        payer: { value: (_b = input.payer) !== null && _b !== void 0 ? _b : null, isWritable: true },
        tokenController: {
            value: (_c = input.tokenController) !== null && _c !== void 0 ? _c : null,
            isWritable: false,
        },
        tokenMinter: { value: (_d = input.tokenMinter) !== null && _d !== void 0 ? _d : null, isWritable: false },
        localToken: { value: (_e = input.localToken) !== null && _e !== void 0 ? _e : null, isWritable: true },
        custodyTokenAccount: {
            value: (_f = input.custodyTokenAccount) !== null && _f !== void 0 ? _f : null,
            isWritable: true,
        },
        localTokenMint: { value: (_g = input.localTokenMint) !== null && _g !== void 0 ? _g : null, isWritable: false },
        tokenProgram: { value: (_h = input.tokenProgram) !== null && _h !== void 0 ? _h : null, isWritable: false },
        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 },
    };
    var accounts = originalAccounts;
    // Resolve default values.
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            "11111111111111111111111111111111";
    }
    var getAccountMeta = getAccountMetaFactory(programAddress, "programId");
    return Object.freeze({
        accounts: [
            getAccountMeta(accounts.payer),
            getAccountMeta(accounts.tokenController),
            getAccountMeta(accounts.tokenMinter),
            getAccountMeta(accounts.localToken),
            getAccountMeta(accounts.custodyTokenAccount),
            getAccountMeta(accounts.localTokenMint),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getAddLocalTokenInstructionDataEncoder().encode({}),
        programAddress: programAddress,
    });
}
export function parseAddLocalTokenInstruction(instruction) {
    if (instruction.accounts.length < 10) {
        // TODO: Coded error.
        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(),
            tokenController: getNextAccount(),
            tokenMinter: getNextAccount(),
            localToken: getNextAccount(),
            custodyTokenAccount: getNextAccount(),
            localTokenMint: getNextAccount(),
            tokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getAddLocalTokenInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=addLocalToken.js.map