"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 IdentityService_exports = {};
__export(IdentityService_exports, {
  IdentityConflictError: () => IdentityConflictError,
  IdentityService: () => IdentityService
});
module.exports = __toCommonJS(IdentityService_exports);
var import_LocalActorContext = require("#behavior/context/server/LocalActorContext.js");
var import_IndexBehavior = require("#behavior/system/index/IndexBehavior.js");
var import_general = require("@matter/general");
var import_protocol = require("@matter/protocol");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class IdentityConflictError extends import_general.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(import_LocalActorContext.LocalActorContext.ReadOnly).get(import_IndexBehavior.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((0, import_protocol.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((0, import_protocol.PeerAddress)(address));
  }
}
//# sourceMappingURL=IdentityService.js.map
