"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _util = require("@polkadot/util");

var _create = require("../../../codec/create");

var _Struct = _interopRequireDefault(require("../../../codec/Struct"));

var _ExtrinsicSignature = _interopRequireDefault(require("./ExtrinsicSignature"));

// Copyright 2017-2019 @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.
const TRANSACTION_VERSION = 2;

/**
 * @name ExtrinsicV2
 * @description
 * The second generation of compact extrinsics
 */
class ExtrinsicV2 extends _Struct.default {
  constructor(value) {
    let {
      isSigned
    } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    super({
      signature: _ExtrinsicSignature.default,
      method: 'Call'
    }, ExtrinsicV2.decodeExtrinsic(value, isSigned));
  }

  static decodeExtrinsic(value) {
    let isSigned = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

    if (!value) {
      return {};
    } else if (value instanceof ExtrinsicV2) {
      return value;
    } else if (value instanceof (0, _create.ClassOf)('Call')) {
      return {
        method: value
      };
    } else if ((0, _util.isU8a)(value)) {
      // here we decode manually since we need to pull through the version information
      const signature = new _ExtrinsicSignature.default(value, {
        isSigned
      });
      const method = (0, _create.createType)('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 [[ExtrinsicSignatureV2]]
   */


  get signature() {
    return this.get('signature');
  }
  /**
   * @description The version for the signature
   */


  get version() {
    return TRANSACTION_VERSION;
  }
  /**
   * @description Add an [[ExtrinsicSignatureV2]] 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;
  }

}

exports.default = ExtrinsicV2;