"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 ClientStructureEvents_exports = {};
__export(ClientStructureEvents_exports, {
  ClientStructureEvents: () => ClientStructureEvents
});
module.exports = __toCommonJS(ClientStructureEvents_exports);
var import_descriptor = require("#behaviors/descriptor");
var import_general = require("@matter/general");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("ClientStructureEvents");
class ClientStructureEvents {
  #endpointEvents;
  #clusterEvents;
  #clusterReplaced;
  #clusterDeleted;
  static [import_general.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(import_descriptor.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 (0, import_general.Observable)(unhandledError);
    function unhandledError(e) {
      logger.error(`Unhandled error in client structure ${kind} event handler:`, e);
    }
  }
}
//# sourceMappingURL=ClientStructureEvents.js.map
