import { encodeFunctionData, } from "viem";
import { ContractType, ENVIRONMENT } from "../types/dimo.js";
import { CHAIN_ABI_MAPPING, ENV_MAPPING, ENV_NETWORK_MAPPING } from "../constants/mappings.js";
import { TRANSFER_VEHICLE_AND_AFTERMARKET_DEVICE_IDS } from "../constants/methods.js";
import { executeTransaction } from "../transactions/execute.js";
import { polygon } from "viem/chains";
import { DIMODomain, DIMODomainVersion } from "../constants/dimo.js";
import { ethers } from "ethers";
export function transferVehicleAndAftermarketDeviceIDsCallData(args, environment = "prod") {
    var _a;
    const contracts = CHAIN_ABI_MAPPING[(_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.DEV].contracts;
    return encodeFunctionData({
        abi: contracts[ContractType.DIMO_FORWARDER].abi,
        functionName: TRANSFER_VEHICLE_AND_AFTERMARKET_DEVICE_IDS,
        args: [args.vehicleIds, args.aftermarketDeviceIds, args.to],
    });
}
export const transferVehicleAndAftermarketDeviceIDsTransaction = async (args, subOrganizationId, walletAddress, passkeyStamper, config) => {
    var _a, _b;
    const env = (_b = ENV_MAPPING.get((_a = config.environment) !== null && _a !== void 0 ? _a : "prod")) !== null && _b !== void 0 ? _b : ENVIRONMENT.PROD;
    const contracts = CHAIN_ABI_MAPPING[env].contracts;
    const txData = {
        callType: "call",
        to: contracts[ContractType.DIMO_FORWARDER].address,
        value: BigInt("0"),
        data: transferVehicleAndAftermarketDeviceIDsCallData(args, config.environment),
    };
    const resp = await executeTransaction(subOrganizationId, walletAddress, txData, passkeyStamper, config);
    return resp;
};
export const transferVehicleAndAftermarketDeviceIDs = async (args, client, environment = "prod") => {
    var _a;
    const contracts = CHAIN_ABI_MAPPING[(_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.DEV].contracts;
    return await client.account.encodeCallData({
        to: contracts[ContractType.DIMO_FORWARDER].address,
        value: BigInt(0),
        data: encodeFunctionData({
            abi: contracts[ContractType.DIMO_FORWARDER].abi,
            functionName: TRANSFER_VEHICLE_AND_AFTERMARKET_DEVICE_IDS,
            args: [args.vehicleIds, args.aftermarketDeviceIds, args.to],
        }),
    });
};
export async function transferVehicleAndAftermarketDeviceIDsFromAccount(args, walletClient, publicClient, environment = "prod") {
    var _a;
    const contracts = CHAIN_ABI_MAPPING[(_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.DEV].contracts;
    const { request } = await publicClient.simulateContract({
        address: contracts[ContractType.DIMO_FORWARDER].address,
        abi: contracts[ContractType.DIMO_FORWARDER].abi,
        functionName: TRANSFER_VEHICLE_AND_AFTERMARKET_DEVICE_IDS,
        args: [args.vehicleIds, args.aftermarketDeviceIds, args.to],
        account: walletClient.account,
    });
    const txHash = await walletClient.writeContract(request);
    return txHash;
}
export async function transferVehiclesAndAftermarketDevicesForSignature(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_FORWARDER].address,
        abi: contracts[ContractType.DIMO_FORWARDER].abi,
        functionName: TRANSFER_VEHICLE_AND_AFTERMARKET_DEVICE_IDS,
        args: [args.vehicleIds, args.aftermarketDeviceIds, args.to],
        account: senderAddress,
    });
    const callData = transferVehicleAndAftermarketDeviceIDsCallData(args, environment);
    const nonce = await publicClient.getTransactionCount({ address: senderAddress });
    const preparedTransaction = await walletClient.prepareTransactionRequest({
        to: contracts[ContractType.DIMO_FORWARDER].address,
        nonce: nonce,
        data: callData,
        gas: gasPrice,
        gasLimit: gaslimit,
        maxPriorityFeePerGas: gas.maxPriorityFeePerGas,
        maxFeePerGas: gas.maxFeePerGas,
        chain: chain,
    });
    return preparedTransaction;
}
export const transferAllTypeHash = (args, environment = "prod") => {
    var _a, _b, _c;
    const env = (_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.PROD;
    const chain = (_b = ENV_NETWORK_MAPPING.get(env)) !== 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 domain = {
        name: DIMODomain,
        version: DIMODomainVersion,
        chainId: chain.id,
        verifyingContract: contracts[ContractType.DIMO_REGISTRY].address,
    };
    const types = {
        ["TransferVehicleAndAftermarketDeviceIds"]: [
            { name: "vehicleIds", type: "uint256[]" },
            { name: "aftermarketDeviceIds", type: "uint256[]" },
            { name: "to", type: "address" },
        ],
    };
    const message = {
        vehicleIds: args.vehicleIds,
        aftermarketDeviceIds: args.aftermarketDeviceIds,
        to: args.to,
    };
    const hash = ethers.TypedDataEncoder.hash(domain, types, message);
    return { hash, payload: { domain, types, message } };
};
//# sourceMappingURL=transferVehicleAndADs.js.map