"use strict";
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const types_1 = require("../../types");
const util_1 = require("../util");
/**
 * @name bestNumberLag
 * @returns A number of blocks
 * @description Calculates the lag between finalized head and best head
 * @example
 * <BR>
 *
 * ```javascript
 * api.derive.chain.bestNumberLag((lag) => {
 *   console.log(`finalized is ${lag} blocks behind head`);
 * });
 * ```
 */
function bestNumberLag(api) {
    return util_1.memo(() => rxjs_1.combineLatest([
        api.derive.chain.bestNumber(),
        api.derive.chain.bestNumberFinalized()
    ]).pipe(operators_1.map(([bestNumber, bestNumberFinalized]) => types_1.createType(api.registry, 'BlockNumber', bestNumber.sub(bestNumberFinalized)))));
}
exports.bestNumberLag = bestNumberLag;
