"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createApiRx = exports.createApi = void 0;
const api_1 = require("@polkadot/api");
const rpc_provider_1 = require("@polkadot/rpc-provider");
const operators_1 = require("rxjs/operators");
const defs_1 = __importStar(require("./defs"));
const derives_1 = __importDefault(require("./derives"));
const query_1 = __importDefault(require("./mappers/query"));
const tx_1 = __importDefault(require("./mappers/tx"));
const rpc_1 = __importDefault(require("./rpc"));
const consts_1 = __importDefault(require("./mappers/consts"));
const getOptions = (nodes, options, specName) => {
    const provider = new rpc_provider_1.WsProvider(nodes);
    return {
        ...(0, defs_1.default)(specName),
        derives: derives_1.default,
        provider,
        ...options,
    };
};
const createApi = async (...params) => {
    const opt = getOptions(...params);
    const api = await api_1.ApiPromise.create(opt);
    const rpc = { ...api.rpc, ...(0, rpc_1.default)(api) };
    const query = (0, query_1.default)(api);
    const tx = (0, tx_1.default)(api);
    const consts = (0, consts_1.default)(api);
    return {
        /** @deprecated */
        getAccounts: async () => (await api.query.system.account.keys()).map((key) => key.args[0].toHuman()),
        /** @deprecated */
        getBlock: api.rpc.chain.getBlock,
        /** @deprecated */
        getBlockHash: api.rpc.chain.getBlockHash,
        /** @deprecated */
        getError: (...args) => api.registry.findMetaError(...args),
        /** @deprecated */
        getNonce: api.rpc.system.accountNextIndex,
        /** @deprecated */
        subscribeNewBlockHeads: api.rpc.chain.subscribeNewHeads,
        /** @deprecated */
        batch: api.tx.utility?.batch,
        /** @deprecated */
        sudo: api.tx.sudo?.sudo,
        /** @deprecated */
        _api: api,
        _type: defs_1.chainType,
        derive: api.derive,
        events: api.events,
        multi: api.queryMulti,
        onError: (cb) => opt.provider.on("error", cb),
        query,
        registry: api.registry,
        rpc,
        consts,
        setSigner: (signer) => api.setSigner(signer),
        tx,
    };
};
exports.createApi = createApi;
const createApiRx = (...params) => {
    const opt = getOptions(...params);
    return api_1.ApiRx.create(opt).pipe((0, operators_1.map)((api) => {
        const rpc = { ...api.rpc, ...(0, rpc_1.default)(api) };
        const query = (0, query_1.default)(api);
        const tx = (0, tx_1.default)(api);
        const consts = (0, consts_1.default)(api);
        return {
            /** @deprecated */
            getAccounts: api.query.system.account
                .keys()
                .pipe((0, operators_1.map)((keys) => keys.map((key) => key.args[0].toHuman()))),
            /** @deprecated */
            getBlock: api.rpc.chain.getBlock,
            /** @deprecated */
            getBlockHash: api.rpc.chain.getBlockHash,
            /** @deprecated */
            getError: (...args) => api.registry.findMetaError(...args),
            /** @deprecated */
            getNonce: api.rpc.system.accountNextIndex,
            /** @deprecated */
            subscribeNewBlockHeads: api.rpc.chain.subscribeNewHeads,
            /** @deprecated */
            batch: api.tx.utility.batch,
            /** @deprecated */
            _api: api,
            _type: defs_1.chainType,
            derive: api.derive,
            events: api.events,
            multi: api.queryMulti,
            onError: (cb) => opt.provider.on("error", cb),
            query,
            registry: api.registry,
            rpc,
            consts,
            setSigner: (signer) => api.setSigner(signer),
            tx,
        };
    }));
};
exports.createApiRx = createApiRx;
