/**
 * Normalize job listing URLs so the same role is not stored twice when only
 * tracking params, scheme, or trivial path differences differ.
 */

const TRACKING_QUERY_PREFIXES = ["utm_", "stm_"] as const;

const DROP_QUERY_KEYS = new Set([
  "ref",
  "src",
  "fbclid",
  "gclid",
  "mc_eid",
  "icid",
]);

export function canonicalizeJobUrl(raw: string): string {
  const trimmed = raw.trim();
  if (!trimmed) return trimmed;

  try {
    const u = new URL(trimmed);
    u.hash = "";

    let host = u.hostname.toLowerCase();
    if (host.startsWith("www.")) host = host.slice(4);
    u.hostname = host;
    u.protocol = "https:";

    for (const key of [...u.searchParams.keys()]) {
      const lower = key.toLowerCase();
      if (
        DROP_QUERY_KEYS.has(lower) ||
        TRACKING_QUERY_PREFIXES.some((prefix) => lower.startsWith(prefix))
      ) {
        u.searchParams.delete(key);
      }
    }

    const sortedKeys = [...u.searchParams.keys()].sort((a, b) =>
      a.localeCompare(b),
    );
    const next = new URLSearchParams();
    for (const k of sortedKeys) {
      for (const v of u.searchParams.getAll(k)) {
        next.append(k, v);
      }
    }
    u.search = next.toString() ? `?${next.toString()}` : "";

    let path = u.pathname;
    if (path.length > 1 && path.endsWith("/")) {
      path = path.slice(0, -1);
    }
    u.pathname = path || "/";

    return u.toString();
  } catch {
    return trimmed;
  }
}
