import { BaseCoreSDK } from "./../mixins/base-core-sdk";
import { getValueFromLogs } from "../utils/logs";
import { commitToOffer, revokeVoucher, cancelVoucher, redeemVoucher, completeExchange, completeExchangeBatch, expireVoucher, commitToConditionalOffer, getExchangeTokenId, parseTokenId, signFullOffer, commitToBuyerOffer } from "./handler";
import { getExchangeById, getExchanges } from "./subgraph";
import { bosonExchangeHandlerIface } from "./interface";
import { exchanges } from "..";
export class ExchangesMixin extends BaseCoreSDK {
    /* -------------------------------------------------------------------------- */
    /*                          Exchange related methods                          */
    /* -------------------------------------------------------------------------- */
    /**
     * Returns exchange entity from subgraph.
     * @param exchangeId - ID of exchange entity.
     * @param queryVars - Optional query variables to skip, order or filter.
     * @returns Exchange entity from subgraph.
     */
    async getExchangeById(exchangeId, queryVars) {
        return getExchangeById(this._subgraphUrl, exchangeId, queryVars);
    }
    /**
     * Returns exchange entities from subgraph.
     * @param queryVars - Optional query variables to skip, order or filter.
     * @returns Exchange entities from subgraph.
     */
    async getExchanges(queryVars) {
        return getExchanges(this._subgraphUrl, queryVars);
    }
    async commitToOffer(offerId, overrides = {}) {
        const { returnTxInfo } = overrides;
        const buyer = overrides.buyer || (await this._web3Lib.getSignerAddress());
        const commitArgs = {
            buyer,
            offerId,
            web3Lib: this._web3Lib,
            subgraphUrl: this._subgraphUrl,
            contractAddress: this._protocolDiamond
        };
        if (returnTxInfo === true) {
            return commitToOffer({
                ...commitArgs,
                returnTxInfo: true
            });
        }
        else {
            return commitToOffer({
                ...commitArgs,
                returnTxInfo: false
            });
        }
    }
    async commitToBuyerOffer(offerId, sellerParams = {}, overrides = {}) {
        const { returnTxInfo } = overrides;
        const commitArgs = {
            offerId,
            sellerParams: sellerParams || {},
            web3Lib: this._web3Lib,
            subgraphUrl: this._subgraphUrl,
            contractAddress: this._protocolDiamond
        };
        if (returnTxInfo === true) {
            return commitToBuyerOffer({
                ...commitArgs,
                returnTxInfo: true
            });
        }
        else {
            return commitToBuyerOffer({
                ...commitArgs,
                returnTxInfo: false
            });
        }
    }
    async commitToConditionalOffer(offerId, tokenId, overrides = {}) {
        const { returnTxInfo } = overrides;
        const buyer = overrides.buyer || (await this._web3Lib.getSignerAddress());
        const commitArgs = {
            buyer,
            offerId,
            tokenId,
            web3Lib: this._web3Lib,
            subgraphUrl: this._subgraphUrl,
            contractAddress: this._protocolDiamond
        };
        if (returnTxInfo === true) {
            return commitToConditionalOffer({
                ...commitArgs,
                returnTxInfo: true
            });
        }
        else {
            return commitToConditionalOffer({
                ...commitArgs,
                returnTxInfo: false
            });
        }
    }
    // Implementation
    async createOfferAndCommit(createOfferAndCommitArgs, overrides = {}) {
        const { returnTxInfo } = overrides;
        const offerArgs = {
            createOfferAndCommitArgs,
            web3Lib: this._web3Lib,
            theGraphStorage: this._theGraphStorage,
            metadataStorage: this._metadataStorage,
            subgraphUrl: this._subgraphUrl,
            contractAddress: overrides.contractAddress || this._protocolDiamond,
            txRequest: overrides.txRequest
        };
        if (returnTxInfo === true) {
            return exchanges.handler.createOfferAndCommit({
                ...offerArgs,
                returnTxInfo: true
            });
        }
        else {
            return exchanges.handler.createOfferAndCommit({
                ...offerArgs,
                returnTxInfo: false
            });
        }
    }
    async signFullOffer(args) {
        const { returnTypedDataToSign, ...argsWithoutReturnTypedDataToSign } = args;
        const params = {
            ...argsWithoutReturnTypedDataToSign,
            web3Lib: this._web3Lib,
            contractAddress: this._protocolDiamond,
            chainId: this._chainId
        };
        if (returnTypedDataToSign === true) {
            return signFullOffer({
                ...params,
                returnTypedDataToSign: true
            });
        }
        else {
            return signFullOffer({
                ...params,
                returnTypedDataToSign: false
            });
        }
    }
    /**
     * Utility method to retrieve the created `exchangeId` from logs after calling `commitToOffer` or `commitToBuyerOffer`.
     * @param logs - Logs to search in.
     * @returns Created exchange id.
     */
    getCommittedExchangeIdFromLogs(logs) {
        return (getValueFromLogs({
            iface: bosonExchangeHandlerIface,
            logs,
            eventArgsKey: "exchangeId",
            eventName: "BuyerCommitted"
        }) ||
            getValueFromLogs({
                iface: bosonExchangeHandlerIface,
                logs,
                eventArgsKey: "exchangeId",
                eventName: "SellerCommitted"
            }));
    }
    async revokeVoucher(exchangeId, returnTxInfo) {
        const revokeArgs = {
            web3Lib: this._web3Lib,
            contractAddress: this._protocolDiamond,
            exchangeId,
            subgraphUrl: this._subgraphUrl
        };
        if (returnTxInfo === true) {
            return revokeVoucher({
                ...revokeArgs,
                returnTxInfo: true
            });
        }
        else {
            return revokeVoucher({
                ...revokeArgs,
                returnTxInfo: false
            });
        }
    }
    async cancelVoucher(exchangeId, returnTxInfo) {
        const cancelArgs = {
            web3Lib: this._web3Lib,
            contractAddress: this._protocolDiamond,
            exchangeId,
            subgraphUrl: this._subgraphUrl
        };
        if (returnTxInfo === true) {
            return cancelVoucher({
                ...cancelArgs,
                returnTxInfo: true
            });
        }
        else {
            return cancelVoucher({
                ...cancelArgs,
                returnTxInfo: false
            });
        }
    }
    async redeemVoucher(exchangeId, returnTxInfo) {
        const redeemArgs = {
            web3Lib: this._web3Lib,
            contractAddress: this._protocolDiamond,
            exchangeId,
            subgraphUrl: this._subgraphUrl
        };
        if (returnTxInfo === true) {
            return redeemVoucher({
                ...redeemArgs,
                returnTxInfo: true
            });
        }
        else {
            return redeemVoucher({
                ...redeemArgs,
                returnTxInfo: false
            });
        }
    }
    async completeExchange(exchangeId, returnTxInfo) {
        const completeArgs = {
            web3Lib: this._web3Lib,
            contractAddress: this._protocolDiamond,
            exchangeId,
            subgraphUrl: this._subgraphUrl
        };
        if (returnTxInfo === true) {
            return completeExchange({
                ...completeArgs,
                returnTxInfo: true
            });
        }
        else {
            return completeExchange({
                ...completeArgs,
                returnTxInfo: false
            });
        }
    }
    async completeExchangeBatch(exchangeIds, returnTxInfo) {
        const batchArgs = {
            web3Lib: this._web3Lib,
            contractAddress: this._protocolDiamond,
            exchangeIds,
            subgraphUrl: this._subgraphUrl
        };
        if (returnTxInfo === true) {
            return completeExchangeBatch({
                ...batchArgs,
                returnTxInfo: true
            });
        }
        else {
            return completeExchangeBatch({
                ...batchArgs,
                returnTxInfo: false
            });
        }
    }
    async expireVoucher(exchangeId, returnTxInfo) {
        const expireArgs = {
            web3Lib: this._web3Lib,
            contractAddress: this._protocolDiamond,
            exchangeId,
            subgraphUrl: this._subgraphUrl
        };
        if (returnTxInfo === true) {
            return expireVoucher({
                ...expireArgs,
                returnTxInfo: true
            });
        }
        else {
            return expireVoucher({
                ...expireArgs,
                returnTxInfo: false
            });
        }
    }
    getExchangeTokenId(exchangeId, offerId) {
        return getExchangeTokenId(exchangeId, offerId);
    }
    parseTokenId(tokenId) {
        return parseTokenId(tokenId);
    }
}
//# sourceMappingURL=mixin.js.map