"use strict";
// Copyright 2017-2020 @polkadot/types 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 Struct_1 = __importDefault(require("../../codec/Struct"));
const Tuple_1 = __importDefault(require("../../codec/Tuple"));
const Null_1 = __importDefault(require("../Null"));
/**
 * @name EventData
 * @description
 * Wrapper for the actual data that forms part of an [[Event]]
 */
class EventData extends Tuple_1.default {
    constructor(registry, Types, value, typeDef, meta, section, method) {
        super(registry, Types, value);
        this._meta = meta;
        this._method = method;
        this._section = section;
        this._typeDef = typeDef;
    }
    /**
     * @description The wrapped [[EventMetadata]]
     */
    get meta() {
        return this._meta;
    }
    /**
     * @description The method as a string
     */
    get method() {
        return this._method;
    }
    /**
     * @description The section as a string
     */
    get section() {
        return this._section;
    }
    /**
     * @description The [[TypeDef]] for this event
     */
    get typeDef() {
        return this._typeDef;
    }
}
exports.EventData = EventData;
/**
 * @name Event
 * @description
 * A representation of a system event. These are generated via the [[Metadata]] interfaces and
 * specific to a specific Substrate runtime
 */
class Event extends Struct_1.default {
    // Currently we _only_ decode from Uint8Array, since we expect it to
    // be used via EventRecord
    constructor(registry, _value) {
        const { DataType, value } = Event.decodeEvent(registry, _value);
        super(registry, {
            index: 'EventId',
            data: DataType
        }, value);
    }
    /** @internal */
    static decodeEvent(registry, value = new Uint8Array()) {
        if (!value.length) {
            return {
                DataType: Null_1.default
            };
        }
        const index = value.subarray(0, 2);
        return {
            DataType: registry.findMetaEvent(index),
            value: {
                index,
                data: value.subarray(2)
            }
        };
    }
    /**
     * @description The wrapped [[EventData]]
     */
    get data() {
        return this.get('data');
    }
    /**
     * @description The [[EventId]], identifying the raw event
     */
    get index() {
        return this.get('index');
    }
    /**
     * @description The [[EventMetadata]] with the documentation
     */
    get meta() {
        return this.data.meta;
    }
    /**
     * @description The method string identifying the event
     */
    get method() {
        return this.data.method;
    }
    /**
     * @description The section string identifying the event
     */
    get section() {
        return this.data.section;
    }
    /**
     * @description The [[TypeDef]] for the event
     */
    get typeDef() {
        return this.data.typeDef;
    }
}
exports.default = Event;
