ref });
    } else {
      const ref = new SafeWeakRef(key);
      this.#weakMap.set(key, { value, ref });
      this.#refSet.add(ref);
      this.#finalizationGroup.register(key, {
        set: this.#refSet,
        ref
      }, ref);
    }
  }

  get(key) {
    return this.#weakMap.get(key)?.value;
  }

  has(key) {
    return this.#weakMap.has(key);
  }

  delete(key) {
    const entry = this.#weakMap.get(key);
    if (!entry) {
      return false;
    }
    this.#weakMap.delete(key);
    this.#refSet.delete(entry.ref);
    this.#finalizationGroup.unregister(entry.ref);
    return true;
  }

  [SymbolIterator]() {
    const iterator = this.#refSet[SymbolIterator]();

    const next = () => {
      const result = iterator.next();
      if (result.done) return result;
      const key = result.value.deref();
      if (key == null) return next();
      const { value } = this.#weakMap.get(key);
      return { done: false, value };
    };

    return {
      [SymbolIterator]() { return this; },
      next,
    };
  }
}

function cleanup({ set, ref }) {
  set.delete(ref);
}

ObjectFreeze(IterableWeakMap.prototype);

module.exports = {
  IterableWeakMap,
};
internal/util/iterable_weak_map