import { createLogger } from "../logger.js";
const log = createLogger("fetch");
function hashString(str) {
  let hash = 0;
  for (let i = 0; i < str.length; i++) {
    const char = str.charCodeAt(i);
    hash = (hash << 5) - hash + char;
    hash = hash & hash;
  }
  return Math.abs(hash);
}
class BrowserSelector {
  types;
  strategy;
  roundRobinIndex = 0;
  constructor(types, strategy) {
    if (types.length === 0) {
      throw new Error("BrowserSelector requires at least one browser type");
    }
    this.types = [...types];
    this.strategy = strategy ?? "round-robin";
    log.debug("browser selector initialized", {
      types: this.types,
      strategy: this.strategy
    });
  }
  select() {
    switch (this.strategy) {
      case "round-robin":
        return this.selectRoundRobin();
      case "random":
        return this.selectRandom();
      case "hostname-hash":
        return this.selectRoundRobin();
      default:
        return this.selectRoundRobin();
    }
  }
  selectForHostname(hostname) {
    if (this.strategy === "hostname-hash" && hostname) {
      return this.selectByHostnameHash(hostname);
    }
    return this.select();
  }
  getTypes() {
    return [...this.types];
  }
  getStrategy() {
    return this.strategy;
  }
  reset() {
    this.roundRobinIndex = 0;
  }
  selectRoundRobin() {
    const type = this.types[this.roundRobinIndex % this.types.length];
    this.roundRobinIndex++;
    return type;
  }
  selectRandom() {
    const index = Math.floor(Math.random() * this.types.length);
    return this.types[index];
  }
  selectByHostnameHash(hostname) {
    const hash = hashString(hostname);
    const index = hash % this.types.length;
    return this.types[index];
  }
}
export {
  BrowserSelector
};
//# sourceMappingURL=browser-selector.js.map