"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 _utilCrypto = require("@polkadot/util-crypto");

var _U8aFixed = _interopRequireDefault(require("../../codec/U8aFixed"));

// 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.

/**
 * @name AccountId
 * @description
 * A wrapper around an AccountId/PublicKey representation. Since we are dealing with
 * underlying PublicKeys (32 bytes in length), we extend from U8aFixed which is
 * just a Uint8Array wrapper with a fixed length.
 */
class AccountId extends _U8aFixed.default {
  constructor() {
    let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Uint8Array();
    super(AccountId.decodeAccountId(value), 256);
  }

  static encode(value) {
    return (0, _utilCrypto.encodeAddress)(value);
  }

  static decodeAccountId(value) {
    if ((0, _util.isU8a)(value) || Array.isArray(value)) {
      return (0, _util.u8aToU8a)(value);
    } else if ((0, _util.isHex)(value)) {
      return (0, _util.hexToU8a)(value.toString());
    } else if ((0, _util.isString)(value)) {
      return (0, _utilCrypto.decodeAddress)(value.toString());
    }

    return value;
  }
  /**
   * @description Compares the value of the input to see if there is a match
   */


  eq(other) {
    return super.eq(AccountId.decodeAccountId(other));
  }
  /**
   * @description Converts the Object to JSON, typically used for RPC transfers
   */


  toJSON() {
    return this.toString();
  }
  /**
   * @description Returns the string representation of the value
   */


  toString() {
    return AccountId.encode(this);
  }
  /**
   * @description Returns the base runtime type name for this instance
   */


  toRawType() {
    return 'AccountId';
  }

}

exports.default = AccountId;