/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Instant, Millis, Seconds } from "#time/TimeUnit.js";
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 = Seconds.one,
      backoffFactor = 2
    } = this.config;
    let count = 0;
    let baseInterval = initialInterval;
    let timeSoFar = Instant;
    while ((timeout === void 0 || timeSoFar < timeout) && (maximumCount === void 0 || maximumCount > count)) {
      count++;
      const maxJitter = jitterFactor * baseInterval;
      const jitter = Millis.floor(Millis(maxJitter * this.#entropy.randomUint32 / Math.pow(2, 32)));
      let interval = Millis(baseInterval + jitter);
      if (timeout !== void 0 && timeSoFar + interval > timeout) {
        interval = Millis(timeout - timeSoFar);
      }
      if (maximumInterval !== void 0 && interval > maximumInterval) {
        interval = maximumInterval;
      }
      yield interval;
      timeSoFar = Millis(timeSoFar + interval);
      baseInterval = Millis(baseInterval * backoffFactor);
    }
  }
}
((RetrySchedule2) => {
  function Configuration(defaults, config) {
    return {
      ...defaults,
      ...config
    };
  }
  RetrySchedule2.Configuration = Configuration;
})(RetrySchedule || (RetrySchedule = {}));
export {
  RetrySchedule
};
//# sourceMappingURL=RetrySchedule.js.map
