"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.orderHydrate = orderHydrate;
exports.constructOrderFromOrderApi = constructOrderFromOrderApi;
exports.parseInputAddresses = parseInputAddresses;
exports.parseOutputAddresses = parseOutputAddresses;
const ethers_1 = require("ethers");
const logger_1 = require("./logger");
async function orderHydrate(pricerApi, orderId) {
    try {
        return await pricerApi.fetchOrder(orderId);
    }
    catch (error) {
        logger_1.logger.error({ error: error.message, id: orderId }, '🚨 Could not hydrate order');
    }
    return null;
}
function constructOrderFromOrderApi(order) {
    if (order.sourceAsset === ethers_1.ethers.constants.AddressZero && order.destinationAsset === ethers_1.ethers.constants.AddressZero) {
        order.assetNative = true;
    }
    else {
        order.assetNative = false;
    }
    return {
        ...order,
        amount: ethers_1.BigNumber.from(order.amount),
        insurance: ethers_1.BigNumber.from(order.insurance),
        maxReward: ethers_1.BigNumber.from(order.maxReward),
        profit: ethers_1.BigNumber.from(order.profit),
    };
}
/**
 * Transforms any sourceAccount, targetAccount, or executor addresses from input object to lowercase.
 */
function parseInputAddresses(input) {
    if (Array.isArray(input)) {
        return input.map((item) => parseInputAddresses(item));
    }
    if (typeof input === 'object' && input !== null) {
        for (const key in input) {
            if (!Object.prototype.hasOwnProperty.call(input, key))
                continue;
            if (['sourceAccount', 'targetAccount', 'executor', 'executorId', 'account'].includes(key)) {
                if (typeof input[key] !== 'string') {
                    throw new TypeError(`Invalid type for address ${input[key]}`);
                }
                input[key] = input[key].toLowerCase();
            }
            else {
                input[key] = parseInputAddresses(input[key]);
            }
        }
    }
    return input;
}
/**
 * Transforms any sourceAccount, targetAccount, or executor addresses from input object to valid ethers addresses.
 */
function parseOutputAddresses(input) {
    if (Array.isArray(input)) {
        return input.map((item) => parseOutputAddresses(item));
    }
    if (typeof input === 'object' && input !== null) {
        for (const key in input) {
            if (!Object.prototype.hasOwnProperty.call(input, key))
                continue;
            if (['sourceAccount', 'targetAccount', 'executor', 'executorId', 'account', 'address'].includes(key)) {
                input[key] = getAddress(input[key]);
            }
            else {
                input[key] = parseOutputAddresses(input[key]);
            }
        }
    }
    return input;
}
function getAddress(address) {
    if (typeof address !== 'string') {
        throw new TypeError(`Invalid type for address ${address}`);
    }
    // Avoid running getAddress on empty string
    if (address === '') {
        return address;
    }
    return ethers_1.ethers.utils.getAddress(address);
}
//# sourceMappingURL=order.js.map