"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 type_1 = require("../type");
const util_1 = require("../util");
/**
 * @name bestNumberFinalized
 * @param {( Uint8Array | string )} hash - A block hash as U8 array or string.
 * @returns An array containing the block header and the block author
 * @description Get a specific block header and extend it with the author
 * @example
 * <BR>
 *
 * ```javascript
 * const { author, number } = await api.derive.chain.getHeader('0x123...456');
 *
 * console.log(`block #${number} was authored by ${author}`);
 * ```
 */
function getHeader(api) {
    return util_1.memo((hash) => rxjs_1.combineLatest([
        api.rpc.chain.getHeader(hash),
        api.query.session
            ? api.query.session.validators.at(hash)
            : rxjs_1.of([])
    ]).pipe(operators_1.map(([header, validators]) => new type_1.HeaderExtended(api.registry, header, validators)), operators_1.catchError(() => 
    // where rpc.chain.getHeader throws, we will land here - it can happen that
    // we supplied an invalid hash. (Due to defaults, storeage will have an
    // empty value, so only the RPC is affected). So return undefined
    rxjs_1.of())));
}
exports.getHeader = getHeader;
