import anyOf from './any-of.js';
import '../../utils/types/array.js';
import '../../utils/inheritance.js';
import '../../utils/types/object.js';
import '../../utils/types/iterable.js';
import '../../utils/types/set.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/interfaces.js';
import '../../utils/assert.js';
import '../../utils/error.js';
import '../../utils/types/string.js';
import './with-from.js';

/**
 * Enhances `prototype.eq` of the provided class to make it catch `null`ish values and
 * attempt to instantiate a new value of the supplied class in case if constructor is different.
 * @template C
 * @param {C} klass
 * @returns {C}
 */
function withEq(klass) {
  const name = `withEq(${klass.name})`;

  const obj = {
    [name]: class extends klass {
      eq(other) {
        if (other == null) {
          return false;
        } else if (Object.is(this, other)) {
          return true;
        } else {
          let compareWith = other;
          const { value, constructor } = this;
          const tryFrom = [
            constructor,
            value?.constructor,
            value?.constructor?.Class,
          ].filter((fn) => typeof fn?.from === 'function');
          try {
            compareWith = constructor.from(anyOf(...tryFrom).from(compareWith));
          } catch (err) {
            return false;
          }

          return super.eq(compareWith);
        }
      }
    },
  };

  return obj[name];
}

export { withEq as default };
//# sourceMappingURL=with-eq.js.map
