import { encodeFunctionData, } from "viem";
import { ContractType, ENVIRONMENT } from "../types/dimo.js";
import { SEND_DIMO_TOKENS } from "../constants/methods.js";
import { CHAIN_ABI_MAPPING, ENV_MAPPING, ENV_NETWORK_MAPPING } from "../constants/mappings.js";
import { executeTransaction } from "../transactions/execute.js";
import { polygon } from "viem/chains";
export function sendDIMOTokensCallData(args, environment = "prod") {
    var _a;
    const contracts = CHAIN_ABI_MAPPING[(_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.PROD].contracts;
    return encodeFunctionData({
        abi: contracts[ContractType.DIMO_TOKEN].abi,
        functionName: SEND_DIMO_TOKENS,
        args: [args.recipient, args.amount],
    });
}
export const sendDIMOTransaction = async (args, subOrganizationId, walletAddress, passkeyStamper, config) => {
    var _a;
    const _config = config;
    const env = (_a = ENV_MAPPING.get(_config.environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.PROD;
    const contracts = CHAIN_ABI_MAPPING[env].contracts;
    const sendDIMOCallData = sendDIMOTokensCallData(args, config.environment);
    const txData = {
        callType: "call",
        to: contracts[ContractType.DIMO_TOKEN].address,
        value: BigInt("0"),
        data: sendDIMOCallData,
    };
    const resp = await executeTransaction(subOrganizationId, walletAddress, txData, passkeyStamper, _config);
    return resp;
};
export async function sendDIMOTokens(args, client, environment = "prod") {
    var _a;
    const contracts = CHAIN_ABI_MAPPING[(_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.PROD].contracts;
    return await client.account.encodeCallData({
        to: contracts[ContractType.DIMO_TOKEN].address,
        value: BigInt(0),
        data: encodeFunctionData({
            abi: contracts[ContractType.DIMO_TOKEN].abi,
            functionName: SEND_DIMO_TOKENS,
            args: [args.recipient, args.amount],
        }),
    });
}
export async function sendDIMOTokensFromAccount(args, walletClient, publicClient, environment = "prod") {
    var _a;
    const contracts = CHAIN_ABI_MAPPING[(_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.PROD].contracts;
    const { request } = await publicClient.simulateContract({
        address: contracts[ContractType.DIMO_TOKEN].address,
        abi: contracts[ContractType.DIMO_TOKEN].abi,
        functionName: SEND_DIMO_TOKENS,
        args: [args.recipient, args.amount],
        account: walletClient.account,
    });
    const txHash = await walletClient.writeContract(request);
    return txHash;
}
export async function sendDIMOTokensTransactionForSignature(args, publicClient, walletClient, senderAddress, environment = "prod") {
    var _a, _b, _c;
    const chain = (_b = ENV_NETWORK_MAPPING.get((_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.PROD)) !== null && _b !== void 0 ? _b : polygon;
    const contracts = CHAIN_ABI_MAPPING[(_c = ENV_MAPPING.get(environment)) !== null && _c !== void 0 ? _c : ENVIRONMENT.PROD].contracts;
    const gas = await publicClient.estimateFeesPerGas();
    const gaslimit = await publicClient.getGasPrice();
    const gasPrice = await publicClient.estimateContractGas({
        address: contracts[ContractType.DIMO_TOKEN].address,
        abi: contracts[ContractType.DIMO_TOKEN].abi,
        functionName: SEND_DIMO_TOKENS,
        args: [args.recipient, args.amount],
        account: senderAddress,
    });
    const callData = sendDIMOTokensCallData(args, environment);
    const nonce = await publicClient.getTransactionCount({ address: senderAddress });
    const preparedTransaction = await walletClient.prepareTransactionRequest({
        to: contracts[ContractType.DIMO_TOKEN].address,
        nonce: nonce,
        data: callData,
        gas: gasPrice,
        gasLimit: gaslimit,
        maxPriorityFeePerGas: gas.maxPriorityFeePerGas,
        maxFeePerGas: gas.maxFeePerGas,
        chain: chain,
    });
    return preparedTransaction;
}
//# sourceMappingURL=sendDIMOTokens.js.map