const TRACKING_PARAM_PREFIXES = ["utm_"];
const TRACKING_PARAM_EXACT = /* @__PURE__ */ new Set(["gclid", "fbclid"]);
const DEFAULT_CASE_INSENSITIVE_HOSTS = /* @__PURE__ */ new Set([
  "microsoft.com",
  "learn.microsoft.com",
  "docs.microsoft.com",
  "msdn.microsoft.com",
  "support.microsoft.com",
  "archive.org",
  "web.archive.org"
]);
function isTrackingParam(name) {
  if (TRACKING_PARAM_EXACT.has(name)) return true;
  for (const p of TRACKING_PARAM_PREFIXES) if (name.startsWith(p)) return true;
  return false;
}
function caseInsensitiveHosts() {
  const set = new Set(DEFAULT_CASE_INSENSITIVE_HOSTS);
  const extra = process.env.WIGOLO_DEDUP_CASE_INSENSITIVE_HOSTS;
  if (extra) {
    for (const raw of extra.split(",")) {
      const h = raw.trim().toLowerCase();
      if (h) set.add(h);
    }
  }
  return set;
}
function shouldLowercasePathForHost(hostname) {
  const h = hostname.toLowerCase();
  const hosts = caseInsensitiveHosts();
  if (hosts.has(h)) return true;
  for (const known of hosts) {
    if (h.endsWith("." + known)) return true;
  }
  return false;
}
function normalizeUrlForDedup(url) {
  const u = new URL(url);
  u.hostname = u.hostname.toLowerCase();
  u.hash = "";
  if (u.protocol === "http:" && u.port === "80" || u.protocol === "https:" && u.port === "443") {
    u.port = "";
  }
  if (shouldLowercasePathForHost(u.hostname)) {
    u.pathname = u.pathname.toLowerCase();
  }
  const params = [...u.searchParams.entries()].filter(([k]) => !isTrackingParam(k));
  params.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
  u.search = "";
  for (const [k, v] of params) u.searchParams.append(k, v);
  let out = u.toString();
  if (u.pathname !== "/" && out.endsWith("/") && !out.includes("?")) {
    out = out.slice(0, -1);
  } else if (u.pathname === "/" && !u.search) {
    out = out.replace(/\/$/, "");
  }
  return out;
}
function tryNormalize(url) {
  try {
    return normalizeUrlForDedup(url);
  } catch {
    return null;
  }
}
function dedupAgainstRecentUrls(results, recentUrls) {
  if (!recentUrls || recentUrls.length === 0) return results;
  const seen = /* @__PURE__ */ new Set();
  for (const u of recentUrls) {
    const n = tryNormalize(u);
    if (n !== null) seen.add(n);
  }
  if (seen.size === 0) return results;
  return results.filter((r) => {
    const n = tryNormalize(r.url);
    if (n === null) return true;
    return !seen.has(n);
  });
}
export {
  dedupAgainstRecentUrls,
  normalizeUrlForDedup,
  shouldLowercasePathForHost
};
//# sourceMappingURL=recent-cache-dedup.js.map