"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VoucherMixin = void 0;
const bignumber_1 = require("@ethersproject/bignumber");
const _1 = require(".");
const __1 = require("..");
const subgraph_1 = require("../offers/subgraph");
const interface_1 = require("../seaport/interface");
const base_core_sdk_1 = require("./../mixins/base-core-sdk");
class VoucherMixin extends base_core_sdk_1.BaseCoreSDK {
    // Implementation
    burnPremintedVouchers(offerId_1, amount_1) {
        return __awaiter(this, arguments, void 0, function* (offerId, amount, overrides = {}) {
            const { returnTxInfo } = overrides;
            const offerFromSubgraph = yield (0, subgraph_1.getOfferById)(this._subgraphUrl, offerId);
            const burnArgs = {
                offerId,
                amount,
                contractAddress: overrides.contractAddress ||
                    offerFromSubgraph.seller.voucherCloneAddress,
                web3Lib: this._web3Lib
            };
            if (returnTxInfo === true) {
                return _1.handler.burnPremintedVouchers(Object.assign(Object.assign({}, burnArgs), { returnTxInfo: true }));
            }
            else {
                return _1.handler.burnPremintedVouchers(Object.assign(Object.assign({}, burnArgs), { returnTxInfo: false }));
            }
        });
    }
    // Implementation
    preMint(offerId_1, amount_1) {
        return __awaiter(this, arguments, void 0, function* (offerId, amount, overrides = {}) {
            const { returnTxInfo } = overrides;
            const offerFromSubgraph = yield (0, subgraph_1.getOfferById)(this._subgraphUrl, offerId);
            const preMintArgs = {
                offerId,
                amount,
                contractAddress: overrides.contractAddress ||
                    offerFromSubgraph.seller.voucherCloneAddress,
                web3Lib: this._web3Lib
            };
            if (returnTxInfo === true) {
                return _1.handler.preMint(Object.assign(Object.assign({}, preMintArgs), { returnTxInfo: true }));
            }
            else {
                return _1.handler.preMint(Object.assign(Object.assign({}, preMintArgs), { returnTxInfo: false }));
            }
        });
    }
    // Implementation
    transferFrom(offerId_1, to_1, tokenId_1) {
        return __awaiter(this, arguments, void 0, function* (offerId, to, tokenId, overrides = {}) {
            const { returnTxInfo } = overrides;
            const offerFromSubgraph = yield (0, subgraph_1.getOfferById)(this._subgraphUrl, offerId);
            const transferArgs = {
                from: overrides.owner || (yield this._web3Lib.getSignerAddress()),
                to,
                tokenId,
                contractAddress: overrides.contractAddress ||
                    offerFromSubgraph.seller.voucherCloneAddress,
                web3Lib: this._web3Lib
            };
            if (returnTxInfo === true) {
                return _1.handler.transferFrom(Object.assign(Object.assign({}, transferArgs), { returnTxInfo: true }));
            }
            else {
                return _1.handler.transferFrom(Object.assign(Object.assign({}, transferArgs), { returnTxInfo: false }));
            }
        });
    }
    // Implementation
    setContractURI(contractURI_1, collectionIndex_1) {
        return __awaiter(this, arguments, void 0, function* (contractURI, collectionIndex, overrides = {}) {
            const { returnTxInfo } = overrides;
            const sellerAddress = yield this._web3Lib.getSignerAddress();
            const seller = yield __1.accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
            if (!seller) {
                throw new Error(`No seller found for wallet ${sellerAddress}`);
            }
            const collection = seller.collections.find((coll) => bignumber_1.BigNumber.from(coll.collectionIndex).eq(collectionIndex));
            if (!collection) {
                throw new Error(`Collection with index ${collectionIndex} not found for seller with id ${seller.id}`);
            }
            const setContractURIArgs = {
                contractURI,
                contractAddress: collection.collectionContract.address,
                web3Lib: this._web3Lib
            };
            if (returnTxInfo === true) {
                return _1.handler.setContractURI(Object.assign(Object.assign({}, setContractURIArgs), { returnTxInfo: true }));
            }
            else {
                return _1.handler.setContractURI(Object.assign(Object.assign({}, setContractURIArgs), { returnTxInfo: false }));
            }
        });
    }
    /**
     * Gets the number of vouchers available to be pre-minted for an offer.
     * @param offerId - Offer ID
     * @param overrides - Optional overrides.
     * @returns Transaction response.
     */
    getAvailablePreMints(offerId_1) {
        return __awaiter(this, arguments, void 0, function* (offerId, overrides = {}) {
            const offerFromSubgraph = yield (0, subgraph_1.getOfferById)(this._subgraphUrl, offerId);
            return _1.handler.getAvailablePreMints({
                offerId,
                contractAddress: overrides.contractAddress ||
                    offerFromSubgraph.seller.voucherCloneAddress,
                web3Lib: this._web3Lib
            });
        });
    }
    /**
     * Gets the range for an offer.
     * @param offerId - Offer ID
     * @param overrides - Optional overrides.
     * @returns Transaction response.
     */
    getRangeByOfferId(offerId_1) {
        return __awaiter(this, arguments, void 0, function* (offerId, overrides = {}) {
            const offerFromSubgraph = yield (0, subgraph_1.getOfferById)(this._subgraphUrl, offerId);
            return _1.handler.getRangeByOfferId({
                offerId,
                contractAddress: overrides.contractAddress ||
                    offerFromSubgraph.seller.voucherCloneAddress,
                web3Lib: this._web3Lib
            });
        });
    }
    isApprovedForAll(operator_1) {
        return __awaiter(this, arguments, void 0, function* (operator, overrides = {}) {
            const sellerAddress = yield this._web3Lib.getSignerAddress();
            const seller = yield __1.accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
            return _1.handler.isApprovedForAll({
                owner: overrides.owner || sellerAddress,
                operator,
                contractAddress: overrides.contractAddress || seller.voucherCloneAddress,
                web3Lib: this._web3Lib
            });
        });
    }
    setApprovalForAllToContract(operator_1, approved_1) {
        return __awaiter(this, arguments, void 0, function* (operator, approved, overrides = {}) {
            const sellerAddress = yield this._web3Lib.getSignerAddress();
            const seller = yield __1.accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
            return _1.handler.setApprovalForAllToContract({
                operator,
                approved,
                contractAddress: overrides.contractAddress || seller.voucherCloneAddress,
                web3Lib: this._web3Lib
            });
        });
    }
    approveProtocolForAll() {
        return __awaiter(this, arguments, void 0, function* (overrides = {}) {
            const sellerAddress = yield this._web3Lib.getSignerAddress();
            const seller = yield __1.accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
            return __1.erc721.handler.setApprovalForAll({
                operator: overrides.operator || this._protocolDiamond,
                approved: true,
                contractAddress: overrides.contractAddress || seller.voucherCloneAddress,
                web3Lib: this._web3Lib
            });
        });
    }
    validateSeaportOrders(openseaConduit_1, seaportContract_1, orders_1) {
        return __awaiter(this, arguments, void 0, function* (openseaConduit, seaportContract, orders, overrides = { approveIfNeeded: true }) {
            const sellerAddress = yield this._web3Lib.getSignerAddress();
            const seller = yield __1.accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
            if (overrides.approveIfNeeded) {
                // Check the openseaConduit is approved for preminted tokens
                const isApprovedForAll = yield this.isApprovedForAll(openseaConduit, {
                    owner: seller.voucherCloneAddress,
                    contractAddress: overrides.contractAddress || seller.voucherCloneAddress
                });
                if (!isApprovedForAll) {
                    const txApproval = yield this.setApprovalForAllToContract(openseaConduit, true, overrides);
                    yield txApproval.wait();
                }
            }
            return _1.handler.callExternalContract({
                to: seaportContract,
                data: (0, interface_1.encodeValidate)(orders),
                contractAddress: overrides.contractAddress || seller.voucherCloneAddress,
                web3Lib: this._web3Lib
            });
        });
    }
}
exports.VoucherMixin = VoucherMixin;
//# sourceMappingURL=mixin.js.map