import { createHash } from "node:crypto";

export function strongETag(body) {
  return `"${createHash("sha1").update(body).digest("hex")}"`;
}

export function ifNoneMatchHits(header, etag) {
  if (!header || !etag) return false;

  const tags = String(header)
    .split(",")
    .map((part) => part.trim())
    .filter(Boolean);

  if (tags.includes("*")) return true;

  return tags.some((tag) => {
    const normalized = tag.startsWith("W/") ? tag.slice(2).trim() : tag;
    const expected = etag.startsWith("W/") ? etag.slice(2).trim() : etag;
    return normalized === expected || tag === etag;
  });
}
