/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ImplementationError } from "@matter/general";
function createEventClient(event, name, endpointId, clusterId, interactionClient) {
  return new EventClient(event, name, endpointId, clusterId, interactionClient);
}
class EventClient {
  constructor(event, name, endpointId, clusterId, interactionClient) {
    this.event = event;
    this.name = name;
    this.endpointId = endpointId;
    this.clusterId = clusterId;
    this.id = event.id;
    this.#interactionClient = interactionClient;
  }
  #listeners = new Array();
  id;
  #interactionClient;
  async get(minimumEventNumber, isFabricFiltered) {
    if (this.endpointId === void 0) {
      throw new ImplementationError("Cannot read event without endpointId");
    }
    return await this.#interactionClient.getEvent({
      endpointId: this.endpointId,
      clusterId: this.clusterId,
      event: this.event,
      minimumEventNumber,
      isFabricFiltered
    });
  }
  async subscribe(minIntervalFloorSeconds, maxIntervalCeilingSeconds, isUrgent = true, minimumEventNumber, isFabricFiltered) {
    if (this.endpointId === void 0) {
      throw new ImplementationError("Cannot read event without endpointId");
    }
    return await this.#interactionClient.subscribeEvent({
      endpointId: this.endpointId,
      clusterId: this.clusterId,
      event: this.event,
      minIntervalFloorSeconds,
      maxIntervalCeilingSeconds,
      isUrgent,
      minimumEventNumber,
      isFabricFiltered,
      listener: this.update.bind(this)
    });
  }
  update(newEvent) {
    for (const listener of this.#listeners) {
      listener(newEvent);
    }
  }
  addListener(listener) {
    this.#listeners.push(listener);
  }
  removeListener(listener) {
    const entryIndex = this.#listeners.indexOf(listener);
    if (entryIndex !== -1) {
      this.#listeners.splice(entryIndex, 1);
    }
  }
}
export {
  EventClient,
  createEventClient
};
//# sourceMappingURL=EventClient.js.map
