"use strict";
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.bosonOfferHandlerIface = void 0;
exports.encodeCreateOffer = encodeCreateOffer;
exports.encodeUpdateOfferRoyaltyRecipients = encodeUpdateOfferRoyaltyRecipients;
exports.encodeUpdateOfferRoyaltyRecipientsBatch = encodeUpdateOfferRoyaltyRecipientsBatch;
exports.encodeCreateOfferBatch = encodeCreateOfferBatch;
exports.createOfferArgsToStructs = createOfferArgsToStructs;
exports.argsToOfferStruct = argsToOfferStruct;
exports.argsToOfferDatesStruct = argsToOfferDatesStruct;
exports.argsToOfferDurationsStruct = argsToOfferDurationsStruct;
exports.argsToDRParametersStruct = argsToDRParametersStruct;
exports.encodeReserveRange = encodeReserveRange;
exports.encodeVoidNonListedOffer = encodeVoidNonListedOffer;
exports.encodeVoidNonListedOfferBatch = encodeVoidNonListedOfferBatch;
exports.encodeGetOfferHash = encodeGetOfferHash;
exports.decodeGetOfferHash = decodeGetOfferHash;
const common_1 = require("@bosonprotocol/common");
const abi_1 = require("@ethersproject/abi");
const address_1 = require("@ethersproject/address");
const constants_1 = require("@ethersproject/constants");
const interface_1 = require("../exchanges/interface");
exports.bosonOfferHandlerIface = new abi_1.Interface(common_1.abis.IBosonOfferHandlerABI);
function encodeCreateOffer(args) {
    return exports.bosonOfferHandlerIface.encodeFunctionData("createOffer", createOfferArgsToStructs(args));
}
function encodeUpdateOfferRoyaltyRecipients(args) {
    return exports.bosonOfferHandlerIface.encodeFunctionData("updateOfferRoyaltyRecipients", [args.offerId, args.royaltyInfo]);
}
function encodeUpdateOfferRoyaltyRecipientsBatch(args) {
    return exports.bosonOfferHandlerIface.encodeFunctionData("updateOfferRoyaltyRecipientsBatch", [args.offerIds, args.royaltyInfo]);
}
function encodeCreateOfferBatch(argsBatch) {
    const argsTuples = argsBatch.map((args) => createOfferArgsToStructs(args));
    const [offers, offerDates, offerDurations, drParameters, agentIds, feeLimits] = argsTuples.reduce((acc, tuple) => {
        const [offer, offerDates, offerDurations, drParameters, agentId, feeLimit] = tuple;
        return [
            [...acc[0], offer],
            [...acc[1], offerDates],
            [...acc[2], offerDurations],
            [...acc[3], drParameters],
            [...acc[4], agentId],
            [...acc[5], feeLimit]
        ];
    }, [[], [], [], [], [], []]);
    return exports.bosonOfferHandlerIface.encodeFunctionData("createOfferBatch", [
        offers,
        offerDates,
        offerDurations,
        drParameters,
        agentIds,
        feeLimits
    ]);
}
function createOfferArgsToStructs(args) {
    const feeLimit = args.feeLimit !== undefined ? args.feeLimit : args.price;
    return [
        argsToOfferStruct(args),
        argsToOfferDatesStruct(args),
        argsToOfferDurationsStruct(args),
        argsToDRParametersStruct(args),
        args.agentId,
        feeLimit
    ];
}
function argsToOfferStruct(args) {
    const { exchangeToken } = args, restArgs = __rest(args, ["exchangeToken"]);
    const priceType = args.priceType !== undefined ? args.priceType : common_1.PriceType.Static;
    const creator = args.creator !== undefined ? args.creator : common_1.OfferCreator.Seller;
    const royaltyInfo = args.royaltyInfo !== undefined
        ? [
            {
                recipients: args.royaltyInfo.recipients.map((recipient) => (0, address_1.getAddress)(recipient)),
                bps: args.royaltyInfo.bps
            }
        ]
        : [
            {
                recipients: [],
                bps: []
            }
        ];
    return Object.assign(Object.assign({ id: "0", sellerId: "0", buyerId: "0", voided: false }, restArgs), { exchangeToken: (0, address_1.getAddress)(exchangeToken), priceType,
        royaltyInfo,
        creator });
}
function argsToOfferDatesStruct(args) {
    const { validFromDateInMS, validUntilDateInMS, voucherRedeemableFromDateInMS, voucherRedeemableUntilDateInMS } = args;
    return {
        validFrom: common_1.utils.timestamp.msToSec(validFromDateInMS),
        validUntil: common_1.utils.timestamp.msToSec(validUntilDateInMS),
        voucherRedeemableFrom: common_1.utils.timestamp.msToSec(voucherRedeemableFromDateInMS),
        voucherRedeemableUntil: common_1.utils.timestamp.msToSec(voucherRedeemableUntilDateInMS)
    };
}
function argsToOfferDurationsStruct(args) {
    const { disputePeriodDurationInMS, voucherValidDurationInMS = 0, resolutionPeriodDurationInMS } = args;
    return {
        disputePeriod: common_1.utils.timestamp.msToSec(disputePeriodDurationInMS),
        voucherValid: common_1.utils.timestamp.msToSec(voucherValidDurationInMS),
        resolutionPeriod: common_1.utils.timestamp.msToSec(resolutionPeriodDurationInMS)
    };
}
function argsToDRParametersStruct(args) {
    return {
        disputeResolverId: args.disputeResolverId,
        mutualizerAddress: args.mutualizerAddress || constants_1.AddressZero
    };
}
function encodeReserveRange(offerId, length, to) {
    return exports.bosonOfferHandlerIface.encodeFunctionData("reserveRange", [
        offerId,
        length,
        to
    ]);
}
function encodeVoidNonListedOffer(fullOfferArgsUnsigned) {
    return exports.bosonOfferHandlerIface.encodeFunctionData("voidNonListedOffer", [
        (0, interface_1.fullOfferArgsToStruct)(fullOfferArgsUnsigned)
    ]);
}
function encodeVoidNonListedOfferBatch(argsBatch) {
    const argsStructs = argsBatch.map((args) => (0, interface_1.fullOfferArgsToStruct)(args));
    return exports.bosonOfferHandlerIface.encodeFunctionData("voidNonListedOfferBatch", [
        argsStructs
    ]);
}
function encodeGetOfferHash(fullOfferArgsUnsigned) {
    return exports.bosonOfferHandlerIface.encodeFunctionData("getOfferHash", [
        (0, interface_1.fullOfferArgsToStruct)(fullOfferArgsUnsigned)
    ]);
}
function decodeGetOfferHash(result) {
    const [offerHash] = exports.bosonOfferHandlerIface.decodeFunctionResult("getOfferHash", result);
    return offerHash;
}
//# sourceMappingURL=interface.js.map