"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 util_1 = require("@polkadot/util");
const create_1 = require("../../../codec/create");
const Struct_1 = __importDefault(require("../../../codec/Struct"));
const TRANSACTION_VERSION = 3;
/**
 * @name GenericExtrinsicV3
 * @description
 * The third generation of compact extrinsics
 */
class ExtrinsicV3 extends Struct_1.default {
    constructor(registry, value, { isSigned } = {}) {
        super(registry, {
            signature: 'ExtrinsicSignatureV3',
            method: 'Call'
        }, ExtrinsicV3.decodeExtrinsic(registry, value, isSigned));
    }
    /** @internal */
    static decodeExtrinsic(registry, value, isSigned = false) {
        if (value instanceof ExtrinsicV3) {
            return value;
        }
        else if (value instanceof create_1.ClassOf(registry, 'Call')) {
            return { method: value };
        }
        else if (util_1.isU8a(value)) {
            // here we decode manually since we need to pull through the version information
            const signature = create_1.createType(registry, 'ExtrinsicSignatureV3', value, { isSigned });
            const method = create_1.createType(registry, 'Call', value.subarray(signature.encodedLength));
            return {
                method,
                signature
            };
        }
        return value || {};
    }
    /**
     * @description The length of the value when encoded as a Uint8Array
     */
    get encodedLength() {
        return this.toU8a().length;
    }
    /**
     * @description The [[Call]] this extrinsic wraps
     */
    get method() {
        return this.get('method');
    }
    /**
     * @description The [[ExtrinsicSignatureV3]]
     */
    get signature() {
        return this.get('signature');
    }
    /**
     * @description The version for the signature
     */
    get version() {
        return TRANSACTION_VERSION;
    }
    /**
     * @description Add an [[ExtrinsicSignatureV3]] to the extrinsic (already generated)
     */
    addSignature(signer, signature, payload) {
        this.signature.addSignature(signer, signature, payload);
        return this;
    }
    /**
     * @description Sign the extrinsic with a specific keypair
     */
    sign(account, options) {
        this.signature.sign(this.method, account, options);
        return this;
    }
    /**
     * @describe Adds a fake signature to the extrinsic
     */
    signFake(signer, options) {
        this.signature.signFake(this.method, signer, options);
        return this;
    }
}
exports.default = ExtrinsicV3;
