"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 EndpointContainer_exports = {};
__export(EndpointContainer_exports, {
  EndpointContainer: () => EndpointContainer
});
module.exports = __toCommonJS(EndpointContainer_exports);
var import_general = require("#general");
var import_IdentityService = require("#node/server/IdentityService.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class EndpointContainer {
  #children = new import_general.BasicSet();
  #owner;
  constructor(endpoint) {
    this.#owner = endpoint;
  }
  get(id) {
    if (typeof id === "number") {
      for (const child of this) {
        if (child.maybeNumber === id) {
          return child;
        }
      }
    } else {
      for (const child of this) {
        if (child.maybeId === id) {
          return child;
        }
      }
    }
  }
  add(endpoint) {
    if (endpoint.lifecycle.hasId) {
      this.assertIdAvailable(endpoint.id, endpoint);
    }
    this.#children.add(endpoint);
    endpoint.owner = this.#owner;
    endpoint.lifecycle.destroyed.once(() => {
      this.delete(endpoint);
    });
    this.#children.add(endpoint);
  }
  delete(endpoint) {
    return this.#children.delete(endpoint);
  }
  clear() {
    this.#children.clear();
  }
  has(endpoint) {
    return this.#children.has(endpoint);
  }
  get added() {
    return this.#children.added;
  }
  get deleted() {
    return this.#children.deleted;
  }
  get empty() {
    return this.#children.empty;
  }
  get size() {
    return this.#children.size;
  }
  map(fn) {
    return this.#children.map(fn);
  }
  filter(predicate) {
    return this.#children.filter(predicate);
  }
  find(predicate) {
    return this.#children.find(predicate);
  }
  forEach(fn) {
    [...this.#children].forEach(fn);
  }
  [Symbol.iterator]() {
    return this.#children[Symbol.iterator]();
  }
  /**
   * Soft-reset all parts.  Invoked automatically by the owner on reset.
   */
  async reset() {
    for (const endpoint of this) {
      await endpoint.reset();
    }
  }
  /**
   * Destroy all parts.  Invoked automatically by the owner on destroy.
   */
  async close() {
    for (const endpoint of this) {
      await endpoint.close();
    }
  }
  /**
   * Confirm the availability of an ID amongst the endpoint's children.
   */
  assertIdAvailable(id, endpoint) {
    const other = this.get(id);
    if (other && other !== endpoint) {
      throw new import_IdentityService.IdentityConflictError(`${other} is already defined; endpoint IDs must be unique within parent`);
    }
  }
  get owner() {
    return this.#owner;
  }
  get [import_general.Diagnostic.value]() {
    return import_general.Diagnostic.list([(0, import_general.decamelize)(this.constructor.name), ...this]);
  }
}
//# sourceMappingURL=EndpointContainer.js.map
