"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 ClientNodeInteraction_exports = {};
__export(ClientNodeInteraction_exports, {
  ClientNodeInteraction: () => ClientNodeInteraction
});
module.exports = __toCommonJS(ClientNodeInteraction_exports);
var import_EndpointInitializer = require("#endpoint/properties/EndpointInitializer.js");
var import_NodePhysicalProperties = require("#node/NodePhysicalProperties.js");
var import_protocol = require("#protocol");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
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(import_protocol.ClientInteraction)) {
      return this.#node.env.get(import_protocol.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 = (0, import_NodePhysicalProperties.NodePhysicalProperties)(this.#node);
    const intermediateRequest = {
      ...this.structure.injectVersionFilters(request),
      ...import_protocol.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(import_protocol.ClientInteraction);
  }
  get structure() {
    return this.#node.env.get(import_EndpointInitializer.EndpointInitializer).structure;
  }
}
//# sourceMappingURL=ClientNodeInteraction.js.map
