"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 MRP_exports = {};
__export(MRP_exports, {
  MRP: () => MRP
});
module.exports = __toCommonJS(MRP_exports);
var import_general = require("#general");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
var MRP;
((MRP2) => {
  MRP2.MAX_TRANSMISSIONS = 5;
  MRP2.BACKOFF_BASE = 1.6;
  MRP2.BACKOFF_JITTER = 0.25;
  MRP2.BACKOFF_MARGIN = 1.1;
  MRP2.BACKOFF_THRESHOLD = 1;
  MRP2.STANDALONE_ACK_TIMEOUT = (0, import_general.Millis)(200);
  MRP2.DEFAULT_EXPECTED_PROCESSING_TIME = (0, import_general.Seconds)(2);
  MRP2.ADDITIONAL_MRP_DELAY = (0, import_general.Seconds)(1.5);
  const PEER_RESPONSE_TIME_BUFFER = (0, import_general.Seconds)(5);
  function maxPeerResponseTimeOf({
    peerSessionParameters,
    localSessionParameters,
    channelType,
    isPeerActive,
    usesMrp = channelType === import_general.ChannelType.UDP,
    expectedProcessingTime = MRP2.DEFAULT_EXPECTED_PROCESSING_TIME
  }) {
    switch (channelType) {
      case "tcp":
        return (0, import_general.Millis)((0, import_general.Seconds)(30) + PEER_RESPONSE_TIME_BUFFER);
      case "udp":
        if (!usesMrp) {
          throw new import_general.MatterFlowError("No response expected for this message exchange because UDP and no MRP");
        }
        return (0, import_general.Millis)(
          (peerSessionParameters !== void 0 ? maxResponseTimeOf(peerSessionParameters, isPeerActive) : 0) + maxResponseTimeOf(localSessionParameters, true) + // We consider us as always active initially
          expectedProcessingTime + PEER_RESPONSE_TIME_BUFFER
        );
      case "ble":
        return (0, import_general.Millis)((0, import_general.Seconds)(30) + PEER_RESPONSE_TIME_BUFFER);
      default:
        throw new import_general.MatterFlowError(
          `Can not calculate expected timeout for unknown channel type: ${channelType}`
        );
    }
  }
  MRP2.maxPeerResponseTimeOf = maxPeerResponseTimeOf;
  function maxRetransmissionIntervalOf(inputs) {
    return retransmissionIntervalOf(inputs, true);
  }
  MRP2.maxRetransmissionIntervalOf = maxRetransmissionIntervalOf;
  function retransmissionIntervalOf({ transmissionNumber, sessionParameters, isPeerActive }, calculateMaximum = false) {
    const { activeInterval, idleInterval } = sessionParameters;
    const peerActive = transmissionNumber > 0 && (!calculateMaximum || isPeerActive);
    let baseInterval = peerActive ? activeInterval : idleInterval;
    if (!calculateMaximum) {
      baseInterval += MRP2.ADDITIONAL_MRP_DELAY;
    }
    return import_general.Millis.floor(
      (0, import_general.Millis)(
        baseInterval * MRP2.BACKOFF_MARGIN * Math.pow(MRP2.BACKOFF_BASE, Math.max(0, transmissionNumber - MRP2.BACKOFF_THRESHOLD)) * (1 + (calculateMaximum ? 1 : Math.random()) * MRP2.BACKOFF_JITTER)
      )
    );
  }
  MRP2.retransmissionIntervalOf = retransmissionIntervalOf;
})(MRP || (MRP = {}));
function maxResponseTimeOf(sessionParameters, isPeerActive) {
  let finalWaitTime = 0;
  for (let i = 0; i < MRP.MAX_TRANSMISSIONS; i++) {
    if (isPeerActive && finalWaitTime > sessionParameters.activeThreshold) {
      isPeerActive = false;
    }
    finalWaitTime = (0, import_general.Millis)(
      finalWaitTime + MRP.maxRetransmissionIntervalOf({
        transmissionNumber: i,
        sessionParameters,
        isPeerActive
      })
    );
  }
  return finalWaitTime;
}
//# sourceMappingURL=MRP.js.map
