"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 AtomicWriteState_exports = {};
__export(AtomicWriteState_exports, {
  AtomicWriteState: () => AtomicWriteState
});
module.exports = __toCommonJS(AtomicWriteState_exports);
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("AtomicWriteState");
const MAXIMUM_ALLOWED_TIMEOUT = (0, import_general.Seconds)(9);
class AtomicWriteState {
  peerAddress;
  endpoint;
  clusterId;
  attributeRequests;
  attributeNames = /* @__PURE__ */ new Map();
  pendingAttributeValues = {};
  timeout;
  initialValues;
  closed = (0, import_general.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 = import_general.Time.getTimer("AtomicWriteState Timeout", (0, import_general.Millis)(this.timeout), () => this.#timeoutTriggered());
  }
  get [import_general.Diagnostic.value]() {
    return import_general.Diagnostic.dict({
      peerAddress: this.peerAddress.toString(),
      endpointId: this.endpoint.id,
      clusterId: (0, import_general.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();
  }
}
//# sourceMappingURL=AtomicWriteState.js.map
