"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Multiplex_exports = {};
__export(Multiplex_exports, {
  BasicMultiplex: () => BasicMultiplex
});
module.exports = __toCommonJS(Multiplex_exports);
var import_Logger = require("#log/Logger.js");
var import_MatterError = require("#MatterError.js");
var import_Promises = require("./Promises.js");
var import_Set = require("./Set.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_Logger.Logger.get("Multiplex");
class BasicMultiplex {
  #workers = new import_Set.BasicSet();
  add(worker, description) {
    if (!import_Promises.MaybePromise.is(worker)) {
      return;
    }
    const entry = {
      done: Promise.resolve(worker).catch((e) => {
        let message = "Error";
        if (description) {
          message = `${message} ${description}`;
        }
        logger.error(`${message}:`, e);
      }).finally(() => this.#workers.delete(entry)),
      description
    };
    this.#workers.add(entry);
  }
  async close() {
    while (this.#workers.size) {
      await import_MatterError.MatterAggregateError.allSettled([...this.#workers].map((entry) => entry.done));
    }
  }
  then(onfulfilled, onrejected) {
    return this.close().then(onfulfilled, onrejected);
  }
  [Symbol.asyncDispose] = this.close.bind(this);
}
//# sourceMappingURL=Multiplex.js.map
