import { abis } from "@bosonprotocol/common";
import { Interface } from "@ethersproject/abi";
import { getAddress } from "@ethersproject/address";
import { AddressZero } from "@ethersproject/constants";
import { argsToDRParametersStruct, argsToOfferDatesStruct, argsToOfferDurationsStruct, argsToOfferStruct } from "../offers/interface";
import { conditionArgsToStructs } from "../groups/interface";
export const bosonExchangeHandlerIface = new Interface(abis.IBosonExchangeHandlerABI);
export const bosonExchangeCommitHandlerIface = new Interface(abis.IBosonExchangeCommitHandlerABI);
export function encodeCommitToOffer(buyer, offerId) {
    return bosonExchangeCommitHandlerIface.encodeFunctionData("commitToOffer", [
        buyer,
        offerId
    ]);
}
export function encodeCommitToBuyerOffer(offerId, sellerParams) {
    return bosonExchangeCommitHandlerIface.encodeFunctionData("commitToBuyerOffer", [offerId, argsToSellerOfferStruct(sellerParams)]);
}
export function encodeCommitToConditionalOffer(buyer, offerId, tokenId) {
    return bosonExchangeCommitHandlerIface.encodeFunctionData("commitToConditionalOffer", [buyer, offerId, tokenId]);
}
export function encodeCreateOfferAndCommit(args) {
    return bosonExchangeCommitHandlerIface.encodeFunctionData("createOfferAndCommit", fullOfferArgsToStructs(args));
}
function fullOfferArgsToStructs(args) {
    return [
        fullOfferArgsToStruct(args),
        args.offerCreator,
        args.committer,
        args.signature,
        args.conditionalTokenId || "0",
        [
            args.sellerOfferParams.collectionIndex,
            [
                args.sellerOfferParams.royaltyInfo.recipients,
                args.sellerOfferParams.royaltyInfo.bps
            ],
            args.sellerOfferParams.mutualizerAddress
        ]
    ];
}
export function fullOfferArgsToStruct(args) {
    const feeLimit = args.feeLimit !== undefined ? args.feeLimit : args.price;
    return [
        // fullOffer
        argsToOfferStruct(args),
        argsToOfferDatesStruct(args),
        argsToOfferDurationsStruct(args),
        argsToDRParametersStruct(args),
        conditionArgsToStructs(args.condition),
        args.agentId,
        feeLimit.toString(),
        args.useDepositedFunds
    ];
}
export function encodeCompleteExchange(exchangeId) {
    return bosonExchangeHandlerIface.encodeFunctionData("completeExchange", [
        exchangeId
    ]);
}
export function encodeCompleteExchangeBatch(exchangeIds) {
    return bosonExchangeHandlerIface.encodeFunctionData("completeExchangeBatch", [
        exchangeIds
    ]);
}
export function encodeRevokeVoucher(exchangeId) {
    return bosonExchangeHandlerIface.encodeFunctionData("revokeVoucher", [
        exchangeId
    ]);
}
export function encodeCancelVoucher(exchangeId) {
    return bosonExchangeHandlerIface.encodeFunctionData("cancelVoucher", [
        exchangeId
    ]);
}
export function encodeExpireVoucher(exchangeId) {
    return bosonExchangeHandlerIface.encodeFunctionData("expireVoucher", [
        exchangeId
    ]);
}
export function encodeRedeemVoucher(exchangeId) {
    return bosonExchangeHandlerIface.encodeFunctionData("redeemVoucher", [
        exchangeId
    ]);
}
export function argsToSellerOfferStruct(args) {
    const royaltyInfo = args.royaltyInfo !== undefined
        ? {
            ...args.royaltyInfo,
            recipients: args.royaltyInfo.recipients.map((recipient) => getAddress(recipient.toLowerCase()))
        }
        : {
            recipients: [],
            bps: []
        };
    return {
        collectionIndex: args.collectionIndex || 0,
        royaltyInfo,
        mutualizerAddress: args.mutualizerAddress
            ? getAddress(args.mutualizerAddress.toLowerCase())
            : AddressZero
    };
}
//# sourceMappingURL=interface.js.map