"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.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const definitions_1 = __importDefault(require("../../types/interfaces/runtime/definitions"));
const types_1 = require("../../types");
// We can ignore the properties, added via Struct.with
const _Header = types_1.Struct.with(definitions_1.default.types.Header);
/**
 * @name HeaderExtended
 * @description
 * A [[Block]] header with an additional `author` field that indicates the block author
 */
class HeaderExtended extends _Header {
    constructor(registry, header, sessionValidators) {
        super(registry, header);
        this._author = this.extractAuthor(sessionValidators);
    }
    extractAuthor(sessionValidators = []) {
        const [pitem] = this.digest.logsWith('PreRuntime');
        // extract from the substrate 2.0 PreRuntime digest
        if (pitem) {
            const [engine, data] = pitem.asPreRuntime;
            return engine.extractAuthor(data, sessionValidators);
        }
        else {
            const [citem] = this.digest.logsWith('Consensus');
            // extract author from the consensus (substrate 1.0, digest)
            if (citem) {
                const [engine, data] = citem.asConsensus;
                return engine.extractAuthor(data, sessionValidators);
            }
        }
        return undefined;
    }
    /**
     * @description Convenience method, returns the author for the block
     */
    get author() {
        return this._author;
    }
    /**
     * @description Creates the JSON representation
     */
    toJSON() {
        return Object.assign(Object.assign({}, super.toJSON()), { author: this.author
                ? this.author.toJSON()
                : undefined });
    }
}
exports.default = HeaderExtended;
