/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { LocalActorContext } from "#behavior/context/server/LocalActorContext.js";
import { IndexBehavior } from "#behavior/system/index/IndexBehavior.js";
import { ImplementationError } from "@matter/general";
import { PeerAddress } from "@matter/protocol";
class IdentityConflictError extends ImplementationError {
}
class IdentityService {
  #partsById;
  #node;
  #reservedPeerAddresses = /* @__PURE__ */ new Set();
  constructor(node) {
    this.#node = node;
  }
  /**
   * Textual description of the node.
   */
  get nodeDescription() {
    return this.#node.toString();
  }
  /**
   * Ensure that a number is available for assignment to a {@link Endpoint}.
   */
  assertEndpointNumberAvailable(number, endpoint) {
    let other;
    if (this.#node.lifecycle.hasNumber && this.#node.number === number) {
      other = this.#node;
    } else {
      if (this.#partsById === void 0) {
        this.#partsById = this.#node.agentFor(LocalActorContext.ReadOnly).get(IndexBehavior).partsById;
      }
      other = this.#partsById?.[number];
    }
    if (other && other !== endpoint) {
      let owner;
      if (other.lifecycle.hasId) {
        owner = `endpoint ${other.id}`;
      } else {
        owner = `another endpoint`;
      }
      throw new IdentityConflictError(`Endpoint number ${number} is already assigned to ${owner}`);
    }
  }
  /**
   * Detect whether a peer address is currently assigned to a peer.
   */
  peerAddressInUse(address) {
    return this.#reservedPeerAddresses.has(PeerAddress(address));
  }
  /**
   * Mark a peer address as in use.
   */
  reservePeerAddress(address) {
    this.#reservedPeerAddresses.add(address);
  }
  /**
   * Mark a peer address as available for use.
   */
  releasePeerAddress(address) {
    this.#reservedPeerAddresses.delete(PeerAddress(address));
  }
}
export {
  IdentityConflictError,
  IdentityService
};
//# sourceMappingURL=IdentityService.js.map
