'use strict';

var utils_inheritance = require('../../utils/inheritance.cjs');
var utils_interfaces = require('../../utils/interfaces.cjs');
var utils_types_array = require('../../utils/types/array.cjs');
require('crypto');
require('bs58');
require('multiformats/bases/base58');
require('varint');
require('../../utils/encoding/ss-58.cjs');
var types_generic_withBase = require('./with-base.cjs');
var types_generic_withCatchNull = require('./with-catch-null.cjs');
var types_generic_withEq = require('./with-eq.cjs');
require('../../utils/types/object.cjs');
require('../../utils/types/iterable.cjs');
require('../../utils/types/set.cjs');
require('../../utils/assert.cjs');
require('../../utils/error.cjs');
require('blake2b');
require('../../utils/types/bytes.cjs');
require('../../utils/types/string.cjs');
require('./with-from-json-bytes.cjs');
require('./any-of.cjs');
require('./with-from.cjs');

let TypedTuple$1 = class TypedTuple extends types_generic_withBase(utils_types_array.ArrayWithoutPrototypeMethods) {
  /**
   * Item's classes.
   * @type {Array<Function>}
   */
  static Classes;

  static get Size() {
    return this.Classes.length;
  }

  constructor(...items) {
    super(items.length);

    const { Size } = this.constructor;

    if (this.constructor.Size !== items.length) {
      throw new Error(
        `Received \`${items.length}\` items while expected \`${Size}\` by \`${this.constructor.name}\``,
      );
    }

    for (let idx = 0; idx < items.length; idx++) {
      const prop = Symbol(idx);

      Object.defineProperty(this, idx, {
        enumerable: true,
        get() {
          return this[prop];
        },
        set(newValue) {
          try {
            this[prop] = utils_interfaces.maybeFrom(this.constructor.Classes[idx], newValue);
          } catch (error) {
            error.message = `\nFailed to set property \`${idx}\` of \`${this.constructor.name}\`: ${error.message}`;

            throw error;
          }
        },
      });

      this[idx] = items[idx];
    }

    Object.seal(this);
  }

  apply(fn) {
    return [...this].map(fn);
  }

  static from(value) {
    if (value instanceof this) {
      return value;
    } else if (Array.isArray(value)) {
      return this.fromJSON(value);
    } else if (typeof value === 'object') {
      return this.fromApi(value);
    } else {
      return new this(value);
    }
  }

  static fromJSON(arr) {
    return new this(...utils_types_array.ensureArrayLike(arr, this.name));
  }

  static fromApi(arr) {
    return this.fromJSON(arr);
  }

  eq(other) {
    if (this.length !== other.length) {
      return false;
    }

    for (let i = 0; i < this.length; i++) {
      if (!utils_interfaces.maybeEq(this[i], other[i])) {
        return false;
      }
    }

    return true;
  }
};

var TypedTuple = types_generic_withEq(
  types_generic_withCatchNull(utils_inheritance.withExtendedStaticProperties(['Classes'], TypedTuple$1)),
);

module.exports = TypedTuple;
//# sourceMappingURL=typed-tuple.cjs.map
