"use strict";
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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Middleware = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const queries_1 = require("./middleware/queries");
const conversion_1 = require("./utils/conversion");
const internal_1 = require("./utils/internal");
/**
 * Handles all Middleware related functionality
 */
class Middleware {
    /**
     * @hidden
     */
    constructor(context) {
        this.context = context;
    }
    /**
     * Retrieve a single event by any of its indexed arguments. Can be filtered using parameters
     *
     * @param opts.moduleId - type of the module to fetch
     * @param opts.eventId - type of the event to fetch
     * @param opts.eventArg0 - event parameter value to filter by in position 0
     * @param opts.eventArg1 - event parameter value to filter by in position 1
     * @param opts.eventArg2 - event parameter value to filter by in position 2
     *
     * @note uses the middleware
     */
    getEventByIndexedArgs(opts) {
        return __awaiter(this, void 0, void 0, function* () {
            const { context } = this;
            const { moduleId, eventId, eventArg0, eventArg1, eventArg2 } = opts;
            const { data: { eventByIndexedArgs: event }, } = yield context.queryMiddleware(queries_1.eventByIndexedArgs({
                moduleId,
                eventId,
                eventArg0,
                eventArg1,
                eventArg2,
            }));
            return internal_1.optionize(conversion_1.middlewareEventToEventIdentifier)(event);
        });
    }
    /**
     * Retrieve a list of events. Can be filtered using parameters
     *
     * @param opts.moduleId - type of the module to fetch
     * @param opts.eventId - type of the event to fetch
     * @param opts.eventArg0 - event parameter value to filter by in position 0
     * @param opts.eventArg1 - event parameter value to filter by in position 1
     * @param opts.eventArg2 - event parameter value to filter by in position 2
     * @param opts.size - page size
     * @param opts.start - page offset
     *
     * @note uses the middleware
     */
    getEventsByIndexedArgs(opts) {
        return __awaiter(this, void 0, void 0, function* () {
            const { context } = this;
            const { moduleId, eventId, eventArg0, eventArg1, eventArg2, size, start } = opts;
            const result = yield context.queryMiddleware(queries_1.eventsByIndexedArgs({
                moduleId,
                eventId,
                eventArg0,
                eventArg1,
                eventArg2,
                count: size,
                skip: start,
            }));
            const { data: { eventsByIndexedArgs: events }, } = result;
            if (events) {
                // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                return events.map(event => conversion_1.middlewareEventToEventIdentifier(event));
            }
            return null;
        });
    }
    /**
     * Retrieve a transaction by hash
     *
     * @param opts.txHash - hash of the transaction
     *
     * @note uses the middleware
     */
    getTransactionByHash(opts) {
        return __awaiter(this, void 0, void 0, function* () {
            const { context } = this;
            const { txHash: transactionHash } = opts;
            const result = yield context.queryMiddleware(queries_1.transactionByHash({
                transactionHash,
            }));
            const { data: { transactionByHash: transaction }, } = result;
            if (transaction) {
                const { block_id: blockNumber, extrinsic_idx: extrinsicIdx, address: rawAddress, nonce, module_id: moduleId, call_id: callId, params, success: txSuccess, spec_version_id: specVersionId, extrinsic_hash: extrinsicHash, } = transaction;
                return {
                    blockNumber: new bignumber_js_1.default(blockNumber),
                    extrinsicIdx,
                    address: rawAddress !== null && rawAddress !== void 0 ? rawAddress : null,
                    /* eslint-disable @typescript-eslint/no-non-null-assertion */
                    nonce: nonce,
                    txTag: conversion_1.extrinsicIdentifierToTxTag({ moduleId, callId }),
                    params,
                    success: !!txSuccess,
                    specVersionId,
                    extrinsicHash: extrinsicHash,
                    /* eslint-enable @typescript-eslint/no-non-null-assertion */
                };
            }
            return null;
        });
    }
}
exports.Middleware = Middleware;
//# sourceMappingURL=Middleware.js.map