"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.bestNumberLag = bestNumberLag;

var _rxjs = require("rxjs");

var _operators = require("rxjs/operators");

var _types = require("@polkadot/types");

var _drr = require("../util/drr");

var _bestNumber = require("./bestNumber");

var _bestNumberFinalized = require("./bestNumberFinalized");

// Copyright 2017-2019 @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.

/**
 * @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 () => (0, _rxjs.combineLatest)([(0, _bestNumber.bestNumber)(api)(), (0, _bestNumberFinalized.bestNumberFinalized)(api)()]).pipe((0, _operators.map)((_ref) => {
    let [bestNumber, bestNumberFinalized] = _ref;
    return (0, _types.createType)('BlockNumber', bestNumber.sub(bestNumberFinalized));
  }), (0, _drr.drr)());
}