'use client';

const DEVICE_KEY = 'support_device_id';

/**
 * Stable per-device id for the portal's single support thread. Persisted in
 * localStorage — no login or form required; the visitor is dropped straight
 * into their one ongoing conversation.
 */
export function getDeviceId(): string {
  if (typeof window === 'undefined') return '';
  let id = localStorage.getItem(DEVICE_KEY);
  if (!id) {
    id =
      typeof crypto !== 'undefined' && 'randomUUID' in crypto
        ? crypto.randomUUID()
        : `device-${Date.now()}-${Math.floor(Math.random() * 1e9)}`;
    localStorage.setItem(DEVICE_KEY, id);
  }
  return id;
}
