"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExchangesMixin = void 0;
const base_core_sdk_1 = require("./../mixins/base-core-sdk");
const logs_1 = require("../utils/logs");
const handler_1 = require("./handler");
const subgraph_1 = require("./subgraph");
const interface_1 = require("./interface");
const __1 = require("..");
class ExchangesMixin extends base_core_sdk_1.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.
     */
    getExchangeById(exchangeId, queryVars) {
        return __awaiter(this, void 0, void 0, function* () {
            return (0, subgraph_1.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.
     */
    getExchanges(queryVars) {
        return __awaiter(this, void 0, void 0, function* () {
            return (0, subgraph_1.getExchanges)(this._subgraphUrl, queryVars);
        });
    }
    commitToOffer(offerId_1) {
        return __awaiter(this, arguments, void 0, function* (offerId, overrides = {}) {
            const { returnTxInfo } = overrides;
            const buyer = overrides.buyer || (yield this._web3Lib.getSignerAddress());
            const commitArgs = {
                buyer,
                offerId,
                web3Lib: this._web3Lib,
                subgraphUrl: this._subgraphUrl,
                contractAddress: this._protocolDiamond
            };
            if (returnTxInfo === true) {
                return (0, handler_1.commitToOffer)(Object.assign(Object.assign({}, commitArgs), { returnTxInfo: true }));
            }
            else {
                return (0, handler_1.commitToOffer)(Object.assign(Object.assign({}, commitArgs), { returnTxInfo: false }));
            }
        });
    }
    commitToBuyerOffer(offerId_1) {
        return __awaiter(this, arguments, void 0, function* (offerId, sellerParams = {}, overrides = {}) {
            const { returnTxInfo } = overrides;
            const commitArgs = {
                offerId,
                sellerParams: sellerParams || {},
                web3Lib: this._web3Lib,
                subgraphUrl: this._subgraphUrl,
                contractAddress: this._protocolDiamond
            };
            if (returnTxInfo === true) {
                return (0, handler_1.commitToBuyerOffer)(Object.assign(Object.assign({}, commitArgs), { returnTxInfo: true }));
            }
            else {
                return (0, handler_1.commitToBuyerOffer)(Object.assign(Object.assign({}, commitArgs), { returnTxInfo: false }));
            }
        });
    }
    commitToConditionalOffer(offerId_1, tokenId_1) {
        return __awaiter(this, arguments, void 0, function* (offerId, tokenId, overrides = {}) {
            const { returnTxInfo } = overrides;
            const buyer = overrides.buyer || (yield this._web3Lib.getSignerAddress());
            const commitArgs = {
                buyer,
                offerId,
                tokenId,
                web3Lib: this._web3Lib,
                subgraphUrl: this._subgraphUrl,
                contractAddress: this._protocolDiamond
            };
            if (returnTxInfo === true) {
                return (0, handler_1.commitToConditionalOffer)(Object.assign(Object.assign({}, commitArgs), { returnTxInfo: true }));
            }
            else {
                return (0, handler_1.commitToConditionalOffer)(Object.assign(Object.assign({}, commitArgs), { returnTxInfo: false }));
            }
        });
    }
    // Implementation
    createOfferAndCommit(createOfferAndCommitArgs_1) {
        return __awaiter(this, arguments, void 0, function* (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 __1.exchanges.handler.createOfferAndCommit(Object.assign(Object.assign({}, offerArgs), { returnTxInfo: true }));
            }
            else {
                return __1.exchanges.handler.createOfferAndCommit(Object.assign(Object.assign({}, offerArgs), { returnTxInfo: false }));
            }
        });
    }
    signFullOffer(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const { returnTypedDataToSign } = args, argsWithoutReturnTypedDataToSign = __rest(args, ["returnTypedDataToSign"]);
            const params = Object.assign(Object.assign({}, argsWithoutReturnTypedDataToSign), { web3Lib: this._web3Lib, contractAddress: this._protocolDiamond, chainId: this._chainId });
            if (returnTypedDataToSign === true) {
                return (0, handler_1.signFullOffer)(Object.assign(Object.assign({}, params), { returnTypedDataToSign: true }));
            }
            else {
                return (0, handler_1.signFullOffer)(Object.assign(Object.assign({}, 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 ((0, logs_1.getValueFromLogs)({
            iface: interface_1.bosonExchangeHandlerIface,
            logs,
            eventArgsKey: "exchangeId",
            eventName: "BuyerCommitted"
        }) ||
            (0, logs_1.getValueFromLogs)({
                iface: interface_1.bosonExchangeHandlerIface,
                logs,
                eventArgsKey: "exchangeId",
                eventName: "SellerCommitted"
            }));
    }
    revokeVoucher(exchangeId, returnTxInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            const revokeArgs = {
                web3Lib: this._web3Lib,
                contractAddress: this._protocolDiamond,
                exchangeId,
                subgraphUrl: this._subgraphUrl
            };
            if (returnTxInfo === true) {
                return (0, handler_1.revokeVoucher)(Object.assign(Object.assign({}, revokeArgs), { returnTxInfo: true }));
            }
            else {
                return (0, handler_1.revokeVoucher)(Object.assign(Object.assign({}, revokeArgs), { returnTxInfo: false }));
            }
        });
    }
    cancelVoucher(exchangeId, returnTxInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            const cancelArgs = {
                web3Lib: this._web3Lib,
                contractAddress: this._protocolDiamond,
                exchangeId,
                subgraphUrl: this._subgraphUrl
            };
            if (returnTxInfo === true) {
                return (0, handler_1.cancelVoucher)(Object.assign(Object.assign({}, cancelArgs), { returnTxInfo: true }));
            }
            else {
                return (0, handler_1.cancelVoucher)(Object.assign(Object.assign({}, cancelArgs), { returnTxInfo: false }));
            }
        });
    }
    redeemVoucher(exchangeId, returnTxInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            const redeemArgs = {
                web3Lib: this._web3Lib,
                contractAddress: this._protocolDiamond,
                exchangeId,
                subgraphUrl: this._subgraphUrl
            };
            if (returnTxInfo === true) {
                return (0, handler_1.redeemVoucher)(Object.assign(Object.assign({}, redeemArgs), { returnTxInfo: true }));
            }
            else {
                return (0, handler_1.redeemVoucher)(Object.assign(Object.assign({}, redeemArgs), { returnTxInfo: false }));
            }
        });
    }
    completeExchange(exchangeId, returnTxInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            const completeArgs = {
                web3Lib: this._web3Lib,
                contractAddress: this._protocolDiamond,
                exchangeId,
                subgraphUrl: this._subgraphUrl
            };
            if (returnTxInfo === true) {
                return (0, handler_1.completeExchange)(Object.assign(Object.assign({}, completeArgs), { returnTxInfo: true }));
            }
            else {
                return (0, handler_1.completeExchange)(Object.assign(Object.assign({}, completeArgs), { returnTxInfo: false }));
            }
        });
    }
    completeExchangeBatch(exchangeIds, returnTxInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            const batchArgs = {
                web3Lib: this._web3Lib,
                contractAddress: this._protocolDiamond,
                exchangeIds,
                subgraphUrl: this._subgraphUrl
            };
            if (returnTxInfo === true) {
                return (0, handler_1.completeExchangeBatch)(Object.assign(Object.assign({}, batchArgs), { returnTxInfo: true }));
            }
            else {
                return (0, handler_1.completeExchangeBatch)(Object.assign(Object.assign({}, batchArgs), { returnTxInfo: false }));
            }
        });
    }
    expireVoucher(exchangeId, returnTxInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            const expireArgs = {
                web3Lib: this._web3Lib,
                contractAddress: this._protocolDiamond,
                exchangeId,
                subgraphUrl: this._subgraphUrl
            };
            if (returnTxInfo === true) {
                return (0, handler_1.expireVoucher)(Object.assign(Object.assign({}, expireArgs), { returnTxInfo: true }));
            }
            else {
                return (0, handler_1.expireVoucher)(Object.assign(Object.assign({}, expireArgs), { returnTxInfo: false }));
            }
        });
    }
    getExchangeTokenId(exchangeId, offerId) {
        return (0, handler_1.getExchangeTokenId)(exchangeId, offerId);
    }
    parseTokenId(tokenId) {
        return (0, handler_1.parseTokenId)(tokenId);
    }
}
exports.ExchangesMixin = ExchangesMixin;
//# sourceMappingURL=mixin.js.map