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 { CLAIM_AFTERMARKET_DEVICE } from "../constants/methods.js";
import { polygon } from "viem/chains";
import { ethers } from "ethers";
import { ClaimAftermarketDeviceSign, AftermarketDeviceNode, Owner, SolidityTypeUint256, SolidityTypeAddress, DIMODomain, DIMODomainVersion, } from "../constants/dimo.js";
import { executeTransaction } from "../transactions/execute.js";
export const claimAftermarketDeviceTypeHash = (aftermarketDeviceNode, owner, 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 = {
        [ClaimAftermarketDeviceSign]: [
            { name: AftermarketDeviceNode, type: SolidityTypeUint256 },
            { name: Owner, type: SolidityTypeAddress },
        ],
    };
    const message = {
        aftermarketDeviceNode: aftermarketDeviceNode,
        owner: owner,
    };
    const hash = ethers.TypedDataEncoder.hash(domain, types, message);
    return { hash, payload: { domain, types, message } };
};
export function claimAftermarketDeviceCallData(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_REGISTRY].abi,
        functionName: CLAIM_AFTERMARKET_DEVICE,
        args: [args.aftermarketDeviceNode, args.aftermarketDeviceSig],
    });
}
export const claimAftermarketDeviceTransaction = 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_REGISTRY].address,
        value: BigInt("0"),
        data: claimAftermarketDeviceCallData(args, config.environment),
    };
    const resp = await executeTransaction(subOrganizationId, walletAddress, txData, passkeyStamper, config);
    return resp;
};
export const claimAftermarketDevice = 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_REGISTRY].address,
        value: BigInt(0),
        data: encodeFunctionData({
            abi: contracts[ContractType.DIMO_REGISTRY].abi,
            functionName: CLAIM_AFTERMARKET_DEVICE,
            args: [args.aftermarketDeviceNode, args.aftermarketDeviceSig],
        }),
    });
};
//# sourceMappingURL=claimAftermarketDevice.js.map