import { maybeFrom, maybeToJSONString, maybeToJSON } from '../../utils/interfaces.js';
import { withExtendedStaticProperties } from '../../utils/inheritance.js';
import withCatchNull from './with-catch-null.js';
import withEq from './with-eq.js';
import withBase from './with-base.js';
import '../../utils/assert.js';
import '../../utils/error.js';
import '../../utils/types/object.js';
import '../../utils/types/iterable.js';
import '../../utils/types/set.js';
import './any-of.js';
import '../../utils/types/array.js';
import 'crypto';
import 'bs58';
import 'multiformats/bases/base58';
import 'varint';
import '../../utils/encoding/ss-58.js';
import 'blake2b';
import '../../utils/types/bytes.js';
import '../../utils/types/string.js';
import './with-from.js';
import './with-from-json-bytes.js';

class TypedSet extends withBase(Set) {
  static Class;

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

    this.valuesAsClass = new Map();

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

  valueAsClass(value) {
    const valuesAsClass = maybeFrom(this.constructor.Class, value);
    const valueStr = 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(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$1 = withEq(
  withCatchNull(withExtendedStaticProperties(['Class'], TypedSet)),
);

export { TypedSet$1 as default };
//# sourceMappingURL=typed-set.js.map
