"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.enrichRatesResponse = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const inferRatesError_1 = require("./inferRatesError");
function enrichRatesResponse(response, unitTo, unitFrom) {
    return response.map(r => {
        if (r.status === "success") {
            const payoutNetworkFees = r.providerType === "DEX" ? 0 : r.payoutNetworkFees;
            const rate = r.tradeMethod === "fixed"
                ? (0, bignumber_js_1.default)(r.rate)
                : (0, bignumber_js_1.default)(r.amountTo).minus(payoutNetworkFees).div(r.amountFrom);
            const magnitudeAwareToAmount = (0, bignumber_js_1.default)(r.amountTo)
                .minus(payoutNetworkFees)
                .times(new bignumber_js_1.default(10).pow(unitTo.magnitude));
            const magnitudeAwarePayoutNetworkFees = (0, bignumber_js_1.default)(payoutNetworkFees).times(new bignumber_js_1.default(10).pow(unitTo.magnitude));
            const magnitudeAwareRate = rate.div(new bignumber_js_1.default(10).pow(unitFrom.magnitude - unitTo.magnitude));
            return {
                ...(r.tradeMethod === "fixed" ? { expirationTime: Number(r.expirationTime) } : {}),
                magnitudeAwareRate,
                payoutNetworkFees: magnitudeAwarePayoutNetworkFees,
                provider: r.provider,
                providerType: r.providerType,
                providerURL: r.providerURL,
                rate,
                rateId: r.rateId,
                toAmount: magnitudeAwareToAmount,
                tradeMethod: r.tradeMethod,
            };
        }
        const error = r.status === "error" ? (0, inferRatesError_1.inferError)(r, unitFrom) : undefined;
        return {
            magnitudeAwareRate: (0, bignumber_js_1.default)(0),
            payoutNetworkFees: (0, bignumber_js_1.default)(0),
            provider: r.provider,
            providerType: r.providerType,
            rate: undefined,
            toAmount: (0, bignumber_js_1.default)(0),
            tradeMethod: r.tradeMethod,
            error,
        };
    });
}
exports.enrichRatesResponse = enrichRatesResponse;
//# sourceMappingURL=enrichRatesResponse.js.map