// Copyright 2021-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { plainToInstance } from 'class-transformer';
import { ClientMethodHandler } from './client-method-handler';
import { AccountOutput, BasicOutput, FoundryOutput, NftOutput, parseBlock, parseUnsignedBlock, } from '../types/block';
import { UTXOInput, } from '../types';
import { OutputResponse, OutputWithMetadataResponse, } from '../types/models/api';
/** The Client to interact with nodes. */
var Client = /** @class */ (function () {
    /**
     * @param methodHandler The Rust method handler created in `ClientMethodHandler.create()`.
     */
    function Client(methodHandler) {
        this.methodHandler = methodHandler;
    }
    /**
     * @param options The client options.
     */
    Client.create = function (options) {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = Client.bind;
                        return [4 /*yield*/, ClientMethodHandler.create(options)];
                    case 1: return [2 /*return*/, new (_a.apply(Client, [void 0, _b.sent()]))()];
                }
            });
        });
    };
    Client.prototype.destroy = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.methodHandler.destroy()];
            });
        });
    };
    // Node routes.
    /**
     * Returns the health of the node.
     * GET /health
     */
    Client.prototype.getHealth = function (url) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getHealth',
                            data: {
                                url: url
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Returns the available API route groups of the node.
     * GET /api/routes
     */
    Client.prototype.getRoutes = function () {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getRoutes'
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the node information together with the url of the used node.
     */
    Client.prototype.getNodeInfo = function () {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getNodeInfo'
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Returns general information about the node.
     * GET /api/core/v3/info
     *
     * @param url The URL of the node.
     * @param auth An authentication object (e.g. JWT).
     */
    Client.prototype.getInfo = function (url, auth) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getInfo',
                            data: {
                                url: url,
                                auth: auth
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the network metrics.
     */
    Client.prototype.getNetworkMetrics = function () {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getNetworkMetrics'
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    // Accounts routes.
    /**
     * Checks if the account is ready to issue a block.
     * GET /api/core/v3/accounts/{bech32Address}/congestion
     */
    Client.prototype.getAccountCongestion = function (accountId, workScore) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getAccountCongestion',
                            data: {
                                accountId: accountId,
                                workScore: workScore
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    // Rewards routes.
    /**
     * Returns the total available Mana rewards of an account or delegation output decayed up to `epochEnd` index
     * provided in the response.
     * Note that rewards for an epoch only become available at the beginning of the next epoch. If the end epoch of a
     * staking feature is equal or greater than the current epoch, the rewards response will not include the potential
     * future rewards for those epochs. `epochStart` and `epochEnd` indicates the actual range for which reward value
     * is returned and decayed for.
     * GET /api/core/v3/rewards/{outputId}
     */
    Client.prototype.getOutputManaRewards = function (outputId, slotIndex) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getOutputManaRewards',
                            data: {
                                outputId: outputId,
                                slotIndex: slotIndex
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    // Validators routes.
    /**
     * Returns information of all registered validators and if they are active, ordered by their holding stake.
     * GET /api/core/v3/validators
     */
    Client.prototype.getValidators = function (pageSize, cursor) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getValidators',
                            data: {
                                pageSize: pageSize,
                                cursor: cursor
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Return information about a staker (registered validator).
     * GET /api/core/v3/validators/{bech32Address}
     */
    Client.prototype.getValidator = function (accountId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getValidator',
                            data: {
                                accountId: accountId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    // Committee routes.
    /**
     * Returns the information of committee members at the given epoch index. If epoch index is not provided, the
     * current committee members are returned.
     * GET /api/core/v3/committee/?epoch
     */
    Client.prototype.getCommittee = function (epoch) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getCommittee',
                            data: {
                                epoch: epoch
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    // Blocks routes.
    /**
     * Returns information that is ideal for attaching a block in the network.
     * GET /api/core/v3/blocks/issuance
     */
    Client.prototype.getIssuance = function () {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getIssuance'
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Returns the BlockId of the submitted block.
     * POST /api/core/v3/blocks
     *
     * @param block The block to post.
     * @returns The block ID once the block has been posted.
     */
    Client.prototype.postBlock = function (block) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'postBlock',
                            data: {
                                block: block
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Returns the BlockId of the submitted block.
     * POST /api/core/v3/blocks
     *
     * @param blockBytes The block as raw bytes.
     * @returns The ID of the posted block.
     */
    Client.prototype.postBlockRaw = function (blockBytes) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'postBlockRaw',
                            data: {
                                blockBytes: blockBytes
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Finds a block by its ID and returns it as object.
     * GET /api/core/v3/blocks/{blockId}
     *
     * @param blockId The corresponding block ID of the requested block.
     * @returns The requested block.
     */
    Client.prototype.getBlock = function (blockId) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getBlock',
                            data: {
                                blockId: blockId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, parseBlock(parsed.payload)];
                }
            });
        });
    };
    /**
     * Finds a block by its ID and returns it as raw bytes.
     * GET /api/core/v3/blocks/{blockId}
     *
     * @param blockId The block ID of the requested block.
     * @returns The raw bytes of the requested block.
     */
    Client.prototype.getBlockRaw = function (blockId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getBlockRaw',
                            data: {
                                blockId: blockId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Returns the metadata of a block.
     * GET /api/core/v3/blocks/{blockId}/metadata
     *
     * @param blockId The corresponding block ID of the requested block metadata.
     * @returns The requested block metadata.
     */
    Client.prototype.getBlockMetadata = function (blockId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getBlockMetadata',
                            data: {
                                blockId: blockId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Returns a block with its metadata.
     * GET /api/core/v2/blocks/{blockId}/full
     *
     * @param blockId The corresponding block ID of the requested block.
     * @returns The requested block with its metadata.
     */
    Client.prototype.getBlockWithMetadata = function (blockId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getBlockWithMetadata',
                            data: {
                                blockId: blockId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    // UTXO routes.
    /**
     * Finds an output by its ID and returns it as object.
     * GET /api/core/v3/outputs/{outputId}
     */
    Client.prototype.getOutput = function (outputId) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getOutput',
                            data: {
                                outputId: outputId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, plainToInstance(OutputResponse, parsed.payload)];
                }
            });
        });
    };
    /**
     * Finds an output by its ID and returns it as raw bytes.
     * GET /api/core/v3/outputs/{outputId}
     */
    Client.prototype.getOutputRaw = function (outputId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getOutputRaw',
                            data: {
                                outputId: outputId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Finds output metadata by output ID.
     * GET /api/core/v3/outputs/{outputId}/metadata
     */
    Client.prototype.getOutputMetadata = function (outputId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getOutputMetadata',
                            data: {
                                outputId: outputId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Finds an output with its metadata by output ID.
     * GET /api/core/v3/outputs/{outputId}/full
     */
    Client.prototype.getOutputWithMetadata = function (outputId) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getOutputWithMetadata',
                            data: {
                                outputId: outputId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, plainToInstance(OutputWithMetadataResponse, parsed.payload)];
                }
            });
        });
    };
    /**
     * Returns the earliest confirmed block containing the transaction with the given ID.
     * GET /api/core/v3/transactions/{transactionId}/included-block
     *
     * @param transactionId The ID of the transaction.
     * @returns The included block that contained the transaction.
     */
    Client.prototype.getIncludedBlock = function (transactionId) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getIncludedBlock',
                            data: {
                                transactionId: transactionId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, parseBlock(parsed.payload)];
                }
            });
        });
    };
    /**
     * Returns the earliest confirmed block containing the transaction with the given ID, as raw bytes.
     * GET /api/core/v3/transactions/{transactionId}/included-block
     *
     * @param transactionId The ID of the transaction.
     * @returns The included block that contained the transaction.
     */
    Client.prototype.getIncludedBlockRaw = function (transactionId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getIncludedBlockRaw',
                            data: {
                                transactionId: transactionId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Returns the metadata of the earliest block containing the tx that was confirmed.
     * GET /api/core/v3/transactions/{transactionId}/included-block/metadata
     *
     * @param transactionId The ID of the transaction.
     * @returns The included block that contained the transaction.
     */
    Client.prototype.getIncludedBlockMetadata = function (transactionId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getIncludedBlockMetadata',
                            data: {
                                transactionId: transactionId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Finds the metadata of a transaction.
     * GET /api/core/v3/transactions/{transactionId}/metadata
     *
     * @param transactionId The ID of the transaction.
     * @returns The transaction metadata.
     */
    Client.prototype.getTransactionMetadata = function (transactionId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getTransactionMetadata',
                            data: {
                                transactionId: transactionId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    // Commitments routes.
    /**
     * Finds a slot commitment by its ID and returns it as object.
     * GET /api/core/v3/commitments/{commitmentId}
     *
     * @param commitmentId Commitment ID of the commitment to look up.
     * @returns The commitment.
     */
    Client.prototype.getCommitment = function (commitmentId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getCommitment',
                            data: {
                                commitmentId: commitmentId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Finds a slot commitment by its ID and returns it as raw bytes.
     * GET /api/core/v3/commitments/{commitmentId}
     *
     * @param commitmentId Commitment ID of the commitment to look up.
     * @returns The commitment as raw bytes.
     */
    Client.prototype.getCommitmentRaw = function (commitmentId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getCommitmentRaw',
                            data: {
                                commitmentId: commitmentId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get all UTXO changes of a given slot by slot commitment ID.
     * GET /api/core/v3/commitments/{commitmentId}/utxo-changes
     *
     * @param commitmentId Commitment ID of the commitment to look up.
     * @returns The UTXO changes.
     */
    Client.prototype.getUtxoChanges = function (commitmentId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getUtxoChanges',
                            data: {
                                commitmentId: commitmentId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get all full UTXO changes of a given slot by slot commitment ID.
     * GET /api/core/v3/commitments/{commitmentId}/utxo-changes/full
     *
     * @param commitmentId Commitment ID of the commitment to look up.
     * @returns The UTXO changes.
     */
    Client.prototype.getUtxoChangesFull = function (commitmentId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getUtxoChangesFull',
                            data: {
                                commitmentId: commitmentId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Finds a slot commitment by slot index and returns it as object.
     * GET /api/core/v3/commitments/by-slot/{slot}
     *
     * @param slot Index of the commitment to look up.
     * @returns The commitment.
     */
    Client.prototype.getCommitmentBySlot = function (slot) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getCommitmentBySlot',
                            data: {
                                slot: slot
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Finds a slot commitment by slot index and returns it as raw bytes.
     * GET /api/core/v3/commitments/by-slot/{slot}
     *
     * @param slot Index of the commitment to look up.
     * @returns The commitment as raw bytes.
     */
    Client.prototype.getCommitmentBySlotRaw = function (slot) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getCommitmentBySlotRaw',
                            data: {
                                slot: slot
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get all UTXO changes of a given slot by its index.
     * GET /api/core/v3/commitments/by-slot/{slot}/utxo-changes
     *
     * @param slot Index of the commitment to look up.
     * @returns The UTXO changes.
     */
    Client.prototype.getUtxoChangesBySlot = function (slot) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getUtxoChangesBySlot',
                            data: {
                                slot: slot
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get all full UTXO changes of a given slot by its index.
     * GET /api/core/v3/commitments/by-slot/{slot}/utxo-changes/full
     *
     * @param slot Index of the commitment to look up.
     * @returns The UTXO changes.
     */
    Client.prototype.getUtxoChangesFullBySlot = function (slot) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getUtxoChangesFullBySlot',
                            data: {
                                slot: slot
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    // High level API routes.
    /**
     * Fetch OutputResponse from given output IDs. Requests are sent in parallel.
     */
    Client.prototype.getOutputs = function (outputIds) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getOutputs',
                            data: {
                                outputIds: outputIds
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, plainToInstance(OutputResponse, parsed.payload)];
                }
            });
        });
    };
    /**
     * Get outputs from provided output IDs (requests are sent
     * in parallel and errors are ignored, can be useful for spent outputs)
     *
     * @param outputIds An array of output IDs.
     * @returns An array of corresponding output responses.
     */
    Client.prototype.getOutputsIgnoreNotFound = function (outputIds) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getOutputsIgnoreNotFound',
                            data: {
                                outputIds: outputIds
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, plainToInstance(OutputResponse, parsed.payload)];
                }
            });
        });
    };
    /**
     * Find blocks by their IDs.
     *
     * @param blockIds An array of `BlockId`s.
     * @returns An array of corresponding blocks.
     */
    Client.prototype.findBlocks = function (blockIds) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'findBlocks',
                            data: {
                                blockIds: blockIds
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, parsed.payload.map(function (p) { return parseBlock(p); })];
                }
            });
        });
    };
    /**
     * Find inputs from addresses for a given amount (useful for offline signing).
     *
     * @param addresses A list of included addresses.
     * @param amount The amount to find inputs for.
     * @returns An array of UTXO inputs.
     */
    Client.prototype.findInputs = function (addresses, amount) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'findInputs',
                            data: {
                                addresses: addresses,
                                amount: Number(amount)
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, plainToInstance(UTXOInput, parsed.payload)];
                }
            });
        });
    };
    // Other routes.
    /**
     * Build an unsigned block.
     *
     * @param issuerId The identifier of the block issuer account.
     * @param payload The payload to post.
     * @returns The block ID followed by the block containing the payload.
     */
    Client.prototype.buildBasicBlock = function (issuerId, payload) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'buildBasicBlock',
                            data: {
                                issuerId: issuerId,
                                payload: payload
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, parseUnsignedBlock(parsed.payload)];
                }
            });
        });
    };
    /**
     * Get a node candidate from the healthy node pool.
     */
    Client.prototype.getNode = function () {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getNode'
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the ID of the network the node is connected to.
     */
    Client.prototype.getNetworkId = function () {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getNetworkId'
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the Bech32 HRP (human readable part) of the network the node is connected to.
     */
    Client.prototype.getBech32Hrp = function () {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getBech32Hrp'
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the token supply.
     */
    Client.prototype.getTokenSupply = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = BigInt;
                        return [4 /*yield*/, this.getProtocolParameters()];
                    case 1: return [2 /*return*/, _a.apply(void 0, [(_b.sent()).tokenSupply])];
                }
            });
        });
    };
    /**
     * Get the protocol parameters.
     */
    Client.prototype.getProtocolParameters = function () {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'getProtocolParameters'
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Converts an address to its bech32 representation
     *
     * @param address An address.
     * @param bech32Hrp The Bech32 HRP (human readable part) to be used.
     * @returns The corresponding Bech32 address.
     */
    Client.prototype.addressToBech32 = function (address, bech32Hrp) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'addressToBech32',
                            data: {
                                address: address,
                                bech32Hrp: bech32Hrp
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Return the unhealthy nodes.
     */
    Client.prototype.unhealthyNodes = function () {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'unhealthyNodes'
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Build a basic output.
     *
     * @param params An instance of `BasicOutputBuilderParams`.
     */
    Client.prototype.buildBasicOutput = function (params) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (params.amount && typeof params.amount === 'bigint') {
                            params.amount = params.amount.toString(10);
                        }
                        return [4 /*yield*/, this.methodHandler.callMethod({
                                name: 'buildBasicOutput',
                                data: params
                            })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, plainToInstance(BasicOutput, parsed.payload)];
                }
            });
        });
    };
    /**
     * Build an account output.
     *
     * @param params An instance of `AccountOutputBuilderParams`.
     */
    Client.prototype.buildAccountOutput = function (params) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (params.amount && typeof params.amount === 'bigint') {
                            params.amount = params.amount.toString(10);
                        }
                        return [4 /*yield*/, this.methodHandler.callMethod({
                                name: 'buildAccountOutput',
                                data: params
                            })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, plainToInstance(AccountOutput, parsed.payload)];
                }
            });
        });
    };
    /**
     * Build a foundry output.
     *
     * @param params An instance of `FoundryOutputBuilderParams`.
     */
    Client.prototype.buildFoundryOutput = function (params) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (params.amount && typeof params.amount === 'bigint') {
                            params.amount = params.amount.toString(10);
                        }
                        return [4 /*yield*/, this.methodHandler.callMethod({
                                name: 'buildFoundryOutput',
                                data: params
                            })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, plainToInstance(FoundryOutput, parsed.payload)];
                }
            });
        });
    };
    /**
     * Build an NFT output.
     *
     * @param params An instance of `NftOutputBuilderParams`.
     */
    Client.prototype.buildNftOutput = function (params) {
        return __awaiter(this, void 0, void 0, function () {
            var response, parsed;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (params.amount && typeof params.amount === 'bigint') {
                            params.amount = params.amount.toString(10);
                        }
                        return [4 /*yield*/, this.methodHandler.callMethod({
                                name: 'buildNftOutput',
                                data: params
                            })];
                    case 1:
                        response = _a.sent();
                        parsed = JSON.parse(response);
                        return [2 /*return*/, plainToInstance(NftOutput, parsed.payload)];
                }
            });
        });
    };
    /**
     * Listen to MQTT events.
     *
     * @param topics An array of MQTT topics to listen to.
     */
    Client.prototype.listenMqtt = function (topics, callback) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.methodHandler.listenMqtt(topics, callback)];
            });
        });
    };
    /**
     * Stop listening to certain MQTT events.
     *
     * @param topics An array of MQTT topics to stop listening to.
     */
    Client.prototype.clearMqttListeners = function (topics) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'clearListeners',
                            data: {
                                topics: topics
                            }
                        })];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    /**
     * Calculate the minimum required amount for an output.
     *
     * @param output The output to calculate the minimum amount for.
     * @returns The minimum required amount.
     */
    Client.prototype.computeMinimumOutputAmount = function (output) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'computeMinimumOutputAmount',
                            data: {
                                output: output
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Request funds from a faucet.
     *
     * Example URLs: `https://faucet.testnet.shimmer.network/api/enqueue` or `http://localhost:8091/api/enqueue`.
     *
     * @param url The URL of the faucet.
     * @param address The address to send the funds to.
     * @returns The faucet response.
     */
    Client.prototype.requestFundsFromFaucet = function (url, address) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'requestFundsFromFaucet',
                            data: { url: url, address: address }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Extension method which provides request methods for plugins.
     *
     * @param basePluginPath The base path for the plugin eg indexer/v2/ .
     * @param method The http method.
     * @param endpoint The path for the plugin request.
     * @param queryParams Additional query params for the request.
     * @param request The request object.
     * @returns The response json.
     */
    Client.prototype.callPluginRoute = function (basePluginPath, method, endpoint, queryParams, request) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'callPluginRoute',
                            data: {
                                basePluginPath: basePluginPath,
                                method: method,
                                endpoint: endpoint,
                                queryParams: queryParams !== null && queryParams !== void 0 ? queryParams : [],
                                request: request
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    // inx-indexer routes
    /**
     * Fetch account/anchor/basic/delegation/NFT/foundry output IDs based on the given query parameters.
     */
    Client.prototype.outputIds = function (queryParameters) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'outputIds',
                            data: {
                                queryParameters: queryParameters
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Fetch basic output IDs based on the given query parameters.
     */
    Client.prototype.basicOutputIds = function (queryParameters) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'basicOutputIds',
                            data: {
                                queryParameters: queryParameters
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output IDs given a list of account query parameters.
     *
     * @param outputQueryParameters `AccountOutputQueryParameters`.
     * @returns A paginated query response of corresponding output IDs.
     */
    Client.prototype.accountOutputIds = function (queryParameters) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'accountOutputIds',
                            data: {
                                queryParameters: queryParameters
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output ID from an account ID.
     *
     * @param accountId An account ID.
     * @returns The corresponding output ID.
     */
    Client.prototype.accountOutputId = function (accountId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'accountOutputId',
                            data: {
                                accountId: accountId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output IDs given a list of anchor query parameters.
     *
     * @param outputQueryParameters `AnchorOutputQueryParameters`.
     * @returns A paginated query response of corresponding output IDs.
     */
    Client.prototype.anchorOutputIds = function (queryParameters) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'anchorOutputIds',
                            data: {
                                queryParameters: queryParameters
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output ID from an anchor ID.
     *
     * @param anchorId An anchor ID.
     * @returns The corresponding output ID.
     */
    Client.prototype.anchorOutputId = function (anchorId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'anchorOutputId',
                            data: {
                                anchorId: anchorId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output IDs given a list of delegation query parameters.
     *
     * @param outputQueryParameters `DelegationOutputQueryParameters`.
     * @returns A paginated query response of corresponding output IDs.
     */
    Client.prototype.delegationOutputIds = function (queryParameters) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'delegationOutputIds',
                            data: {
                                queryParameters: queryParameters
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output ID from an delegation ID.
     *
     * @param delegationId A delegation ID.
     * @returns The corresponding output ID.
     */
    Client.prototype.delegationOutputId = function (delegationId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'delegationOutputId',
                            data: {
                                delegationId: delegationId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output IDs given a list of Foundry query parameters.
     *
     * @param outputQueryParameters `FoundryOutputQueryParameters`.
     * @returns A paginated query response of corresponding output IDs.
     */
    Client.prototype.foundryOutputIds = function (queryParameters) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'foundryOutputIds',
                            data: {
                                queryParameters: queryParameters
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output ID from a Foundry ID.
     *
     * @param foundryId A Foundry ID.
     * @returns The corresponding output ID.
     */
    Client.prototype.foundryOutputId = function (foundryId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'foundryOutputId',
                            data: {
                                foundryId: foundryId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output IDs given a list of NFT query parameters.
     *
     * @param outputQueryParameters `NftOutputQueryParameters`.
     * @returns A paginated query response of corresponding output IDs.
     */
    Client.prototype.nftOutputIds = function (queryParameters) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'nftOutputIds',
                            data: {
                                queryParameters: queryParameters
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Get the corresponding output ID from an NFT ID.
     *
     * @param nftId An NFT ID.
     * @returns The corresponding output ID.
     */
    Client.prototype.nftOutputId = function (nftId) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'nftOutputId',
                            data: {
                                nftId: nftId
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    /**
     * Compute the block ID (Blake2b256 hash of the block bytes) of a block.
     *
     * @param block A block.
     * @returns The corresponding block ID.
     */
    Client.prototype.blockId = function (block) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.methodHandler.callMethod({
                            name: 'blockId',
                            data: {
                                block: block
                            }
                        })];
                    case 1:
                        response = _a.sent();
                        return [2 /*return*/, JSON.parse(response).payload];
                }
            });
        });
    };
    return Client;
}());
export { Client };
//# sourceMappingURL=client.js.map