function parseCookieHeader(cookieHeader) {
  const cookies = {};
  if (!cookieHeader || typeof cookieHeader !== "string") return cookies;

  const segments = cookieHeader.split(";");
  for (const segment of segments) {
    const index = segment.indexOf("=");
    if (index === -1) continue;
    const key = segment.slice(0, index).trim();
    const value = segment.slice(index + 1).trim();
    if (!key) continue;
    try {
      cookies[key] = decodeURIComponent(value);
    } catch (_) {
      // Ignore malformed cookie values instead of throwing.
      continue;
    }
  }

  return cookies;
}

module.exports = {
  parseCookieHeader,
};
