import { encodeFunctionData } from "viem";
import { ContractType, ENVIRONMENT } from "../types/dimo.js";
import { CHAIN_ABI_MAPPING, ENV_MAPPING } from "../constants/mappings.js";
import { SET_PERMISSIONS_SACD } from "../constants/methods.js";
import { executeTransaction } from "../transactions/execute.js";
export async function setVehiclePermissions(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 setPermissionsSACD({
        asset: contracts[ContractType.DIMO_VEHICLE_ID].address,
        tokenId: args.tokenId,
        grantee: args.grantee,
        permissions: args.permissions,
        expiration: args.expiration,
        source: args.source,
    }, client, contracts);
}
export async function setVehiclePermissionsBulk(arg, client, environment = "prod") {
    var _a;
    const contracts = CHAIN_ABI_MAPPING[(_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.PROD].contracts;
    const callData = arg.tokenIds.map((tokenId) => {
        return {
            to: contracts[ContractType.DIMO_SACD].address,
            value: BigInt(0),
            data: encodeFunctionData({
                abi: contracts[ContractType.DIMO_SACD].abi,
                functionName: SET_PERMISSIONS_SACD,
                args: [
                    contracts[ContractType.DIMO_VEHICLE_ID].address,
                    tokenId,
                    arg.grantee,
                    arg.permissions,
                    arg.expiration,
                    arg.source,
                ],
            }),
        };
    });
    return await client.account.encodeCallData(callData);
}
export async function setVehiclePermissionsBatch(args, client, environment = "prod") {
    var _a;
    const contracts = CHAIN_ABI_MAPPING[(_a = ENV_MAPPING.get(environment)) !== null && _a !== void 0 ? _a : ENVIRONMENT.PROD].contracts;
    const callData = args.map((arg) => {
        return {
            to: contracts[ContractType.DIMO_SACD].address,
            value: BigInt(0),
            data: encodeFunctionData({
                abi: contracts[ContractType.DIMO_SACD].abi,
                functionName: SET_PERMISSIONS_SACD,
                args: [
                    contracts[ContractType.DIMO_VEHICLE_ID].address,
                    arg.tokenId,
                    arg.grantee,
                    arg.permissions,
                    arg.expiration,
                    arg.source,
                ],
            }),
        };
    });
    return await client.account.encodeCallData(callData);
}
export async function setPermissionsSACD(args, client, contracts) {
    return await client.account.encodeCallData({
        to: contracts[ContractType.DIMO_SACD].address,
        value: BigInt(0),
        data: encodeFunctionData({
            abi: contracts[ContractType.DIMO_SACD].abi,
            functionName: SET_PERMISSIONS_SACD,
            args: [args.asset, args.tokenId, args.grantee, args.permissions, args.expiration, args.source],
        }),
    });
}
export function sacdCallData(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_SACD].abi,
        functionName: SET_PERMISSIONS_SACD,
        args: [args.asset, args.tokenId, args.grantee, args.permissions, args.expiration, args.source],
    });
}
export const setVehiclePermissionsTransaction = 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 sacdArgs = args;
    sacdArgs.asset = contracts[ContractType.DIMO_VEHICLE_ID].address;
    const txData = {
        callType: "call",
        to: contracts[ContractType.DIMO_SACD].address,
        value: BigInt("0"),
        data: sacdCallData(sacdArgs, config.environment),
    };
    const resp = await executeTransaction(subOrganizationId, walletAddress, txData, passkeyStamper, config);
    return resp;
};
//# sourceMappingURL=setPermissionsSACD.js.map