"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.displayIndexerAllocation = exports.displayIndexerAllocations = exports.printIndexerAllocations = exports.indexerAllocationFromGraphQL = exports.formatIndexerAllocation = void 0;
const common_ts_1 = require("@graphprotocol/common-ts");
const yaml_1 = __importDefault(require("yaml"));
const table_1 = require("table");
const ethers_1 = require("ethers");
const command_helpers_1 = require("./command-helpers");
const ALLOCATION_CONVERTERS_FROM_GRAPHQL = {
    id: x => x,
    subgraphDeployment: (d) => typeof d === 'string' ? d : d.ipfsHash,
    allocatedTokens: nullPassThrough((x) => ethers_1.BigNumber.from(x)),
    createdAtEpoch: nullPassThrough((x) => parseInt(x)),
    closedAtEpoch: nullPassThrough((x) => parseInt(x)),
    signalAmount: nullPassThrough((x) => ethers_1.BigNumber.from(x)),
    stakedTokens: nullPassThrough((x) => ethers_1.BigNumber.from(x)),
};
const ALLOCATION_FORMATTERS = {
    id: nullPassThrough(x => x),
    subgraphDeployment: (d) => typeof d === 'string' ? d : d.ipfsHash,
    allocatedTokens: x => ethers_1.utils.commify(common_ts_1.formatGRT(x)),
    createdAtEpoch: x => x,
    closedAtEpoch: x => x,
    signalAmount: x => ethers_1.utils.commify(common_ts_1.formatGRT(x)),
    stakedTokens: x => ethers_1.utils.commify(common_ts_1.formatGRT(x)),
};
/**
 * Formats an indexer allocation for display in the console.
 */
exports.formatIndexerAllocation = (allocation) => {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const obj = {};
    for (const [key, value] of Object.entries(allocation)) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        obj[key] = ALLOCATION_FORMATTERS[key](value);
    }
    return obj;
};
/**
 * Parses an indexer allocation returned from the indexer management GraphQL
 * API into normalized form.
 */
exports.indexerAllocationFromGraphQL = (allocation) => {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const obj = {};
    for (const [key, value] of Object.entries(command_helpers_1.pickFields(allocation, []))) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        obj[key] = ALLOCATION_CONVERTERS_FROM_GRAPHQL[key](value);
    }
    return obj;
};
exports.printIndexerAllocations = (print, outputFormat, allocationOrAllocations, keys) => {
    if (Array.isArray(allocationOrAllocations)) {
        const allocations = allocationOrAllocations.map(allocation => exports.formatIndexerAllocation(command_helpers_1.pickFields(allocation, keys)));
        print.info(exports.displayIndexerAllocations(outputFormat, allocations));
    }
    else if (allocationOrAllocations) {
        const allocation = exports.formatIndexerAllocation(command_helpers_1.pickFields(allocationOrAllocations, keys));
        print.info(exports.displayIndexerAllocation(outputFormat, allocation));
    }
    else {
        print.error(`No allocations found`);
    }
};
exports.displayIndexerAllocations = (outputFormat, allocations) => outputFormat === 'json'
    ? JSON.stringify(allocations, null, 2)
    : outputFormat === 'yaml'
        ? yaml_1.default.stringify(allocations).trim()
        : allocations.length === 0
            ? 'No data'
            : table_1.table([
                Object.keys(allocations[0]),
                ...allocations.map(allocation => Object.values(allocation)),
            ], {
                border: table_1.getBorderCharacters('norc'),
            }).trim();
exports.displayIndexerAllocation = (outputFormat, allocation) => outputFormat === 'json'
    ? JSON.stringify(allocation, null, 2)
    : outputFormat === 'yaml'
        ? yaml_1.default.stringify(allocation).trim()
        : table_1.table([Object.keys(allocation), Object.values(allocation)], {
            border: table_1.getBorderCharacters('norc'),
        }).trim();
function nullPassThrough(fn) {
    return (x) => (x === null ? null : fn(x));
}
