export function getDeviceId(): string {
  const KEY = 'device-id';
  let id = localStorage.getItem(KEY);
  if (!id) {
    id = genId();
    localStorage.setItem(KEY, id);
  }
  return id;
}

function genId() {
  // Simple UUID v4-ish generator
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    const r = (crypto.getRandomValues(new Uint8Array(1))[0] & 0xf) >> 0;
    const v = c === 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}

