var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
var __typeError = (msg) => {
  throw TypeError(msg);
};
var __using = (stack, value, async) => {
  if (value != null) {
    if (typeof value !== "object" && typeof value !== "function") __typeError("Object expected");
    var dispose, inner;
    if (async) dispose = value[__knownSymbol("asyncDispose")];
    if (dispose === void 0) {
      dispose = value[__knownSymbol("dispose")];
      if (async) inner = dispose;
    }
    if (typeof dispose !== "function") __typeError("Object not disposable");
    if (inner) dispose = function() {
      try {
        inner.call(this);
      } catch (e) {
        return Promise.reject(e);
      }
    };
    stack.push([async, dispose, value]);
  } else if (async) {
    stack.push([async]);
  }
  return value;
};
var __callDispose = (stack, error, hasError) => {
  var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
    return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
  };
  var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
  var next = (it) => {
    while (it = stack.pop()) {
      try {
        var result = it[1] && it[1].call(it[2]);
        if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next()));
      } catch (e) {
        fail(e);
      }
    }
    if (hasError) throw error;
  };
  return next();
};
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { NodeActivity } from "#behavior/context/NodeActivity.js";
import { RemoteActorContext } from "#behavior/context/server/RemoteActorContext.js";
import {
  AsyncObservable,
  ClosedError,
  Diagnostic,
  Duration,
  hex,
  Hours,
  InternalError,
  Logger,
  MatterError,
  Millis,
  Minutes,
  NetworkError,
  NoResponseTimeoutError,
  ObserverGroup,
  Seconds,
  Time,
  Timestamp
} from "@matter/general";
import { Specification } from "@matter/model";
import {
  AttributeReadResponse,
  AttributeSubscriptionResponse,
  EventReadResponse,
  InteractionServerMessenger,
  Read,
  SessionClosedError
} from "@matter/protocol";
import {
  ClusterId,
  EndpointNumber,
  EventNumber,
  INTERACTION_PROTOCOL_ID,
  Status,
  StatusResponseError
} from "@matter/types";
const logger = Logger.get("ServerSubscription");
const MAX_INTERVAL_PUBLISHER_LIMIT = Hours.one;
const INTERNAL_INTERVAL_PUBLISHER_LIMIT = Minutes(3);
const MIN_INTERVAL = Seconds(2);
const DEFAULT_RANDOMIZATION_WINDOW = Seconds(10);
var ServerSubscriptionConfig;
((ServerSubscriptionConfig2) => {
  function of(options) {
    return {
      maxInterval: options?.maxInterval ?? INTERNAL_INTERVAL_PUBLISHER_LIMIT,
      minInterval: Duration.max(options?.minInterval ?? MIN_INTERVAL, MIN_INTERVAL),
      randomizationWindow: options?.randomizationWindow ?? DEFAULT_RANDOMIZATION_WINDOW
    };
  }
  ServerSubscriptionConfig2.of = of;
})(ServerSubscriptionConfig || (ServerSubscriptionConfig = {}));
class ServerSubscription {
  #lifetime;
  #context;
  #id;
  #isClosed = false;
  #isCanceledByPeer = false;
  #request;
  #cancelled = AsyncObservable();
  #maxInterval;
  #lastUpdateTime = Timestamp.zero;
  #updateTimer;
  #sendDelayTimer;
  #outstandingAttributeUpdates;
  #outstandingEventsMinNumber;
  #changeHandlers = new ObserverGroup();
  #sendUpdatesActivated = false;
  #seededClusterDetails = /* @__PURE__ */ new Map();
  #latestSeededEventNumber = EventNumber(0);
  #sendInterval;
  #peerAddress;
  #sendNextUpdateImmediately = false;
  #sendUpdateErrorCounter = 0;
  #currentUpdatePromise;
  #currentSendExchange;
  constructor(options) {
    const { id, context, request, subscriptionOptions, useAsMaxInterval, useAsSendInterval } = options;
    this.#id = id;
    this.#request = request;
    this.#context = context;
    this.#peerAddress = this.session.peerAddress;
    let maxInterval;
    let sendInterval;
    if (useAsMaxInterval !== void 0 && useAsSendInterval !== void 0) {
      maxInterval = useAsMaxInterval;
      sendInterval = useAsSendInterval;
    } else {
      ({ maxInterval, sendInterval } = this.#determineSendingIntervals(
        subscriptionOptions.minInterval,
        subscriptionOptions.maxInterval,
        subscriptionOptions.randomizationWindow
      ));
    }
    this.maxInterval = maxInterval;
    this.#sendInterval = sendInterval;
    this.#sendDelayTimer = Time.getTimer(
      `Subscription ${this.idStr} delay`,
      Millis(50),
      () => this.#triggerSendUpdate()
    );
    this.#updateTimer = Time.getTimer(
      `Subscription ${this.idStr} update`,
      this.#sendInterval,
      () => this.#prepareDataUpdate()
    );
  }
  get subscriptionId() {
    return this.#id;
  }
  get idStr() {
    return hex.fixed(this.#id, 8);
  }
  get session() {
    return this.#context.session;
  }
  get isCanceledByPeer() {
    return this.#isCanceledByPeer;
  }
  get request() {
    return this.#request;
  }
  get cancelled() {
    return this.#cancelled;
  }
  get maxInterval() {
    if (this.#maxInterval === void 0) {
      throw new InternalError("Subscription maxInterval accessed before it was set");
    }
    return this.#maxInterval;
  }
  get sendInterval() {
    return this.#sendInterval;
  }
  get minIntervalFloor() {
    return Seconds(this.request.minIntervalFloorSeconds);
  }
  get maxIntervalCeiling() {
    return Seconds(this.request.maxIntervalCeilingSeconds);
  }
  set maxInterval(value) {
    if (this.#maxInterval !== void 0) {
      throw new InternalError("Subscription maxInterval set twice");
    }
    this.#maxInterval = value;
  }
  async handlePeerCancel() {
    this.#isCanceledByPeer = true;
    try {
      await this.#currentSendExchange?.close(new ClosedError("Subscription cancelled by peer"));
    } catch (error) {
      logger.debug("Error closing in-flight send exchange on peer cancel:", error);
    } finally {
      await this.close();
    }
  }
  #determineSendingIntervals(subscriptionMinInterval, subscriptionMaxInterval, subscriptionRandomizationWindow) {
    const maxInterval = Duration.min(
      Millis.floor(
        Millis(
          Duration.max(
            subscriptionMinInterval,
            Duration.max(
              this.minIntervalFloor,
              Duration.min(subscriptionMaxInterval, this.maxIntervalCeiling)
            )
          ) + subscriptionRandomizationWindow * Math.random()
        )
      ),
      MAX_INTERVAL_PUBLISHER_LIMIT
    );
    let sendInterval = Millis.floor(Millis(maxInterval / 2));
    if (sendInterval < Minutes.one) {
      sendInterval = Duration.max(this.minIntervalFloor, Millis.floor(Millis(maxInterval * 0.8)));
    }
    if (sendInterval < subscriptionMinInterval) {
      logger.warn(
        `Determined subscription send interval of ${Duration.format(sendInterval)} is too low. Using maxInterval (${Duration.format(maxInterval)}) instead.`
      );
      sendInterval = subscriptionMinInterval;
    }
    return { maxInterval, sendInterval };
  }
  #addOutstandingAttributes(endpointId, clusterId, changedAttrs) {
    if (!changedAttrs.length) {
      return;
    }
    this.#outstandingAttributeUpdates = this.#outstandingAttributeUpdates ?? {};
    this.#outstandingAttributeUpdates[endpointId] = this.#outstandingAttributeUpdates[endpointId] ?? {};
    this.#outstandingAttributeUpdates[endpointId][clusterId] = this.#outstandingAttributeUpdates[endpointId][clusterId] ?? /* @__PURE__ */ new Set();
    for (const attributeId of changedAttrs) {
      this.#outstandingAttributeUpdates[endpointId][clusterId].add(attributeId);
    }
  }
  #handleClusterStateChanges(endpointId, clusterId, changedAttrs, version) {
    if (this.#isClosed || !changedAttrs.length) {
      return;
    }
    if (this.#seededClusterDetails !== void 0) {
      const seededVersion = this.#seededClusterDetails.get(`${endpointId}-${clusterId}`);
      if (seededVersion !== void 0 && seededVersion === version) {
        return;
      }
    }
    this.#addOutstandingAttributes(endpointId, clusterId, changedAttrs);
    this.#prepareDataUpdate();
  }
  #handleAddedEvents(occurrence) {
    if (this.#isClosed) {
      return;
    }
    if (this.#outstandingEventsMinNumber === void 0 || this.#latestSeededEventNumber !== void 0) {
      this.#outstandingEventsMinNumber = occurrence.number;
    }
    if (this.#sendEventUrgently(occurrence)) {
      this.#prepareDataUpdate();
    }
  }
  #sendEventUrgently({ endpointId, clusterId, eventId }) {
    return (this.request.eventRequests ?? []).some(
      ({ endpointId: reqEndpointId, clusterId: reqClusterId, eventId: reqEventId, isUrgent }) => isUrgent && (reqEndpointId === void 0 || reqEndpointId === endpointId) && (reqClusterId === void 0 || reqClusterId === clusterId) && (reqEventId === void 0 || reqEventId === eventId)
    );
  }
  activate() {
    this.session.subscriptions.add(this);
    logger.debug(this.session.via, "New subscription", Diagnostic.strong(this.idStr));
    this.#lifetime = this.#context.session.join("subscription", Diagnostic.strong(this.#id));
    if (this.request.eventFilters !== void 0) this.request.eventFilters.length = 0;
    if (this.request.dataVersionFilters !== void 0) this.request.dataVersionFilters.length = 0;
    this.#sendUpdatesActivated = true;
    if (this.#outstandingEventsMinNumber !== void 0 && this.#latestSeededEventNumber !== void 0) {
      if (this.#latestSeededEventNumber < this.#outstandingEventsMinNumber) {
        this.#outstandingEventsMinNumber = EventNumber(BigInt(this.#latestSeededEventNumber) + BigInt(1));
      } else {
        this.#outstandingEventsMinNumber = void 0;
      }
    }
    this.#latestSeededEventNumber = void 0;
    this.#seededClusterDetails = void 0;
    if (this.#outstandingAttributeUpdates !== void 0 || this.#outstandingEventsMinNumber !== void 0) {
      this.#triggerSendUpdate();
    }
    this.#updateTimer = Time.getTimer(
      "Subscription update",
      this.#sendInterval,
      () => this.#prepareDataUpdate()
    ).start();
  }
  /**
   * Check if data should be sent straight away or delayed because the minimum interval is not reached. Delay real
   * sending by 50ms in any case to make sure to catch all updates.
   */
  #prepareDataUpdate() {
    if (this.#sendDelayTimer.isRunning || this.#isClosed) {
      return;
    }
    if (!this.#sendUpdatesActivated) {
      return;
    }
    this.#updateTimer.stop();
    const now = Time.nowMs;
    const timeSinceLastUpdate = Millis(now - this.#lastUpdateTime);
    if (timeSinceLastUpdate < this.minIntervalFloor) {
      this.#updateTimer = Time.getTimer(
        "Subscription update",
        Millis(this.minIntervalFloor - timeSinceLastUpdate),
        () => this.#prepareDataUpdate()
      ).start();
      return;
    }
    this.#sendDelayTimer.start();
    this.#updateTimer = Time.getTimer(
      `Subscription update ${this.idStr}`,
      this.#sendInterval,
      () => this.#prepareDataUpdate()
    ).start();
  }
  #triggerSendUpdate(onlyWithData = false, session) {
    if (this.#currentUpdatePromise !== void 0) {
      logger.debug("Sending update already in progress, delaying update ...");
      this.#sendNextUpdateImmediately = true;
      return;
    }
    this.#currentUpdatePromise = this.#sendUpdate(onlyWithData, session).catch((error) => logger.warn("Sending subscription update failed:", error)).finally(() => this.#currentUpdatePromise = void 0);
  }
  /**
   * Determine all attributes that have changed since the last update and send them out to the subscriber.
   */
  async #sendUpdate(onlyWithData = false, session) {
    var _stack3 = [];
    try {
      const updating = __using(_stack3, this.#lifetime?.join("updating"));
      while (true) {
        const attributeFilter = this.#outstandingAttributeUpdates;
        this.#outstandingAttributeUpdates = void 0;
        const eventsMinNumber = this.#outstandingEventsMinNumber;
        this.#outstandingEventsMinNumber = void 0;
        if (onlyWithData && attributeFilter === void 0 && eventsMinNumber === void 0) {
          break;
        }
        this.#lastUpdateTime = Time.nowMs;
        try {
          var _stack = [];
          try {
            const sending = __using(_stack, updating?.join("sending"));
            if (await this.#sendUpdateMessage(sending, attributeFilter, eventsMinNumber, onlyWithData, session)) {
              this.#sendUpdateErrorCounter = 0;
            }
          } catch (_) {
            var _error = _, _hasError = true;
          } finally {
            __callDispose(_stack, _error, _hasError);
          }
        } catch (error) {
          if (this.#isClosed || this.#isCanceledByPeer) {
            return;
          }
          this.#sendUpdateErrorCounter++;
          logger.info(
            `Error sending subscription update message (error count=${this.#sendUpdateErrorCounter}):`,
            error instanceof MatterError && error.message || error
          );
          if (this.#sendUpdateErrorCounter <= 2) {
            if (attributeFilter !== void 0) {
              for (const [endpointId, clusters] of Object.entries(attributeFilter)) {
                for (const [clusterId, attributes] of Object.entries(clusters)) {
                  this.#addOutstandingAttributes(
                    EndpointNumber(parseInt(endpointId)),
                    ClusterId(parseInt(clusterId)),
                    Array.from(attributes)
                  );
                }
              }
            }
            if (eventsMinNumber !== void 0) {
              this.#outstandingEventsMinNumber = eventsMinNumber;
            }
          } else {
            logger.info(
              `Sending update failed 3 times in a row, canceling subscription ${this.idStr} and let controller subscribe again.`
            );
            this.#sendNextUpdateImmediately = false;
            if (error instanceof NoResponseTimeoutError || error instanceof NetworkError || error instanceof SessionClosedError) {
              var _stack2 = [];
              try {
                const _messaging = __using(_stack2, updating?.join("canceling"));
                this.#isCanceledByPeer = true;
                await this.#cancel();
                break;
              } catch (_2) {
                var _error2 = _2, _hasError2 = true;
              } finally {
                __callDispose(_stack2, _error2, _hasError2);
              }
            } else {
              throw error;
            }
          }
        }
        if (!this.#sendNextUpdateImmediately) {
          break;
        }
        logger.debug("Sending delayed update immediately after last one was sent");
        this.#sendNextUpdateImmediately = false;
        onlyWithData = true;
      }
    } catch (_3) {
      var _error3 = _3, _hasError3 = true;
    } finally {
      __callDispose(_stack3, _error3, _hasError3);
    }
  }
  /**
   * Returns an iterator that yields the data reports and events data for the given read request.
   */
  async *#processAttributesAndEventsReport(context, suppressStatusReports = false) {
    const request = {
      ...this.request,
      interactionModelRevision: Specification.INTERACTION_MODEL_REVISION
      // irrelevant here, set to our version
    };
    const delayedStatusReports = new Array();
    let hasValuesInResponse = false;
    let validAttributes = 0;
    let validEvents = 0;
    const session = RemoteActorContext(context).beginReadOnly();
    try {
      if (Read.containsAttribute(request)) {
        const attributeReader = new AttributeReadResponse(this.#context.node.protocol, session);
        for (const chunk of attributeReader.process(request)) {
          for await (const report of chunk) {
            if (report.kind === "attr-status") {
              if (suppressStatusReports) {
                continue;
              }
              if (!hasValuesInResponse) {
                delayedStatusReports.push(report);
                continue;
              }
            } else if (!hasValuesInResponse && report.kind === "attr-value") {
              for (const delayedReport of delayedStatusReports) {
                yield InteractionServerMessenger.convertServerInteractionReport(delayedReport);
              }
              delayedStatusReports.length = 0;
              hasValuesInResponse = true;
            }
            if (this.#seededClusterDetails !== void 0 && report.kind === "attr-value") {
              const {
                path: { endpointId, clusterId },
                version
              } = report;
              this.#seededClusterDetails.set(`${endpointId}-${clusterId}`, version);
            }
            yield InteractionServerMessenger.convertServerInteractionReport(report);
          }
        }
        validAttributes = attributeReader.counts.existent;
      }
      if (Read.containsEvent(request)) {
        const eventReader = new EventReadResponse(this.#context.node.protocol, session);
        for await (const chunk of eventReader.process(request)) {
          for await (const report of chunk) {
            if (report.kind === "event-status") {
              if (suppressStatusReports) {
                continue;
              }
              if (!hasValuesInResponse) {
                delayedStatusReports.push(report);
                continue;
              }
            } else if (!hasValuesInResponse && report.kind === "event-value") {
              for (const delayedReport of delayedStatusReports) {
                yield InteractionServerMessenger.convertServerInteractionReport(delayedReport);
              }
              delayedStatusReports.length = 0;
              hasValuesInResponse = true;
            }
            if (this.#latestSeededEventNumber !== void 0 && report.kind === "event-value") {
              this.#latestSeededEventNumber = report.number;
            }
            yield InteractionServerMessenger.convertServerInteractionReport(report);
          }
        }
        validEvents = eventReader.counts.existent;
      }
      if (validAttributes === 0 && validEvents === 0) {
        throw new StatusResponseError(
          "Subscription failed because no attributes or events are matching the query",
          Status.InvalidAction
        );
      } else if (!hasValuesInResponse && delayedStatusReports.length) {
        for (const delayedReport of delayedStatusReports) {
          yield InteractionServerMessenger.convertServerInteractionReport(delayedReport);
        }
      }
    } finally {
      session[Symbol.dispose]();
    }
    this.#lastUpdateTime = Time.nowMs;
  }
  async sendInitialReport(messenger, readContext, suppressStatusReports) {
    this.#updateTimer.stop();
    if (this.request.attributeRequests?.length) {
      this.#changeHandlers.on(
        this.#context.node.protocol.attrsChanged,
        this.#handleClusterStateChanges.bind(this)
      );
    }
    if (this.request.eventRequests?.length) {
      this.#changeHandlers.on(this.#context.node.protocol.eventHandler.added, this.#handleAddedEvents.bind(this));
    }
    await messenger.sendDataReport({
      baseDataReport: {
        suppressResponse: false,
        // we always need proper response for initial report
        subscriptionId: this.subscriptionId,
        interactionModelRevision: Specification.INTERACTION_MODEL_REVISION
      },
      forFabricFilteredRead: this.request.isFabricFiltered,
      payload: this.#processAttributesAndEventsReport(readContext, suppressStatusReports)
    });
  }
  async #flush(flushViaSession) {
    this.#sendDelayTimer.stop();
    if (this.#outstandingAttributeUpdates !== void 0 || this.#outstandingEventsMinNumber !== void 0) {
      logger.debug(`Flushing subscription ${this.idStr}${this.#isClosed ? " (for closing)" : ""}`);
      this.#triggerSendUpdate(true, flushViaSession);
      if (this.#currentUpdatePromise) {
        var _stack = [];
        try {
          const _waiting = __using(_stack, this.#lifetime?.join("waiting on flush"));
          await this.#currentUpdatePromise;
        } catch (_) {
          var _error = _, _hasError = true;
        } finally {
          __callDispose(_stack, _error, _hasError);
        }
      }
    }
  }
  /**
   * Closes the subscription and flushes all outstanding data updates if requested.
   */
  async close(flushViaSession) {
    if (this.#isClosed) {
      return;
    }
    this.#isClosed = true;
    await this.#cancel(flushViaSession);
    if (this.#currentUpdatePromise) {
      var _stack = [];
      try {
        const _waiting = __using(_stack, this.#lifetime?.closing()?.join("waiting on update"));
        await this.#currentUpdatePromise;
      } catch (_) {
        var _error = _, _hasError = true;
      } finally {
        __callDispose(_stack, _error, _hasError);
      }
    }
  }
  async #cancel(flushViaSession) {
    const closing = this.#lifetime?.closing();
    this.#sendUpdatesActivated = false;
    this.#changeHandlers.close();
    if (flushViaSession !== void 0) {
      var _stack = [];
      try {
        const _flushing = __using(_stack, closing?.join("flushing"));
        await this.#flush(flushViaSession);
      } catch (_) {
        var _error = _, _hasError = true;
      } finally {
        __callDispose(_stack, _error, _hasError);
      }
    }
    this.#updateTimer.stop();
    this.#sendDelayTimer.stop();
    this.session.subscriptions.delete(this);
    logger.debug(this.session.via, "Deleted subscription", hex.fixed(this.subscriptionId, 8));
    this.cancelled.emit(this);
  }
  /**
   * Iterates over all attributes and events that have changed since the last update and sends them to
   * the controller.
   * A thrown exception will cancel the sending process immediately.
   */
  async *#iterateDataUpdate(exchange, attributeFilter, eventsMinNumber) {
    const request = {
      ...this.request,
      interactionModelRevision: Specification.INTERACTION_MODEL_REVISION
      // irrelevant here, set to our version
    };
    const session = RemoteActorContext({
      activity: exchange[NodeActivity.activityKey],
      fabricFiltered: request.isFabricFiltered,
      exchange,
      node: this.#context.node
    }).beginReadOnly();
    try {
      if (attributeFilter !== void 0 && Read.containsAttribute(request)) {
        const attributeReader = new AttributeSubscriptionResponse(
          this.#context.node.protocol,
          session,
          attributeFilter
        );
        for (const chunk of attributeReader.process(request)) {
          for await (const report of chunk) {
            yield InteractionServerMessenger.convertServerInteractionReport(report);
          }
        }
      }
      if (eventsMinNumber !== void 0 && Read.containsEvent(request)) {
        request.eventFilters = [{ eventMin: eventsMinNumber }];
        const eventReader = new EventReadResponse(this.#context.node.protocol, session);
        for await (const chunk of eventReader.process(request)) {
          for await (const report of chunk) {
            if (report.kind === "event-status") {
              continue;
            }
            yield InteractionServerMessenger.convertServerInteractionReport(report);
          }
        }
      }
    } finally {
      session[Symbol.dispose]();
    }
  }
  async #sendUpdateMessage(lifetime, attributeFilter, eventsMinNumber, onlyWithData, session) {
    const exchange = this.#context.initiateExchange(session ?? this.#peerAddress, INTERACTION_PROTOCOL_ID);
    if (exchange === void 0) {
      return false;
    }
    this.#currentSendExchange = exchange;
    const messenger = new InteractionServerMessenger(exchange);
    try {
      if (attributeFilter === void 0 && eventsMinNumber === void 0) {
        var _stack = [];
        try {
          const _sending = __using(_stack, lifetime?.join("sending keepalive"));
          await messenger.sendDataReport({
            baseDataReport: {
              suppressResponse: true,
              // suppressResponse true for empty DataReports
              subscriptionId: this.subscriptionId,
              interactionModelRevision: Specification.INTERACTION_MODEL_REVISION
            },
            forFabricFilteredRead: this.request.isFabricFiltered,
            waitForAck: !this.#isClosed
            // Do not wait for ack when closed
          });
        } catch (_) {
          var _error = _, _hasError = true;
        } finally {
          __callDispose(_stack, _error, _hasError);
        }
      } else {
        var _stack2 = [];
        try {
          const _sending = __using(_stack2, lifetime?.join("sending data"));
          await messenger.sendDataReport({
            baseDataReport: {
              suppressResponse: false,
              // Non-empty data reports always need to send response
              subscriptionId: this.subscriptionId,
              interactionModelRevision: Specification.INTERACTION_MODEL_REVISION
            },
            forFabricFilteredRead: this.request.isFabricFiltered,
            payload: this.#iterateDataUpdate(exchange, attributeFilter, eventsMinNumber),
            waitForAck: !this.#isClosed,
            // Do not wait for ack when closed
            suppressEmptyReport: onlyWithData
          });
        } catch (_2) {
          var _error2 = _2, _hasError2 = true;
        } finally {
          __callDispose(_stack2, _error2, _hasError2);
        }
      }
    } catch (error) {
      var _stack3 = [];
      try {
        if (StatusResponseError.is(error, Status.InvalidSubscription, Status.Failure)) {
          logger.notice(`Subscription ${this.idStr} cancelled by peer`);
          this.#isCanceledByPeer = true;
        } else {
          StatusResponseError.accept(error);
          logger.info(`Subscription ${this.idStr} update failed:`, error);
        }
        const _canceling = __using(_stack3, lifetime?.join("canceling"));
        await this.#cancel();
      } catch (_3) {
        var _error3 = _3, _hasError3 = true;
      } finally {
        __callDispose(_stack3, _error3, _hasError3);
      }
    } finally {
      var _stack4 = [];
      try {
        this.#currentSendExchange = void 0;
        const _closing = __using(_stack4, lifetime?.join("closing messenger"));
        await messenger.close();
      } catch (_4) {
        var _error4 = _4, _hasError4 = true;
      } finally {
        __callDispose(_stack4, _error4, _hasError4);
      }
    }
    return true;
  }
}
export {
  DEFAULT_RANDOMIZATION_WINDOW,
  INTERNAL_INTERVAL_PUBLISHER_LIMIT,
  MAX_INTERVAL_PUBLISHER_LIMIT,
  MIN_INTERVAL,
  ServerSubscription,
  ServerSubscriptionConfig
};
//# sourceMappingURL=ServerSubscription.js.map
