const COOKIE_NAME = 'cahoo_aff';
const MAX_AGE = 60 * 60 * 24 * 30; // 30 days

export function setAffiliateCookie(code: string): void {
  document.cookie = `${COOKIE_NAME}=${encodeURIComponent(code)}; max-age=${MAX_AGE}; path=/; SameSite=Lax`;
}

export function getAffiliateCookie(): string | null {
  if (typeof document === 'undefined') return null;
  const match = document.cookie
    .split('; ')
    .find((row) => row.startsWith(`${COOKIE_NAME}=`));
  if (!match) return null;
  return decodeURIComponent(match.split('=')[1]);
}
