/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { DescriptorClient } from "#behaviors/descriptor";
import { Environmental, Logger, Observable } from "#general";
const logger = Logger.get("ClientStructureEvents");
class ClientStructureEvents {
  #endpointEvents;
  #clusterEvents;
  #clusterReplaced;
  #clusterDeleted;
  static [Environmental.create](env) {
    const instance = new ClientStructureEvents();
    env.set(ClientStructureEvents, instance);
    return instance;
  }
  endpointInstalled(type) {
    if (this.#endpointEvents === void 0) {
      this.#endpointEvents = /* @__PURE__ */ new Map();
    }
    let event = this.#endpointEvents.get(type.deviceType);
    if (event === void 0) {
      this.#endpointEvents.set(type.deviceType, event = this.#createEvent("endpointInstalled"));
    }
    return event;
  }
  clusterInstalled(type) {
    if (this.#clusterEvents === void 0) {
      this.#clusterEvents = /* @__PURE__ */ new Map();
    }
    let events = this.#clusterEvents.get(type.id);
    if (events === void 0) {
      this.#clusterEvents.set(type.id, events = []);
    }
    for (const { requestedType, event: event2 } of events) {
      if (requestedType.supports(type)) {
        return event2;
      }
    }
    const event = this.#createEvent("clusterInstalled");
    events.push({ requestedType: type, event });
    return event;
  }
  get clusterReplaced() {
    if (this.#clusterReplaced) {
      return this.#clusterReplaced;
    }
    return this.#clusterReplaced = this.#createEvent("clusterReplaced");
  }
  get clusterDeleted() {
    if (this.#clusterDeleted) {
      return this.#clusterDeleted;
    }
    return this.#clusterDeleted = this.#createEvent("clusterDeleted");
  }
  emitEndpoint(endpoint) {
    if (this.#endpointEvents && endpoint.behaviors.supported.descriptor) {
      const deviceTypes = endpoint.stateOf(DescriptorClient).deviceTypeList;
      for (const dt of deviceTypes) {
        this.#endpointEvents.get(dt.deviceType)?.emit(endpoint);
      }
    }
    for (const type of Object.values(endpoint.behaviors.supported)) {
      if (!("cluster" in type)) {
        continue;
      }
      this.emitCluster(endpoint, type);
    }
    for (const part of endpoint.parts) {
      this.emitEndpoint(part);
    }
  }
  emitCluster(endpoint, type) {
    const events = this.#clusterEvents?.get(type.id);
    if (!events) {
      return;
    }
    for (const { requestedType, event } of events) {
      if (type.supports(requestedType)) {
        event.emit(endpoint, type);
      }
    }
  }
  emitClusterReplaced(endpoint, type) {
    if (!this.#clusterReplaced) {
      return;
    }
    this.#clusterReplaced.emit(endpoint, type);
  }
  emitClusterDeleted(endpoint, type) {
    if (!this.#clusterDeleted) {
      return;
    }
    this.#clusterDeleted.emit(endpoint, type);
  }
  #createEvent(kind) {
    return Observable(unhandledError);
    function unhandledError(e) {
      logger.error(`Unhandled error in client structure ${kind} event handler:`, e);
    }
  }
}
export {
  ClientStructureEvents
};
//# sourceMappingURL=ClientStructureEvents.js.map
