"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UNSAFE_DEPOSIT_DISCRIMINATOR = void 0;
exports.getUnsafeDepositDiscriminatorBytes = getUnsafeDepositDiscriminatorBytes;
exports.getUnsafeDepositInstructionDataEncoder = getUnsafeDepositInstructionDataEncoder;
exports.getUnsafeDepositInstructionDataDecoder = getUnsafeDepositInstructionDataDecoder;
exports.getUnsafeDepositInstructionDataCodec = getUnsafeDepositInstructionDataCodec;
exports.getUnsafeDepositInstructionAsync = getUnsafeDepositInstructionAsync;
exports.getUnsafeDepositInstruction = getUnsafeDepositInstruction;
exports.parseUnsafeDepositInstruction = parseUnsafeDepositInstruction;
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.UNSAFE_DEPOSIT_DISCRIMINATOR = new Uint8Array([
    196, 187, 166, 179, 3, 146, 150, 246,
]);
function getUnsafeDepositDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.UNSAFE_DEPOSIT_DISCRIMINATOR);
}
function getUnsafeDepositInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["depositor", (0, kit_1.getAddressEncoder)()],
        ["recipient", (0, kit_1.getAddressEncoder)()],
        ["inputToken", (0, kit_1.getAddressEncoder)()],
        ["outputToken", (0, kit_1.getAddressEncoder)()],
        ["inputAmount", (0, kit_1.getU64Encoder)()],
        ["outputAmount", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 32)],
        ["destinationChainId", (0, kit_1.getU64Encoder)()],
        ["exclusiveRelayer", (0, kit_1.getAddressEncoder)()],
        ["depositNonce", (0, kit_1.getU64Encoder)()],
        ["quoteTimestamp", (0, kit_1.getU32Encoder)()],
        ["fillDeadline", (0, kit_1.getU32Encoder)()],
        ["exclusivityParameter", (0, kit_1.getU32Encoder)()],
        ["message", (0, kit_1.addEncoderSizePrefix)((0, kit_1.getBytesEncoder)(), (0, kit_1.getU32Encoder)())],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.UNSAFE_DEPOSIT_DISCRIMINATOR })); });
}
function getUnsafeDepositInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["depositor", (0, kit_1.getAddressDecoder)()],
        ["recipient", (0, kit_1.getAddressDecoder)()],
        ["inputToken", (0, kit_1.getAddressDecoder)()],
        ["outputToken", (0, kit_1.getAddressDecoder)()],
        ["inputAmount", (0, kit_1.getU64Decoder)()],
        ["outputAmount", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 32)],
        ["destinationChainId", (0, kit_1.getU64Decoder)()],
        ["exclusiveRelayer", (0, kit_1.getAddressDecoder)()],
        ["depositNonce", (0, kit_1.getU64Decoder)()],
        ["quoteTimestamp", (0, kit_1.getU32Decoder)()],
        ["fillDeadline", (0, kit_1.getU32Decoder)()],
        ["exclusivityParameter", (0, kit_1.getU32Decoder)()],
        ["message", (0, kit_1.addDecoderSizePrefix)((0, kit_1.getBytesDecoder)(), (0, kit_1.getU32Decoder)())],
    ]);
}
function getUnsafeDepositInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getUnsafeDepositInstructionDataEncoder(), getUnsafeDepositInstructionDataDecoder());
}
function getUnsafeDepositInstructionAsync(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, _m, _o, _p, _q;
        return tslib_1.__generator(this, function (_r) {
            switch (_r.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 },
                        state: { value: (_f = input.state) !== null && _f !== void 0 ? _f : null, isWritable: true },
                        delegate: { value: (_g = input.delegate) !== null && _g !== void 0 ? _g : null, isWritable: false },
                        depositorTokenAccount: {
                            value: (_h = input.depositorTokenAccount) !== null && _h !== void 0 ? _h : null,
                            isWritable: true,
                        },
                        vault: { value: (_j = input.vault) !== null && _j !== void 0 ? _j : null, isWritable: true },
                        mint: { value: (_k = input.mint) !== null && _k !== void 0 ? _k : null, isWritable: false },
                        tokenProgram: { value: (_l = input.tokenProgram) !== null && _l !== void 0 ? _l : null, isWritable: false },
                        associatedTokenProgram: {
                            value: (_m = input.associatedTokenProgram) !== null && _m !== void 0 ? _m : null,
                            isWritable: false,
                        },
                        systemProgram: { value: (_o = input.systemProgram) !== null && _o !== void 0 ? _o : null, isWritable: false },
                        eventAuthority: { value: (_p = input.eventAuthority) !== null && _p !== void 0 ? _p : null, isWritable: false },
                        program: { value: (_q = input.program) !== null && _q !== void 0 ? _q : null, isWritable: false },
                    };
                    accounts = originalAccounts;
                    args = tslib_1.__assign({}, input);
                    if (!accounts.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!!accounts.depositorTokenAccount.value) return [3, 2];
                    _a = accounts.depositorTokenAccount;
                    return [4, (0, kit_1.getProgramDerivedAddress)({
                            programAddress: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
                            seeds: [
                                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectSome)(args.depositor)),
                                (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 1:
                    _a.value = _r.sent();
                    _r.label = 2;
                case 2:
                    if (!!accounts.vault.value) return [3, 4];
                    _b = accounts.vault;
                    return [4, (0, kit_1.getProgramDerivedAddress)({
                            programAddress: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
                            seeds: [
                                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.state.value)),
                                (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 = _r.sent();
                    _r.label = 4;
                case 4:
                    if (!accounts.associatedTokenProgram.value) {
                        accounts.associatedTokenProgram.value =
                            "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
                    }
                    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 = _r.sent();
                    _r.label = 6;
                case 6:
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.signer),
                                getAccountMeta(accounts.state),
                                getAccountMeta(accounts.delegate),
                                getAccountMeta(accounts.depositorTokenAccount),
                                getAccountMeta(accounts.vault),
                                getAccountMeta(accounts.mint),
                                getAccountMeta(accounts.tokenProgram),
                                getAccountMeta(accounts.associatedTokenProgram),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getUnsafeDepositInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getUnsafeDepositInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    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 },
        state: { value: (_c = input.state) !== null && _c !== void 0 ? _c : null, isWritable: true },
        delegate: { value: (_d = input.delegate) !== null && _d !== void 0 ? _d : null, isWritable: false },
        depositorTokenAccount: {
            value: (_e = input.depositorTokenAccount) !== null && _e !== void 0 ? _e : null,
            isWritable: true,
        },
        vault: { value: (_f = input.vault) !== null && _f !== void 0 ? _f : null, isWritable: true },
        mint: { value: (_g = input.mint) !== null && _g !== void 0 ? _g : null, isWritable: false },
        tokenProgram: { value: (_h = input.tokenProgram) !== null && _h !== void 0 ? _h : null, isWritable: false },
        associatedTokenProgram: {
            value: (_j = input.associatedTokenProgram) !== null && _j !== void 0 ? _j : null,
            isWritable: false,
        },
        systemProgram: { value: (_k = input.systemProgram) !== null && _k !== void 0 ? _k : null, isWritable: false },
        eventAuthority: { value: (_l = input.eventAuthority) !== null && _l !== void 0 ? _l : null, isWritable: false },
        program: { value: (_m = input.program) !== null && _m !== void 0 ? _m : 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.state),
            getAccountMeta(accounts.delegate),
            getAccountMeta(accounts.depositorTokenAccount),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.associatedTokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getUnsafeDepositInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseUnsafeDepositInstruction(instruction) {
    if (instruction.accounts.length < 11) {
        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(),
            state: getNextAccount(),
            delegate: getNextAccount(),
            depositorTokenAccount: getNextAccount(),
            vault: getNextAccount(),
            mint: getNextAccount(),
            tokenProgram: getNextAccount(),
            associatedTokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getUnsafeDepositInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=unsafeDeposit.js.map