const normalize = (percent) => {
  if (typeof percent !== "number" || Number.isNaN(percent)) return 0;
  if (percent >= 100) return 100;
  if (percent <= 0) return 0;
  return percent;
};
const convertToDivisibleBy5 = (percent) => Math.round(normalize(percent) / 5) * 5;
const percentToFraction = (percent) => {
  const numerator = percent;
  const denominator = 100;
  const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
  const divisor = gcd(numerator, denominator);
  return { numerator: numerator / divisor, denominator: denominator / divisor };
};
class HeadSampler {
  /**
   * Upper bound of the sampling size
   */
  #upperBound = 0;
  /**
   * Total size of the sampling
   */
  #totalSize = 0;
  /**
   * Number of events sampled
   */
  #sampled = 0;
  /**
   * Creates new instance of HeadSampler
   * @param samplingPercent sampling percentage
   */
  constructor(samplingPercent) {
    const value = percentToFraction(convertToDivisibleBy5(samplingPercent));
    this.#upperBound = value.numerator;
    this.#totalSize = value.denominator;
  }
  /**
   * check if the event should be sampled
   * @returns true if the event should be sampled
   */
  shouldSample = () => {
    this.#sampled += 1;
    if (this.#sampled <= this.#upperBound) return true;
    if (this.#sampled === this.#totalSize) this.#sampled = 0;
    return false;
  };
}
export {
  HeadSampler
};
