"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 RetrySchedule_exports = {};
__export(RetrySchedule_exports, {
  RetrySchedule: () => RetrySchedule
});
module.exports = __toCommonJS(RetrySchedule_exports);
var import_TimeUnit = require("#time/TimeUnit.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class RetrySchedule {
  #entropy;
  config;
  constructor(entropy, config) {
    this.#entropy = entropy;
    this.config = config;
  }
  /**
   * Yields intervals.
   *
   * Will yield indefinitely until canceled unless {@link config} specifies a timeout.
   */
  *[Symbol.iterator]() {
    const {
      timeout,
      maximumInterval,
      maximumCount,
      jitterFactor = 0,
      initialInterval = import_TimeUnit.Seconds.one,
      backoffFactor = 2
    } = this.config;
    let count = 0;
    let baseInterval = initialInterval;
    let timeSoFar = import_TimeUnit.Instant;
    while ((timeout === void 0 || timeSoFar < timeout) && (maximumCount === void 0 || maximumCount > count)) {
      count++;
      const maxJitter = jitterFactor * baseInterval;
      const jitter = import_TimeUnit.Millis.floor((0, import_TimeUnit.Millis)(maxJitter * this.#entropy.randomUint32 / Math.pow(2, 32)));
      let interval = (0, import_TimeUnit.Millis)(baseInterval + jitter);
      if (timeout !== void 0 && timeSoFar + interval > timeout) {
        interval = (0, import_TimeUnit.Millis)(timeout - timeSoFar);
      }
      if (maximumInterval !== void 0 && interval > maximumInterval) {
        interval = maximumInterval;
      }
      yield interval;
      timeSoFar = (0, import_TimeUnit.Millis)(timeSoFar + interval);
      baseInterval = (0, import_TimeUnit.Millis)(baseInterval * backoffFactor);
    }
  }
}
((RetrySchedule2) => {
  function Configuration(defaults, config) {
    return {
      ...defaults,
      ...config
    };
  }
  RetrySchedule2.Configuration = Configuration;
})(RetrySchedule || (RetrySchedule = {}));
//# sourceMappingURL=RetrySchedule.js.map
