"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ADD_LOCAL_TOKEN_DISCRIMINATOR = void 0;
exports.getAddLocalTokenDiscriminatorBytes = getAddLocalTokenDiscriminatorBytes;
exports.getAddLocalTokenInstructionDataEncoder = getAddLocalTokenInstructionDataEncoder;
exports.getAddLocalTokenInstructionDataDecoder = getAddLocalTokenInstructionDataDecoder;
exports.getAddLocalTokenInstructionDataCodec = getAddLocalTokenInstructionDataCodec;
exports.getAddLocalTokenInstructionAsync = getAddLocalTokenInstructionAsync;
exports.getAddLocalTokenInstruction = getAddLocalTokenInstruction;
exports.parseAddLocalTokenInstruction = parseAddLocalTokenInstruction;
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.ADD_LOCAL_TOKEN_DISCRIMINATOR = new Uint8Array([
    213, 199, 205, 18, 98, 124, 73, 198,
]);
function getAddLocalTokenDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.ADD_LOCAL_TOKEN_DISCRIMINATOR);
}
function getAddLocalTokenInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)]]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.ADD_LOCAL_TOKEN_DISCRIMINATOR })); });
}
function getAddLocalTokenInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
    ]);
}
function getAddLocalTokenInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getAddLocalTokenInstructionDataEncoder(), getAddLocalTokenInstructionDataDecoder());
}
function getAddLocalTokenInstructionAsync(input, config) {
    return tslib_1.__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 tslib_1.__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 : programs_1.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, 2];
                    _a = accounts.localToken;
                    return [4, (0, pdas_1.findLocalTokenPda)({
                            localTokenMint: (0, shared_1.expectAddress)(accounts.localTokenMint.value),
                        })];
                case 1:
                    _a.value = _q.sent();
                    _q.label = 2;
                case 2:
                    if (!!accounts.custodyTokenAccount.value) return [3, 4];
                    _b = accounts.custodyTokenAccount;
                    return [4, (0, pdas_1.findCustodyTokenAccountPda)({
                            localTokenMint: (0, shared_1.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, 6];
                    _c = accounts.eventAuthority;
                    return [4, (0, pdas_1.findEventAuthorityPda)()];
                case 5:
                    _c.value = _q.sent();
                    _q.label = 6;
                case 6:
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, 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,
                        })];
            }
        });
    });
}
function getAddLocalTokenInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;
    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;
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            "11111111111111111111111111111111";
    }
    var getAccountMeta = (0, shared_1.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,
    });
}
function parseAddLocalTokenInstruction(instruction) {
    if (instruction.accounts.length < 10) {
        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