/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Diagnostic, Logger, Millis, Observable, Seconds, Time, toHex } from "#general";
const logger = Logger.get("AtomicWriteState");
const MAXIMUM_ALLOWED_TIMEOUT = Seconds(9);
class AtomicWriteState {
  peerAddress;
  endpoint;
  clusterId;
  attributeRequests;
  attributeNames = /* @__PURE__ */ new Map();
  pendingAttributeValues = {};
  timeout;
  initialValues;
  closed = Observable();
  #timer;
  constructor(peerAddress, endpoint, cluster, attributeRequests, timeout, attributeNames, initialValues) {
    this.peerAddress = peerAddress;
    this.endpoint = endpoint;
    this.clusterId = cluster;
    this.attributeRequests = attributeRequests;
    this.timeout = Math.min(timeout, MAXIMUM_ALLOWED_TIMEOUT);
    this.attributeNames = attributeNames;
    this.initialValues = initialValues;
    this.#timer = Time.getTimer("AtomicWriteState Timeout", Millis(this.timeout), () => this.#timeoutTriggered());
  }
  get [Diagnostic.value]() {
    return Diagnostic.dict({
      peerAddress: this.peerAddress.toString(),
      endpointId: this.endpoint.id,
      clusterId: toHex(this.clusterId),
      attributeRequests: this.attributeRequests,
      timeout: this.timeout,
      initialValues: this.initialValues,
      pendingAttributeValues: this.pendingAttributeValues
    });
  }
  start() {
    logger.debug(
      `Starting atomic write state for peer ${this.peerAddress.toString()} on endpoint ${this.endpoint.id}`
    );
    this.#timer.start();
  }
  #timeoutTriggered() {
    logger.debug(
      `Atomic write state for peer ${this.peerAddress.toString()} on endpoint ${this.endpoint.id} timed out`
    );
    this.close();
  }
  close() {
    logger.debug(
      `Closing atomic write state for peer ${this.peerAddress.toString()} on endpoint ${this.endpoint.id}`
    );
    if (this.#timer.isRunning) {
      this.#timer.stop();
    }
    this.closed.emit();
  }
}
export {
  AtomicWriteState
};
//# sourceMappingURL=AtomicWriteState.js.map
