"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 _AbstractArray = _interopRequireDefault(require("./AbstractArray"));

var _utils = require("./utils");

var _Vec = _interopRequireDefault(require("./Vec"));

// 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 VecFixed
 * @description
 * This manages codec arrays of a fixed length
 */
class VecFixed extends _AbstractArray.default {
  constructor(Type, length) {
    let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
    const Clazz = (0, _utils.typeToConstructor)(Type);
    super(...VecFixed.decodeVecFixed(Clazz, length, value));
    this._Type = void 0;
    this._Type = Clazz;
  }

  static decodeVecFixed(Type, allocLength, value) {
    const values = _Vec.default.decodeVec(Type, (0, _util.isU8a)(value) ? (0, _util.u8aConcat)((0, _util.compactToU8a)(allocLength), value) : value);

    while (values.length < allocLength) {
      values.push(new Type());
    }

    (0, _util.assert)(values.length === allocLength, "Expected a length of exactly ".concat(allocLength, " entries"));
    return values;
  }

  static with(Type, length) {
    return class extends VecFixed {
      constructor(value) {
        super(Type, length, value);
      }

    };
  }
  /**
   * @description The type for the items
   */


  get Type() {
    return new this._Type().toRawType();
  }

  toU8a() {
    // we override, we don't add the length prefix for outselves, and at the same time we
    // ignore isBare on entries, since they should be properly encoded at all times
    const encoded = this.map(entry => entry.toU8a());
    return (0, _util.u8aConcat)(...encoded);
  }
  /**
   * @description Returns the base runtime type name for this instance
   */


  toRawType() {
    return "[".concat(this.Type, ";").concat(this.length, "]");
  }

}

exports.default = VecFixed;