/**
 * 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 {
  combineCodec,
  getAddressDecoder,
  getAddressEncoder,
  getArrayDecoder,
  getArrayEncoder,
  getProgramDerivedAddress,
  getStructDecoder,
  getStructEncoder,
  getU64Decoder,
  getU64Encoder,
  getU8Decoder,
  getU8Encoder,
  SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS,
  SolanaError,
  transformEncoder,
  type AccountMeta,
  type AccountSignerMeta,
  type Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlySignerAccount,
  type ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
  type WritableSignerAccount,
} from "@solana/kit";
import {
  getAccountMetaFactory,
  getAddressFromResolvedInstructionAccount,
  type ResolvedInstructionAccount,
} from "@solana/kit/program-client-core";
import {
  findAllowedMintPda,
  findEventAuthorityPda,
  findOperatorPda,
} from "../pdas";
import { PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS } from "../programs";

export const RELEASE_FUNDS_DISCRIMINATOR = 7;

export function getReleaseFundsDiscriminatorBytes(): ReadonlyUint8Array {
  return getU8Encoder().encode(RELEASE_FUNDS_DISCRIMINATOR);
}

export type ReleaseFundsInstruction<
  TProgram extends string =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountOperator extends string | AccountMeta<string> = string,
  TAccountInstance extends string | AccountMeta<string> = string,
  TAccountOperatorPda extends string | AccountMeta<string> = string,
  TAccountMint extends string | AccountMeta<string> = string,
  TAccountAllowedMint extends string | AccountMeta<string> = string,
  TAccountUserAta extends string | AccountMeta<string> = string,
  TAccountInstanceAta extends string | AccountMeta<string> = string,
  TAccountTokenProgram extends string | AccountMeta<string> =
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
  TAccountAssociatedTokenProgram extends string | AccountMeta<string> =
    "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountPrivateChannelEscrowProgram extends string | AccountMeta<string> =
    "9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU",
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountPayer extends string
        ? WritableSignerAccount<TAccountPayer> &
            AccountSignerMeta<TAccountPayer>
        : TAccountPayer,
      TAccountOperator extends string
        ? ReadonlySignerAccount<TAccountOperator> &
            AccountSignerMeta<TAccountOperator>
        : TAccountOperator,
      TAccountInstance extends string
        ? WritableAccount<TAccountInstance>
        : TAccountInstance,
      TAccountOperatorPda extends string
        ? ReadonlyAccount<TAccountOperatorPda>
        : TAccountOperatorPda,
      TAccountMint extends string
        ? ReadonlyAccount<TAccountMint>
        : TAccountMint,
      TAccountAllowedMint extends string
        ? ReadonlyAccount<TAccountAllowedMint>
        : TAccountAllowedMint,
      TAccountUserAta extends string
        ? WritableAccount<TAccountUserAta>
        : TAccountUserAta,
      TAccountInstanceAta extends string
        ? WritableAccount<TAccountInstanceAta>
        : TAccountInstanceAta,
      TAccountTokenProgram extends string
        ? ReadonlyAccount<TAccountTokenProgram>
        : TAccountTokenProgram,
      TAccountAssociatedTokenProgram extends string
        ? ReadonlyAccount<TAccountAssociatedTokenProgram>
        : TAccountAssociatedTokenProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountPrivateChannelEscrowProgram extends string
        ? ReadonlyAccount<TAccountPrivateChannelEscrowProgram>
        : TAccountPrivateChannelEscrowProgram,
      ...TRemainingAccounts,
    ]
  >;

export type ReleaseFundsInstructionData = {
  discriminator: number;
  amount: bigint;
  user: Address;
  newWithdrawalRoot: Array<number>;
  transactionNonce: bigint;
  siblingProofs: Array<number>;
};

export type ReleaseFundsInstructionDataArgs = {
  amount: number | bigint;
  user: Address;
  newWithdrawalRoot: Array<number>;
  transactionNonce: number | bigint;
  siblingProofs: Array<number>;
};

export function getReleaseFundsInstructionDataEncoder(): FixedSizeEncoder<ReleaseFundsInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", getU8Encoder()],
      ["amount", getU64Encoder()],
      ["user", getAddressEncoder()],
      ["newWithdrawalRoot", getArrayEncoder(getU8Encoder(), { size: 32 })],
      ["transactionNonce", getU64Encoder()],
      ["siblingProofs", getArrayEncoder(getU8Encoder(), { size: 512 })],
    ]),
    (value) => ({ ...value, discriminator: RELEASE_FUNDS_DISCRIMINATOR }),
  );
}

export function getReleaseFundsInstructionDataDecoder(): FixedSizeDecoder<ReleaseFundsInstructionData> {
  return getStructDecoder([
    ["discriminator", getU8Decoder()],
    ["amount", getU64Decoder()],
    ["user", getAddressDecoder()],
    ["newWithdrawalRoot", getArrayDecoder(getU8Decoder(), { size: 32 })],
    ["transactionNonce", getU64Decoder()],
    ["siblingProofs", getArrayDecoder(getU8Decoder(), { size: 512 })],
  ]);
}

export function getReleaseFundsInstructionDataCodec(): FixedSizeCodec<
  ReleaseFundsInstructionDataArgs,
  ReleaseFundsInstructionData
> {
  return combineCodec(
    getReleaseFundsInstructionDataEncoder(),
    getReleaseFundsInstructionDataDecoder(),
  );
}

export type ReleaseFundsAsyncInput<
  TAccountPayer extends string = string,
  TAccountOperator extends string = string,
  TAccountInstance extends string = string,
  TAccountOperatorPda extends string = string,
  TAccountMint extends string = string,
  TAccountAllowedMint extends string = string,
  TAccountUserAta extends string = string,
  TAccountInstanceAta extends string = string,
  TAccountTokenProgram extends string = string,
  TAccountAssociatedTokenProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountPrivateChannelEscrowProgram extends string = string,
> = {
  /** Transaction fee payer */
  payer: TransactionSigner<TAccountPayer>;
  /** Operator releasing the funds */
  operator: TransactionSigner<TAccountOperator>;
  /** Instance PDA to validate and update */
  instance: Address<TAccountInstance>;
  /** Operator PDA to validate operator permissions */
  operatorPda?: Address<TAccountOperatorPda>;
  /** Token mint being released */
  mint: Address<TAccountMint>;
  /** AllowedMint PDA to validate mint is allowed */
  allowedMint?: Address<TAccountAllowedMint>;
  /** User's Associated Token Account for this mint */
  userAta: Address<TAccountUserAta>;
  /** Instance's Associated Token Account (escrow) for this mint */
  instanceAta?: Address<TAccountInstanceAta>;
  /** Token program for the mint */
  tokenProgram?: Address<TAccountTokenProgram>;
  /** Associated Token program */
  associatedTokenProgram?: Address<TAccountAssociatedTokenProgram>;
  /** Event authority PDA for emitting events */
  eventAuthority?: Address<TAccountEventAuthority>;
  /** Current program for CPI */
  privateChannelEscrowProgram?: Address<TAccountPrivateChannelEscrowProgram>;
  amount: ReleaseFundsInstructionDataArgs["amount"];
  user: ReleaseFundsInstructionDataArgs["user"];
  newWithdrawalRoot: ReleaseFundsInstructionDataArgs["newWithdrawalRoot"];
  transactionNonce: ReleaseFundsInstructionDataArgs["transactionNonce"];
  siblingProofs: ReleaseFundsInstructionDataArgs["siblingProofs"];
};

export async function getReleaseFundsInstructionAsync<
  TAccountPayer extends string,
  TAccountOperator extends string,
  TAccountInstance extends string,
  TAccountOperatorPda extends string,
  TAccountMint extends string,
  TAccountAllowedMint extends string,
  TAccountUserAta extends string,
  TAccountInstanceAta extends string,
  TAccountTokenProgram extends string,
  TAccountAssociatedTokenProgram extends string,
  TAccountEventAuthority extends string,
  TAccountPrivateChannelEscrowProgram extends string,
  TProgramAddress extends Address =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
>(
  input: ReleaseFundsAsyncInput<
    TAccountPayer,
    TAccountOperator,
    TAccountInstance,
    TAccountOperatorPda,
    TAccountMint,
    TAccountAllowedMint,
    TAccountUserAta,
    TAccountInstanceAta,
    TAccountTokenProgram,
    TAccountAssociatedTokenProgram,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  ReleaseFundsInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountOperator,
    TAccountInstance,
    TAccountOperatorPda,
    TAccountMint,
    TAccountAllowedMint,
    TAccountUserAta,
    TAccountInstanceAta,
    TAccountTokenProgram,
    TAccountAssociatedTokenProgram,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payer: { value: input.payer ?? null, isWritable: true },
    operator: { value: input.operator ?? null, isWritable: false },
    instance: { value: input.instance ?? null, isWritable: true },
    operatorPda: { value: input.operatorPda ?? null, isWritable: false },
    mint: { value: input.mint ?? null, isWritable: false },
    allowedMint: { value: input.allowedMint ?? null, isWritable: false },
    userAta: { value: input.userAta ?? null, isWritable: true },
    instanceAta: { value: input.instanceAta ?? null, isWritable: true },
    tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
    associatedTokenProgram: {
      value: input.associatedTokenProgram ?? null,
      isWritable: false,
    },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    privateChannelEscrowProgram: {
      value: input.privateChannelEscrowProgram ?? null,
      isWritable: false,
    },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedInstructionAccount
  >;

  // Original args.
  const args = { ...input };

  // Resolve default values.
  if (!accounts.operatorPda.value) {
    accounts.operatorPda.value = await findOperatorPda({
      instance: getAddressFromResolvedInstructionAccount(
        "instance",
        accounts.instance.value,
      ),
      wallet: getAddressFromResolvedInstructionAccount(
        "operator",
        accounts.operator.value,
      ),
    });
  }
  if (!accounts.allowedMint.value) {
    accounts.allowedMint.value = await findAllowedMintPda({
      instance: getAddressFromResolvedInstructionAccount(
        "instance",
        accounts.instance.value,
      ),
      mint: getAddressFromResolvedInstructionAccount(
        "mint",
        accounts.mint.value,
      ),
    });
  }
  if (!accounts.tokenProgram.value) {
    accounts.tokenProgram.value =
      "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
  }
  if (!accounts.instanceAta.value) {
    accounts.instanceAta.value = await getProgramDerivedAddress({
      programAddress:
        "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" as Address<"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL">,
      seeds: [
        getAddressEncoder().encode(
          getAddressFromResolvedInstructionAccount(
            "instance",
            accounts.instance.value,
          ),
        ),
        getAddressEncoder().encode(
          getAddressFromResolvedInstructionAccount(
            "tokenProgram",
            accounts.tokenProgram.value,
          ),
        ),
        getAddressEncoder().encode(
          getAddressFromResolvedInstructionAccount("mint", accounts.mint.value),
        ),
      ],
    });
  }
  if (!accounts.associatedTokenProgram.value) {
    accounts.associatedTokenProgram.value =
      "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" as Address<"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL">;
  }
  if (!accounts.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }
  if (!accounts.privateChannelEscrowProgram.value) {
    accounts.privateChannelEscrowProgram.value =
      "9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU" as Address<"9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta("payer", accounts.payer),
      getAccountMeta("operator", accounts.operator),
      getAccountMeta("instance", accounts.instance),
      getAccountMeta("operatorPda", accounts.operatorPda),
      getAccountMeta("mint", accounts.mint),
      getAccountMeta("allowedMint", accounts.allowedMint),
      getAccountMeta("userAta", accounts.userAta),
      getAccountMeta("instanceAta", accounts.instanceAta),
      getAccountMeta("tokenProgram", accounts.tokenProgram),
      getAccountMeta("associatedTokenProgram", accounts.associatedTokenProgram),
      getAccountMeta("eventAuthority", accounts.eventAuthority),
      getAccountMeta(
        "privateChannelEscrowProgram",
        accounts.privateChannelEscrowProgram,
      ),
    ],
    data: getReleaseFundsInstructionDataEncoder().encode(
      args as ReleaseFundsInstructionDataArgs,
    ),
    programAddress,
  } as ReleaseFundsInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountOperator,
    TAccountInstance,
    TAccountOperatorPda,
    TAccountMint,
    TAccountAllowedMint,
    TAccountUserAta,
    TAccountInstanceAta,
    TAccountTokenProgram,
    TAccountAssociatedTokenProgram,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >);
}

export type ReleaseFundsInput<
  TAccountPayer extends string = string,
  TAccountOperator extends string = string,
  TAccountInstance extends string = string,
  TAccountOperatorPda extends string = string,
  TAccountMint extends string = string,
  TAccountAllowedMint extends string = string,
  TAccountUserAta extends string = string,
  TAccountInstanceAta extends string = string,
  TAccountTokenProgram extends string = string,
  TAccountAssociatedTokenProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountPrivateChannelEscrowProgram extends string = string,
> = {
  /** Transaction fee payer */
  payer: TransactionSigner<TAccountPayer>;
  /** Operator releasing the funds */
  operator: TransactionSigner<TAccountOperator>;
  /** Instance PDA to validate and update */
  instance: Address<TAccountInstance>;
  /** Operator PDA to validate operator permissions */
  operatorPda: Address<TAccountOperatorPda>;
  /** Token mint being released */
  mint: Address<TAccountMint>;
  /** AllowedMint PDA to validate mint is allowed */
  allowedMint: Address<TAccountAllowedMint>;
  /** User's Associated Token Account for this mint */
  userAta: Address<TAccountUserAta>;
  /** Instance's Associated Token Account (escrow) for this mint */
  instanceAta: Address<TAccountInstanceAta>;
  /** Token program for the mint */
  tokenProgram?: Address<TAccountTokenProgram>;
  /** Associated Token program */
  associatedTokenProgram?: Address<TAccountAssociatedTokenProgram>;
  /** Event authority PDA for emitting events */
  eventAuthority: Address<TAccountEventAuthority>;
  /** Current program for CPI */
  privateChannelEscrowProgram?: Address<TAccountPrivateChannelEscrowProgram>;
  amount: ReleaseFundsInstructionDataArgs["amount"];
  user: ReleaseFundsInstructionDataArgs["user"];
  newWithdrawalRoot: ReleaseFundsInstructionDataArgs["newWithdrawalRoot"];
  transactionNonce: ReleaseFundsInstructionDataArgs["transactionNonce"];
  siblingProofs: ReleaseFundsInstructionDataArgs["siblingProofs"];
};

export function getReleaseFundsInstruction<
  TAccountPayer extends string,
  TAccountOperator extends string,
  TAccountInstance extends string,
  TAccountOperatorPda extends string,
  TAccountMint extends string,
  TAccountAllowedMint extends string,
  TAccountUserAta extends string,
  TAccountInstanceAta extends string,
  TAccountTokenProgram extends string,
  TAccountAssociatedTokenProgram extends string,
  TAccountEventAuthority extends string,
  TAccountPrivateChannelEscrowProgram extends string,
  TProgramAddress extends Address =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
>(
  input: ReleaseFundsInput<
    TAccountPayer,
    TAccountOperator,
    TAccountInstance,
    TAccountOperatorPda,
    TAccountMint,
    TAccountAllowedMint,
    TAccountUserAta,
    TAccountInstanceAta,
    TAccountTokenProgram,
    TAccountAssociatedTokenProgram,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >,
  config?: { programAddress?: TProgramAddress },
): ReleaseFundsInstruction<
  TProgramAddress,
  TAccountPayer,
  TAccountOperator,
  TAccountInstance,
  TAccountOperatorPda,
  TAccountMint,
  TAccountAllowedMint,
  TAccountUserAta,
  TAccountInstanceAta,
  TAccountTokenProgram,
  TAccountAssociatedTokenProgram,
  TAccountEventAuthority,
  TAccountPrivateChannelEscrowProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    payer: { value: input.payer ?? null, isWritable: true },
    operator: { value: input.operator ?? null, isWritable: false },
    instance: { value: input.instance ?? null, isWritable: true },
    operatorPda: { value: input.operatorPda ?? null, isWritable: false },
    mint: { value: input.mint ?? null, isWritable: false },
    allowedMint: { value: input.allowedMint ?? null, isWritable: false },
    userAta: { value: input.userAta ?? null, isWritable: true },
    instanceAta: { value: input.instanceAta ?? null, isWritable: true },
    tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
    associatedTokenProgram: {
      value: input.associatedTokenProgram ?? null,
      isWritable: false,
    },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    privateChannelEscrowProgram: {
      value: input.privateChannelEscrowProgram ?? null,
      isWritable: false,
    },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedInstructionAccount
  >;

  // Original args.
  const args = { ...input };

  // Resolve default values.
  if (!accounts.tokenProgram.value) {
    accounts.tokenProgram.value =
      "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
  }
  if (!accounts.associatedTokenProgram.value) {
    accounts.associatedTokenProgram.value =
      "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" as Address<"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL">;
  }
  if (!accounts.privateChannelEscrowProgram.value) {
    accounts.privateChannelEscrowProgram.value =
      "9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU" as Address<"9tgHa1DcnaSSUtmMsst8ovKTe1Gfxzezn27KnH9xXYeU">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta("payer", accounts.payer),
      getAccountMeta("operator", accounts.operator),
      getAccountMeta("instance", accounts.instance),
      getAccountMeta("operatorPda", accounts.operatorPda),
      getAccountMeta("mint", accounts.mint),
      getAccountMeta("allowedMint", accounts.allowedMint),
      getAccountMeta("userAta", accounts.userAta),
      getAccountMeta("instanceAta", accounts.instanceAta),
      getAccountMeta("tokenProgram", accounts.tokenProgram),
      getAccountMeta("associatedTokenProgram", accounts.associatedTokenProgram),
      getAccountMeta("eventAuthority", accounts.eventAuthority),
      getAccountMeta(
        "privateChannelEscrowProgram",
        accounts.privateChannelEscrowProgram,
      ),
    ],
    data: getReleaseFundsInstructionDataEncoder().encode(
      args as ReleaseFundsInstructionDataArgs,
    ),
    programAddress,
  } as ReleaseFundsInstruction<
    TProgramAddress,
    TAccountPayer,
    TAccountOperator,
    TAccountInstance,
    TAccountOperatorPda,
    TAccountMint,
    TAccountAllowedMint,
    TAccountUserAta,
    TAccountInstanceAta,
    TAccountTokenProgram,
    TAccountAssociatedTokenProgram,
    TAccountEventAuthority,
    TAccountPrivateChannelEscrowProgram
  >);
}

export type ParsedReleaseFundsInstruction<
  TProgram extends string =
    typeof PRIVATE_CHANNEL_ESCROW_PROGRAM_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    /** Transaction fee payer */
    payer: TAccountMetas[0];
    /** Operator releasing the funds */
    operator: TAccountMetas[1];
    /** Instance PDA to validate and update */
    instance: TAccountMetas[2];
    /** Operator PDA to validate operator permissions */
    operatorPda: TAccountMetas[3];
    /** Token mint being released */
    mint: TAccountMetas[4];
    /** AllowedMint PDA to validate mint is allowed */
    allowedMint: TAccountMetas[5];
    /** User's Associated Token Account for this mint */
    userAta: TAccountMetas[6];
    /** Instance's Associated Token Account (escrow) for this mint */
    instanceAta: TAccountMetas[7];
    /** Token program for the mint */
    tokenProgram: TAccountMetas[8];
    /** Associated Token program */
    associatedTokenProgram: TAccountMetas[9];
    /** Event authority PDA for emitting events */
    eventAuthority: TAccountMetas[10];
    /** Current program for CPI */
    privateChannelEscrowProgram: TAccountMetas[11];
  };
  data: ReleaseFundsInstructionData;
};

export function parseReleaseFundsInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedReleaseFundsInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 12) {
    throw new SolanaError(
      SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS,
      {
        actualAccountMetas: instruction.accounts.length,
        expectedAccountMetas: 12,
      },
    );
  }
  let accountIndex = 0;
  const getNextAccount = () => {
    const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      payer: getNextAccount(),
      operator: getNextAccount(),
      instance: getNextAccount(),
      operatorPda: getNextAccount(),
      mint: getNextAccount(),
      allowedMint: getNextAccount(),
      userAta: getNextAccount(),
      instanceAta: getNextAccount(),
      tokenProgram: getNextAccount(),
      associatedTokenProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      privateChannelEscrowProgram: getNextAccount(),
    },
    data: getReleaseFundsInstructionDataDecoder().decode(instruction.data),
  };
}
