'use strict';

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

let TypedSet$1 = class TypedSet extends types_generic_withBase(Set) {
  static Class;

  constructor(values = []) {
    super();

    this.valuesAsClass = new Map();

    for (const value of values) {
      this.add(value);
    }
  }

  valueAsClass(value) {
    const valuesAsClass = utils_interfaces.maybeFrom(this.constructor.Class, value);
    const valueStr = utils_interfaces.maybeToJSONString(valuesAsClass);

    if (this.valuesAsClass.has(valueStr)) {
      return this.valuesAsClass.get(valueStr);
    } else {
      this.valuesAsClass.set(valueStr, valuesAsClass);

      return valuesAsClass;
    }
  }

  delete(key) {
    const removed = super.delete(this.valueAsClass(key));
    if (removed) this.valuesAsClass.delete(key);

    return removed;
  }

  add(value) {
    return super.add(this.valueAsClass(value));
  }

  has(value) {
    return super.has(this.valueAsClass(value));
  }

  toJSON() {
    return this.apply(utils_interfaces.maybeToJSON).sort();
  }

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

  clear() {
    this.clear();
    this.valuesAsClass.clear();
  }

  static fromJSON(values) {
    return new this(values);
  }

  static fromApi(value) {
    return new this(value.values());
  }

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

  eq(other) {
    return (
      this.size === other.size && this.values().all((value) => other.has(value))
    );
  }
};

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

module.exports = TypedSet;
//# sourceMappingURL=typed-set.cjs.map
