/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
var AsyncIterator;
((AsyncIterator2) => {
  async function* merge(iterators, errorMessage = "One or more async iterators failed") {
    const asyncIterators = iterators.map((iter) => iter[Symbol.asyncIterator]());
    const pending = /* @__PURE__ */ new Map();
    const errors = [];
    for (let i = 0; i < asyncIterators.length; i++) {
      pending.set(
        i,
        asyncIterators[i].next().then(
          (result) => ({ index: i, result }),
          (error) => {
            errors.push(error);
            return { index: i, result: { done: true, value: void 0 } };
          }
        )
      );
    }
    while (pending.size > 0) {
      const { index, result } = await Promise.race(pending.values());
      if (result.done) {
        pending.delete(index);
      } else {
        yield result.value;
        pending.set(
          index,
          asyncIterators[index].next().then(
            (result2) => ({ index, result: result2 }),
            (error) => {
              errors.push(error);
              return { index, result: { done: true, value: void 0 } };
            }
          )
        );
      }
    }
    if (errors.length > 0) {
      throw new AggregateError(errors, errorMessage);
    }
  }
  AsyncIterator2.merge = merge;
})(AsyncIterator || (AsyncIterator = {}));
export {
  AsyncIterator
};
//# sourceMappingURL=AsyncIterator.js.map
