export type CanonicalIntent = 'customer' | 'professional' | 'company' | 'job_seeker' | null;

const INTENT_KEY = 'nxtgauge_intent_v1';

export function normalizeIntent(value: string | null | undefined): CanonicalIntent {
  if (!value) return null;
  const normalized = value.trim().toLowerCase();
  if (!normalized) return null;
  if (normalized === 'customer') return 'customer';
  if (normalized === 'professional' || normalized === 'pro') return 'professional';
  if (normalized === 'company' || normalized === 'employer') return 'company';
  if (normalized === 'job_seeker' || normalized === 'job-seeker' || normalized === 'jobseeker') return 'job_seeker';
  return null;
}

export function intentToOnboardingPath(intent: CanonicalIntent): string {
  if (intent === 'company') return '/signup?intent=company';
  if (intent === 'job_seeker') return '/signup?intent=job_seeker';
  if (intent === 'professional') return '/signup?intent=professional';
  return '/signup?intent=customer';
}

export function saveCanonicalIntent(intent: CanonicalIntent): void {
  if (typeof window === 'undefined') return;
  if (!intent) {
    window.localStorage.removeItem(INTENT_KEY);
    return;
  }
  window.localStorage.setItem(INTENT_KEY, intent);
}

export function readCanonicalIntent(): CanonicalIntent {
  if (typeof window === 'undefined') return null;
  return normalizeIntent(window.localStorage.getItem(INTENT_KEY));
}
