"use strict";
// Copyright 2017-2020 @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.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("@polkadot/util");
const AbstractArray_1 = __importDefault(require("./AbstractArray"));
const utils_1 = require("./utils");
const Vec_1 = __importDefault(require("./Vec"));
/**
 * @name VecFixed
 * @description
 * This manages codec arrays of a fixed length
 */
class VecFixed extends AbstractArray_1.default {
    constructor(registry, Type, length, value = []) {
        const Clazz = utils_1.typeToConstructor(registry, Type);
        super(registry, ...VecFixed.decodeVecFixed(registry, Clazz, length, value));
        this._Type = Clazz;
    }
    /** @internal */
    static decodeVecFixed(registry, Type, allocLength, value) {
        const values = Vec_1.default.decodeVec(registry, Type, util_1.isU8a(value)
            ? util_1.u8aConcat(util_1.compactToU8a(allocLength), value)
            : value);
        while (values.length < allocLength) {
            values.push(new Type(registry));
        }
        util_1.assert(values.length === allocLength, `Expected a length of exactly ${allocLength} entries`);
        return values;
    }
    static with(Type, length) {
        return class extends VecFixed {
            constructor(registry, value) {
                super(registry, Type, length, value);
            }
        };
    }
    /**
     * @description The type for the items
     */
    get Type() {
        return new this._Type(this.registry).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 util_1.u8aConcat(...encoded);
    }
    /**
     * @description Returns the base runtime type name for this instance
     */
    toRawType() {
        return `[${this.Type};${this.length}]`;
    }
}
exports.default = VecFixed;
