"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 _create = require("../codec/create");

var _Compact = _interopRequireDefault(require("../codec/Compact"));

// 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 Text
 * @description
 * This is a string wrapper, along with the length. It is used both for strings as well
 * as items such as documentation. It simply extends the standard JS `String` built-in
 * object, inheriting all methods exposed from `String`.
 * @noInheritDoc
 */
// TODO
//   - Strings should probably be trimmed (docs do come through with extra padding)
class Text extends String {
  constructor() {
    let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    super(Text.decodeText(value));
  }

  static decodeText(value) {
    if ((0, _util.isHex)(value)) {
      return (0, _util.u8aToString)((0, _util.hexToU8a)(value.toString()));
    } else if (value instanceof Uint8Array) {
      if (!value.length) {
        return '';
      }

      const [offset, length] = _Compact.default.decodeU8a(value);

      const total = offset + length.toNumber();
      (0, _util.assert)(total <= value.length, "Text: required length less than remainder, expected at least ".concat(total, ", found ").concat(value.length));
      return (0, _util.u8aToString)(value.subarray(offset, total));
    }

    return "".concat(value);
  }
  /**
   * @description The length of the value when encoded as a Uint8Array
   */


  get encodedLength() {
    return this.toU8a().length;
  }
  /**
   * @description returns a hash of the contents
   */


  get hash() {
    return (0, _create.createType)('Hash', (0, _utilCrypto.blake2AsU8a)(this.toU8a(), 256));
  }
  /**
   * @description Checks if the value is an empty value
   */


  get isEmpty() {
    return this.length === 0;
  }
  /**
   * @description The length of the value
   */


  get length() {
    // only included here since we ignore inherited docs
    return super.length;
  }
  /**
   * @description Compares the value of the input to see if there is a match
   */


  eq(other) {
    return (0, _util.isString)(other) ? this.toString() === other.toString() : false;
  }
  /**
   * @description Returns a hex string representation of the value
   */


  toHex() {
    // like  with Vec<u8>, when we are encoding to hex, we don't actually add
    // the length prefix (it is already implied by the actual string length)
    return (0, _util.u8aToHex)(this.toU8a(true));
  }
  /**
   * @description Converts the Object to JSON, typically used for RPC transfers
   */


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


  toRawType() {
    return 'Text';
  }
  /**
   * @description Returns the string representation of the value
   */


  toString() {
    // only included here since we do not inherit docs
    return super.toString();
  }
  /**
   * @description Encodes the value as a Uint8Array as per the SCALE specifications
   * @param isBare true when the value has none of the type-specific prefixes (internal)
   */


  toU8a(isBare) {
    const encoded = (0, _util.stringToU8a)(this.toString());
    return isBare ? encoded : _Compact.default.addLengthPrefix(encoded);
  }

}

exports.default = Text;