"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultSystemAPI = void 0;
class DefaultSystemAPI {
    constructor(api, transactionAPI) {
        this.api = api;
        this.transactionAPI = transactionAPI;
    }
    getCurrentBlockNumber() {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.api.query.system.number()).toNumber();
        });
    }
    getCurrentActiveBlockNumber(atBlock) {
        return __awaiter(this, void 0, void 0, function* () {
            const block = atBlock || (yield this.api.rpc.chain.getFinalizedHead());
            return (yield this.api.query.security.activeBlockCount.at(block)).toNumber();
        });
    }
    subscribeToFinalizedBlockHeads(callback) {
        return __awaiter(this, void 0, void 0, function* () {
            const unsub = yield this.api.rpc.chain.subscribeFinalizedHeads((head) => {
                callback(head);
            });
            return unsub;
        });
    }
    subscribeToCurrentBlockHeads(callback) {
        return __awaiter(this, void 0, void 0, function* () {
            const unsub = yield this.api.rpc.chain.subscribeAllHeads((head) => {
                callback(head);
            });
            return unsub;
        });
    }
    getStatusCode() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.api.query.security.parachainStatus();
        });
    }
    setCode(code) {
        return __awaiter(this, void 0, void 0, function* () {
            const tx = this.api.tx.sudo.sudoUncheckedWeight(this.api.tx.system.setCode(code), "0");
            yield this.transactionAPI.sendLogged(tx, this.api.events.system.CodeUpdated, true);
        });
    }
    getBlockHash(blockNumber) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.api.query.system.blockHash(blockNumber);
        });
    }
}
exports.DefaultSystemAPI = DefaultSystemAPI;
//# sourceMappingURL=system.js.map