"use client";

// src/clients/nado.tsx
import {
  concat,
  encodeAbiParameters,
  erc20Abi,
  getAddress,
  maxUint256,
  pad,
  stringToHex,
  toFunctionSelector
} from "viem";
var NADO_ENDPOINT_ADDRESS = getAddress(
  "0x05ec92d78ed421f3d3ada77ffde167106565974e"
);
var INK_VAULT_DEPOSITOR_ADDRESS = getAddress(
  "0xDB9d579B0DCbf566E372C73A2e374c44B161E1Ee"
);
var AMOUNT_PLACEHOLDER = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffdeadbeefn;
var VAULT_DEPOSITOR_ABI = [
  {
    name: "deposit",
    type: "function",
    stateMutability: "nonpayable",
    inputs: [
      { name: "token", type: "address" },
      { name: "vault", type: "address" },
      { name: "callData", type: "bytes" },
      { name: "minAmountOut", type: "uint256" }
    ],
    outputs: []
  }
];
var NADO_NO_REFERRAL_CODE = "-1";
var NADO_DEFAULT_SUBACCOUNT_NAME = "default";
var NADO_SUBACCOUNT_NAME_MAX_BYTES = 12;
function buildNadoSubaccount(owner, name = NADO_DEFAULT_SUBACCOUNT_NAME) {
  const nameHex = stringToHex(name);
  const nameByteLength = (nameHex.length - 2) / 2;
  if (nameByteLength > NADO_SUBACCOUNT_NAME_MAX_BYTES) {
    throw new Error(
      `Nado subaccount name must encode to \u2264 ${NADO_SUBACCOUNT_NAME_MAX_BYTES} bytes (got ${nameByteLength}: "${name}")`
    );
  }
  return concat([
    owner,
    pad(nameHex, { dir: "right", size: NADO_SUBACCOUNT_NAME_MAX_BYTES })
  ]);
}
function createNadoDepositActions(config) {
  const {
    recipientAddress,
    collateralTokenAddress,
    productId,
    subaccountName,
    referralCode = NADO_NO_REFERRAL_CODE
  } = config;
  if (!recipientAddress) {
    throw new Error("Please connect your wallet before starting a Nado deposit");
  }
  const collateral = getAddress(collateralTokenAddress);
  const subaccount = buildNadoSubaccount(recipientAddress, subaccountName);
  const depositCalldata = concat([
    toFunctionSelector(
      "depositCollateralWithReferral(bytes32,uint32,uint128,string)"
    ),
    encodeAbiParameters(
      [
        { type: "bytes32" },
        { type: "uint32" },
        { type: "uint256" },
        { type: "string" }
      ],
      [subaccount, productId, AMOUNT_PLACEHOLDER, referralCode]
    )
  ]);
  return [
    // MAX approve so the pull covers whatever balance actually arrived (the
    // amount is only known at execution); the spender is Fun's own audited
    // VaultDepositooor. No zero-first reset needed: Ink USDT0 uses plain OZ
    // approve (verified), unlike legacy mainnet USDT.
    {
      contractAbi: erc20Abi,
      contractAddress: collateral,
      functionName: "approve",
      functionArgs: [INK_VAULT_DEPOSITOR_ADDRESS, maxUint256]
    },
    // VaultDepositooor pulls the live balance, injects it over the
    // placeholder, and calls the Endpoint. minAmountOut is 0 because the
    // collateral passes through 1:1 (no vault rate to bound).
    {
      contractAbi: VAULT_DEPOSITOR_ABI,
      contractAddress: INK_VAULT_DEPOSITOR_ADDRESS,
      functionName: "deposit",
      functionArgs: [collateral, NADO_ENDPOINT_ADDRESS, depositCalldata, 0n]
    }
  ];
}
export {
  INK_VAULT_DEPOSITOR_ADDRESS,
  NADO_DEFAULT_SUBACCOUNT_NAME,
  NADO_ENDPOINT_ADDRESS,
  NADO_NO_REFERRAL_CODE,
  buildNadoSubaccount,
  createNadoDepositActions
};
