import { AuthTokenType } from "@bosonprotocol/common";
import { handler } from ".";
import { getOfferById } from "../offers/subgraph";
import { BaseCoreSDK } from "./../mixins/base-core-sdk";
import { accounts } from "..";
export class MetaTxMixin extends BaseCoreSDK {
    // Implementation
    async signMetaTx(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTx({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTx({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCreateSeller(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCreateSeller({
                web3Lib: this._web3Lib,
                theGraphStorage: this._theGraphStorage,
                metadataStorage: this._metadataStorage,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCreateSeller({
            web3Lib: this._web3Lib,
            theGraphStorage: this._theGraphStorage,
            metadataStorage: this._metadataStorage,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxUpdateSeller(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxUpdateSeller({
                web3Lib: this._web3Lib,
                theGraphStorage: this._theGraphStorage,
                metadataStorage: this._metadataStorage,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxUpdateSeller({
            web3Lib: this._web3Lib,
            theGraphStorage: this._theGraphStorage,
            metadataStorage: this._metadataStorage,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxOptInToSellerUpdate(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxOptInToSellerUpdate({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxOptInToSellerUpdate({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    async signMetaTxUpdateSellerAndOptIn(sellerUpdates) {
        let nonce = Date.now();
        const updateMetaTx = await this.signMetaTxUpdateSeller({
            updateSellerArgs: sellerUpdates,
            nonce,
            theGraphStorage: this._theGraphStorage,
            metadataStorage: this._metadataStorage
        });
        const updateTx = await this.relayMetaTransaction({
            functionName: updateMetaTx.functionName,
            functionSignature: updateMetaTx.functionSignature,
            sigR: updateMetaTx.r,
            sigS: updateMetaTx.s,
            sigV: updateMetaTx.v,
            nonce
        });
        await updateTx.wait();
        let seller;
        let count = 200;
        while ((!seller || !seller.pendingSeller) && count-- > 0) {
            await new Promise((resolve) => setTimeout(resolve, 300));
            seller = await this["getSellerById"](sellerUpdates.id);
        }
        if (!seller) {
            throw new Error("[signMetaTxUpdateSellerAndOptIn] seller could not be retrieved in time");
        }
        const pendingSellerUpdate = seller.pendingSeller;
        if (!pendingSellerUpdate) {
            throw new Error("[signMetaTxUpdateSellerAndOptIn] seller.pendingSeller could not be retrieved in time");
        }
        // Find all updates that can be opted in by the current account
        const currentAccount = (await this._web3Lib.getSignerAddress()).toLowerCase();
        const fieldsToUpdate = {
            assistant: currentAccount === pendingSellerUpdate.assistant?.toLowerCase(),
            admin: currentAccount === pendingSellerUpdate.admin?.toLowerCase(),
            authToken: pendingSellerUpdate.authTokenType !== undefined &&
                pendingSellerUpdate.authTokenType !== null &&
                pendingSellerUpdate.authTokenType !== AuthTokenType.NONE
        };
        if (fieldsToUpdate.assistant ||
            fieldsToUpdate.admin ||
            fieldsToUpdate.authToken) {
            nonce = Date.now();
            const optInMetaTx = await this.signMetaTxOptInToSellerUpdate({
                optInToSellerUpdateArgs: {
                    id: sellerUpdates.id,
                    fieldsToUpdate: {
                        assistant: currentAccount === pendingSellerUpdate.assistant.toLowerCase(),
                        admin: currentAccount === pendingSellerUpdate.admin.toLowerCase(),
                        authToken: pendingSellerUpdate.authTokenType !== AuthTokenType.NONE
                    }
                },
                nonce
            });
            return this.relayMetaTransaction({
                functionName: optInMetaTx.functionName,
                functionSignature: optInMetaTx.functionSignature,
                sigR: optInMetaTx.r,
                sigS: optInMetaTx.s,
                sigV: optInMetaTx.v,
                nonce
            });
        }
        // If there is nothing to optIn from the current account, then return the response from updateSeller
        return updateTx;
    }
    // Implementation
    async signMetaTxCreateOffer(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCreateOffer({
                web3Lib: this._web3Lib,
                theGraphStorage: this._theGraphStorage,
                metadataStorage: this._metadataStorage,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCreateOffer({
            web3Lib: this._web3Lib,
            theGraphStorage: this._theGraphStorage,
            metadataStorage: this._metadataStorage,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCreateOfferBatch(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCreateOfferBatch({
                web3Lib: this._web3Lib,
                theGraphStorage: this._theGraphStorage,
                metadataStorage: this._metadataStorage,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCreateOfferBatch({
            web3Lib: this._web3Lib,
            theGraphStorage: this._theGraphStorage,
            metadataStorage: this._metadataStorage,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCreateGroup(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCreateGroup({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCreateGroup({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxReserveRange(args) {
        const offer = await getOfferById(this._subgraphUrl, args.offerId);
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxReserveRange({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                to: args.to === "contract"
                    ? offer.seller.voucherCloneAddress
                    : offer.seller.assistant,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxReserveRange({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            to: args.to === "contract"
                ? offer.seller.voucherCloneAddress
                : offer.seller.assistant,
            returnTypedDataToSign: false
        });
    }
    async signMetaTxPreMint(args, overrides = {}) {
        const forwarderAddress = this._contracts.forwarder;
        const batchId = overrides.batchId || 0;
        const offerFromSubgraph = await getOfferById(this._subgraphUrl, args.offerId);
        return handler.signMetaTxPreMint({
            web3Lib: this._web3Lib,
            bosonVoucherAddress: offerFromSubgraph.seller.voucherCloneAddress,
            chainId: this._chainId,
            forwarderAddress,
            batchId,
            forwarderAbi: this._metaTxConfig.forwarderAbi,
            relayerUrl: this._metaTxConfig.relayerUrl,
            ...args
        });
    }
    async signMetaTxSetApprovalForAll(args, overrides = {}) {
        const sellerAddress = await this._web3Lib.getSignerAddress();
        const seller = await accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
        const forwarderAddress = this._contracts.forwarder;
        const batchId = overrides.batchId || 0;
        return handler.signMetaTxSetApprovalForAll({
            web3Lib: this._web3Lib,
            bosonVoucherAddress: seller.voucherCloneAddress,
            chainId: this._chainId,
            forwarderAddress,
            batchId,
            forwarderAbi: this._metaTxConfig.forwarderAbi,
            relayerUrl: this._metaTxConfig.relayerUrl,
            ...args
        });
    }
    async signMetaTxSetApprovalForAllToContract(args, overrides = {}) {
        const sellerAddress = await this._web3Lib.getSignerAddress();
        const seller = await accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
        const forwarderAddress = this._contracts.forwarder;
        const batchId = overrides.batchId || 0;
        return handler.signMetaTxSetApprovalForAllToContract({
            web3Lib: this._web3Lib,
            bosonVoucherAddress: args.bosonVoucherAddress || seller.voucherCloneAddress,
            chainId: this._chainId,
            forwarderAddress,
            batchId,
            forwarderAbi: this._metaTxConfig.forwarderAbi,
            relayerUrl: this._metaTxConfig.relayerUrl,
            ...args
        }, {
            txGas: overrides.txGas
        });
    }
    async signMetaTxCallExternalContract(args, overrides = {}) {
        const sellerAddress = await this._web3Lib.getSignerAddress();
        const seller = await accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
        const forwarderAddress = this._contracts.forwarder;
        const batchId = overrides.batchId || 0;
        return handler.signMetaTxCallExternalContract({
            web3Lib: this._web3Lib,
            bosonVoucherAddress: seller.voucherCloneAddress,
            chainId: this._chainId,
            forwarderAddress,
            batchId,
            forwarderAbi: this._metaTxConfig.forwarderAbi,
            relayerUrl: this._metaTxConfig.relayerUrl,
            ...args
        }, {
            txGas: overrides.txGas
        });
    }
    async relayBiconomyMetaTransaction(contractAddress, metaTxParams, overrides = {}) {
        const { metaTxApiId, metaTxApiKey, metaTxRelayerUrl } = this.assertAndGetMetaTxConfig({ ...overrides, contractAddress });
        return handler.relayBiconomyMetaTransaction({
            web3LibAdapter: this._web3Lib,
            contractAddress,
            chainId: this._chainId,
            metaTx: {
                config: {
                    relayerUrl: metaTxRelayerUrl,
                    apiId: metaTxApiId,
                    apiKey: metaTxApiKey
                },
                params: {
                    userAddress: overrides.userAddress || (await this._web3Lib.getSignerAddress()),
                    request: metaTxParams.request,
                    domainSeparator: metaTxParams.domainSeparator,
                    signature: metaTxParams.signature
                }
            }
        });
    }
    // Implementation
    async signMetaTxCreateOfferWithCondition(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCreateOfferWithCondition({
                web3Lib: this._web3Lib,
                theGraphStorage: this._theGraphStorage,
                metadataStorage: this._metadataStorage,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCreateOfferWithCondition({
            web3Lib: this._web3Lib,
            theGraphStorage: this._theGraphStorage,
            metadataStorage: this._metadataStorage,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxVoidOffer(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxVoidOffer({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxVoidOffer({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxVoidOfferBatch(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxVoidOfferBatch({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxVoidOfferBatch({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxExtendOffer(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxExtendOffer({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxExtendOffer({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxExtendOfferBatch(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxExtendOfferBatch({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxExtendOfferBatch({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCompleteExchange(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCompleteExchange({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCompleteExchange({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCompleteExchangeBatch(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCompleteExchangeBatch({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCompleteExchangeBatch({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCommitToOffer(args) {
        const offer = await getOfferById(this._subgraphUrl, args.offerId);
        if (offer.condition) {
            // keep compatibility with previous version
            // Using type assertion because TypeScript can't resolve overloads inside the implementation body
            // when the args type comes from a complex Omit<Parameters<...>[0], ...> expression
            const self = this;
            const conditionalArgs = {
                ...args,
                tokenId: offer.condition.minTokenId
            };
            return self.signMetaTxCommitToConditionalOffer(conditionalArgs);
        }
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCommitToOffer({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCommitToOffer({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCommitToConditionalOffer(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCommitToConditionalOffer({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCommitToConditionalOffer({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCommitToBuyerOffer(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCommitToBuyerOffer({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCommitToBuyerOffer({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCreateOfferAndCommit(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCreateOfferAndCommit({
                web3Lib: this._web3Lib,
                theGraphStorage: this._theGraphStorage,
                metadataStorage: this._metadataStorage,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCreateOfferAndCommit({
            web3Lib: this._web3Lib,
            theGraphStorage: this._theGraphStorage,
            metadataStorage: this._metadataStorage,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCommitToOfferAndRedeemVoucher(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCommitToOfferAndRedeemVoucher({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCommitToOfferAndRedeemVoucher({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCommitToConditionalOfferAndRedeemVoucher(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCommitToConditionalOfferAndRedeemVoucher({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCommitToConditionalOfferAndRedeemVoucher({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCreateOfferCommitAndRedeem(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCreateOfferCommitAndRedeem({
                web3Lib: this._web3Lib,
                theGraphStorage: this._theGraphStorage,
                metadataStorage: this._metadataStorage,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCreateOfferCommitAndRedeem({
            web3Lib: this._web3Lib,
            theGraphStorage: this._theGraphStorage,
            metadataStorage: this._metadataStorage,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxCancelVoucher(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxCancelVoucher({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxCancelVoucher({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxRedeemVoucher(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxRedeemVoucher({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxRedeemVoucher({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxExpireVoucher(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxExpireVoucher({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxExpireVoucher({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxRevokeVoucher(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxRevokeVoucher({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxRevokeVoucher({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxRetractDispute(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxRetractDispute({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxRetractDispute({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxEscalateDispute(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxEscalateDispute({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxEscalateDispute({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxRaiseDispute(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxRaiseDispute({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxRaiseDispute({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxResolveDispute(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxResolveDispute({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxResolveDispute({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxExtendDisputeTimeout(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxExtendDisputeTimeout({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxExtendDisputeTimeout({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxWithdrawFunds(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxWithdrawFunds({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxWithdrawFunds({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    // Implementation
    async signMetaTxDepositFunds(args) {
        if (args.returnTypedDataToSign) {
            return handler.signMetaTxDepositFunds({
                web3Lib: this._web3Lib,
                metaTxHandlerAddress: this._protocolDiamond,
                chainId: this._chainId,
                ...args,
                returnTypedDataToSign: true
            });
        }
        return handler.signMetaTxDepositFunds({
            web3Lib: this._web3Lib,
            metaTxHandlerAddress: this._protocolDiamond,
            chainId: this._chainId,
            ...args,
            returnTypedDataToSign: false
        });
    }
    /**
     * Relay a meta transaction,
     * @param metaTxParams - Required params for meta transaction.
     * @param overrides - Optional overrides.
     * @returns Transaction response.
     */
    async relayMetaTransaction(metaTxParams, overrides = {}) {
        const { metaTxApiId, metaTxApiKey, metaTxRelayerUrl, contractAddress } = this.assertAndGetMetaTxConfig(overrides);
        return handler.relayMetaTransaction({
            web3LibAdapter: this._web3Lib,
            contractAddress,
            chainId: this._chainId,
            metaTx: {
                config: {
                    relayerUrl: metaTxRelayerUrl,
                    apiId: metaTxApiId,
                    apiKey: metaTxApiKey
                },
                params: {
                    userAddress: overrides.userAddress || (await this._web3Lib.getSignerAddress()),
                    functionName: metaTxParams.functionName,
                    functionSignature: metaTxParams.functionSignature,
                    nonce: metaTxParams.nonce,
                    sigR: metaTxParams.sigR,
                    sigS: metaTxParams.sigS,
                    sigV: metaTxParams.sigV,
                    transferAuthorizations: metaTxParams.transferAuthorizations
                }
            }
        });
    }
    /**
     * Execute a signed meta transaction directly on-chain (no Biconomy relayer).
     * Routes to `executeMetaTransaction` or `executeMetaTransactionWithTokenTransferAuthorization`
     * depending on whether `transferAuthorizations` is provided and non-empty.
     * The wallet backing the SDK pays the gas; pass `overrides.userAddress` when
     * the meta-tx signer differs from that wallet.
     * @param metaTxParams - Required params for meta transaction.
     * @param overrides - Optional overrides.
     * @returns Transaction response.
     */
    async executeMetaTransaction(metaTxParams, overrides = {}) {
        const userAddress = overrides.userAddress || (await this._web3Lib.getSignerAddress());
        const contractAddress = overrides.contractAddress || this._protocolDiamond;
        if (metaTxParams.transferAuthorizations?.length) {
            return handler.executeMetaTransactionWithTokenTransferAuthorization({
                web3Lib: this._web3Lib,
                contractAddress,
                userAddress,
                functionName: metaTxParams.functionName,
                functionSignature: metaTxParams.functionSignature,
                nonce: metaTxParams.nonce,
                sigR: metaTxParams.sigR,
                sigS: metaTxParams.sigS,
                sigV: metaTxParams.sigV,
                transferAuthorizations: metaTxParams.transferAuthorizations
            });
        }
        return handler.executeMetaTransaction({
            web3Lib: this._web3Lib,
            contractAddress,
            userAddress,
            functionName: metaTxParams.functionName,
            functionSignature: metaTxParams.functionSignature,
            nonce: metaTxParams.nonce,
            sigR: metaTxParams.sigR,
            sigS: metaTxParams.sigS,
            sigV: metaTxParams.sigV
        });
    }
    /**
     * Returns information of submitted meta transaction.
     * See https://docs.biconomy.io/api/native-meta-tx/get-retried-hashes.
     * @param originalMetaTxHash - Original meta transaction as returned by `coreSDK.relayMetaTransaction`
     * @param overrides - Optional overrides for meta transaction config.
     * @returns - Additional meta transaction information.
     */
    async getResubmittedMetaTx(originalMetaTxHash, overrides = {}) {
        const { metaTxApiId, metaTxApiKey, metaTxRelayerUrl } = this.assertAndGetMetaTxConfig(overrides);
        return handler.getResubmitted({
            chainId: this._chainId,
            metaTx: {
                config: {
                    relayerUrl: metaTxRelayerUrl,
                    apiId: metaTxApiId,
                    apiKey: metaTxApiKey
                },
                originalHash: originalMetaTxHash
            }
        });
    }
}
//# sourceMappingURL=mixin.js.map