import { BigNumber } from "@ethersproject/bignumber";
import { handler } from ".";
import { accounts, erc721 } from "..";
import { getOfferById } from "../offers/subgraph";
import { encodeValidate } from "../seaport/interface";
import { BaseCoreSDK } from "./../mixins/base-core-sdk";
export class VoucherMixin extends BaseCoreSDK {
    // Implementation
    async burnPremintedVouchers(offerId, amount, overrides = {}) {
        const { returnTxInfo } = overrides;
        const offerFromSubgraph = await getOfferById(this._subgraphUrl, offerId);
        const burnArgs = {
            offerId,
            amount,
            contractAddress: overrides.contractAddress ||
                offerFromSubgraph.seller.voucherCloneAddress,
            web3Lib: this._web3Lib
        };
        if (returnTxInfo === true) {
            return handler.burnPremintedVouchers({
                ...burnArgs,
                returnTxInfo: true
            });
        }
        else {
            return handler.burnPremintedVouchers({
                ...burnArgs,
                returnTxInfo: false
            });
        }
    }
    // Implementation
    async preMint(offerId, amount, overrides = {}) {
        const { returnTxInfo } = overrides;
        const offerFromSubgraph = await getOfferById(this._subgraphUrl, offerId);
        const preMintArgs = {
            offerId,
            amount,
            contractAddress: overrides.contractAddress ||
                offerFromSubgraph.seller.voucherCloneAddress,
            web3Lib: this._web3Lib
        };
        if (returnTxInfo === true) {
            return handler.preMint({
                ...preMintArgs,
                returnTxInfo: true
            });
        }
        else {
            return handler.preMint({
                ...preMintArgs,
                returnTxInfo: false
            });
        }
    }
    // Implementation
    async transferFrom(offerId, to, tokenId, overrides = {}) {
        const { returnTxInfo } = overrides;
        const offerFromSubgraph = await getOfferById(this._subgraphUrl, offerId);
        const transferArgs = {
            from: overrides.owner || (await this._web3Lib.getSignerAddress()),
            to,
            tokenId,
            contractAddress: overrides.contractAddress ||
                offerFromSubgraph.seller.voucherCloneAddress,
            web3Lib: this._web3Lib
        };
        if (returnTxInfo === true) {
            return handler.transferFrom({
                ...transferArgs,
                returnTxInfo: true
            });
        }
        else {
            return handler.transferFrom({
                ...transferArgs,
                returnTxInfo: false
            });
        }
    }
    // Implementation
    async setContractURI(contractURI, collectionIndex, overrides = {}) {
        const { returnTxInfo } = overrides;
        const sellerAddress = await this._web3Lib.getSignerAddress();
        const seller = await accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
        if (!seller) {
            throw new Error(`No seller found for wallet ${sellerAddress}`);
        }
        const collection = seller.collections.find((coll) => 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 handler.setContractURI({
                ...setContractURIArgs,
                returnTxInfo: true
            });
        }
        else {
            return handler.setContractURI({
                ...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.
     */
    async getAvailablePreMints(offerId, overrides = {}) {
        const offerFromSubgraph = await getOfferById(this._subgraphUrl, offerId);
        return 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.
     */
    async getRangeByOfferId(offerId, overrides = {}) {
        const offerFromSubgraph = await getOfferById(this._subgraphUrl, offerId);
        return handler.getRangeByOfferId({
            offerId,
            contractAddress: overrides.contractAddress ||
                offerFromSubgraph.seller.voucherCloneAddress,
            web3Lib: this._web3Lib
        });
    }
    async isApprovedForAll(operator, overrides = {}) {
        const sellerAddress = await this._web3Lib.getSignerAddress();
        const seller = await accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
        return handler.isApprovedForAll({
            owner: overrides.owner || sellerAddress,
            operator,
            contractAddress: overrides.contractAddress || seller.voucherCloneAddress,
            web3Lib: this._web3Lib
        });
    }
    async setApprovalForAllToContract(operator, approved, overrides = {}) {
        const sellerAddress = await this._web3Lib.getSignerAddress();
        const seller = await accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
        return handler.setApprovalForAllToContract({
            operator,
            approved,
            contractAddress: overrides.contractAddress || seller.voucherCloneAddress,
            web3Lib: this._web3Lib
        });
    }
    async approveProtocolForAll(overrides = {}) {
        const sellerAddress = await this._web3Lib.getSignerAddress();
        const seller = await accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
        return erc721.handler.setApprovalForAll({
            operator: overrides.operator || this._protocolDiamond,
            approved: true,
            contractAddress: overrides.contractAddress || seller.voucherCloneAddress,
            web3Lib: this._web3Lib
        });
    }
    async validateSeaportOrders(openseaConduit, seaportContract, orders, overrides = { approveIfNeeded: true }) {
        const sellerAddress = await this._web3Lib.getSignerAddress();
        const seller = await accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
        if (overrides.approveIfNeeded) {
            // Check the openseaConduit is approved for preminted tokens
            const isApprovedForAll = await this.isApprovedForAll(openseaConduit, {
                owner: seller.voucherCloneAddress,
                contractAddress: overrides.contractAddress || seller.voucherCloneAddress
            });
            if (!isApprovedForAll) {
                const txApproval = await this.setApprovalForAllToContract(openseaConduit, true, overrides);
                await txApproval.wait();
            }
        }
        return handler.callExternalContract({
            to: seaportContract,
            data: encodeValidate(orders),
            contractAddress: overrides.contractAddress || seller.voucherCloneAddress,
            web3Lib: this._web3Lib
        });
    }
}
//# sourceMappingURL=mixin.js.map