/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { EndpointInitializer } from "#endpoint/properties/EndpointInitializer.js";
import { NodePhysicalProperties } from "#node/NodePhysicalProperties.js";
import {
  ClientInteraction,
  PhysicalDeviceProperties
} from "#protocol";
class ClientNodeInteraction {
  #node;
  constructor(node) {
    this.#node = node;
  }
  /**
   * The current session used for interaction with the node if any session is established, otherwise undefined.
   */
  get session() {
    if (this.#node.env.has(ClientInteraction)) {
      return this.#node.env.get(ClientInteraction).session;
    }
  }
  /**
   * Read chosen attributes remotely from the node. Known data versions are automatically injected into the request to
   * optimize the read.
   * Therefore, the returned data only contains attributes that have changed since the last read or subscription.
   * TODO: Allow control of data version injection and enrich response with attribute data missing in response due to data versioning.
   */
  async *read(request, context) {
    request = this.structure.injectVersionFilters(request);
    const interaction = await this.#connect();
    const response = interaction.read(request, context);
    yield* this.structure.mutate(request, response);
  }
  /**
   * Subscribe to remote events and attributes as defined by {@link request}.
   *
   * matter.js updates local state
   *
   * By default matter.js subscribes to all attributes and events of the peer and updates {@link ClientNode} state
   * automatically.  So you normally do not need to subscribe manually.
   */
  async subscribe(request, context) {
    const physicalProps = NodePhysicalProperties(this.#node);
    const intermediateRequest = {
      ...this.structure.injectVersionFilters(request),
      ...PhysicalDeviceProperties.subscriptionIntervalBoundsFor({
        description: this.#node.toString(),
        properties: physicalProps,
        request
      }),
      sustain: request.sustain === void 0 ? true : request.sustain,
      updated: async (data) => {
        const result = this.structure.mutate(request, data);
        if (request.updated) {
          await request.updated(result);
        } else {
          for await (const _chunk of result) ;
        }
      },
      closed: request.closed?.bind(request)
    };
    return (await this.#connect()).subscribe(intermediateRequest, context);
  }
  /**
   * Write chosen attributes remotely to the node.
   * The returned attribute write status information is returned.
   */
  async write(request, context) {
    return (await this.#connect()).write(request, context);
  }
  async *invoke(request, context) {
    yield* (await this.#connect()).invoke(request, context);
  }
  async #connect() {
    if (!this.#node.lifecycle.isOnline) {
      await this.#node.start();
    }
    return this.#node.env.get(ClientInteraction);
  }
  get structure() {
    return this.#node.env.get(EndpointInitializer).structure;
  }
}
export {
  ClientNodeInteraction
};
//# sourceMappingURL=ClientNodeInteraction.js.map
