"use strict";

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

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

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

var _Enum = _interopRequireDefault(require("../../codec/Enum"));

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

// 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.
class StorageFunctionType extends _Enum.default {
  constructor(value, index) {
    super({
      Type: 'PlainTypeV2',
      Map: 'MapTypeV2'
    }, value, index);
  }
  /**
   * @description The value as a mapped value
   */


  get asMap() {
    (0, _util.assert)(this.isMap, "Cannot convert '".concat(this.type, "' via asMap"));
    return this.value;
  }
  /**
   * @description The value as a [[Type]] value
   */


  get asType() {
    (0, _util.assert)(this.isPlainType, "Cannot convert '".concat(this.type, "' via asType"));
    return this.value;
  }
  /**
   * @description `true` if the storage entry is a map
   */


  get isMap() {
    return this.toNumber() === 1;
  }
  /**
   * @description `true` if the storage entry is a plain type
   */


  get isPlainType() {
    return this.toNumber() === 0;
  }
  /**
   * @description Returns the string representation of the value
   */


  toString() {
    if (this.isMap) {
      if (this.asMap.linked.isTrue) {
        return "(".concat(this.asMap.value.toString(), ", Linkage<").concat(this.asMap.key.toString(), ">)");
      }

      return this.asMap.value.toString();
    }

    return this.asType.toString();
  }

}

exports.StorageFunctionType = StorageFunctionType;

/**
 * @name ModuleMetadata
 * @description
 * The definition of a storage function
 */
class StorageFunctionMetadata extends _Struct.default {
  constructor(value) {
    super({
      name: 'Text',
      modifier: 'StorageFunctionModifierV2',
      type: StorageFunctionType,
      fallback: 'Bytes',
      documentation: 'Vec<Text>'
    }, value);
  }
  /**
   * @description The [[Text]] documentation
   */


  get documentation() {
    return this.get('documentation');
  }
  /**
   * @description The [[Bytes]] fallback default
   */


  get fallback() {
    return this.get('fallback');
  }
  /**
   * @description The [[StorageFunctionModifierV2]] for arguments
   */


  get modifier() {
    return this.get('modifier');
  }
  /**
   * @description The call name
   */


  get name() {
    return this.get('name');
  }
  /**
   * @description The [[StorageFunctionType]]
   */


  get type() {
    return this.get('type');
  }

}

exports.StorageFunctionMetadata = StorageFunctionMetadata;