import { DEVICE_TOKEN_KEY } from "./constants";

export function getDeviceToken(): string {
  if (typeof window === "undefined") {
    return "";
  }

  const existing = window.localStorage.getItem(DEVICE_TOKEN_KEY);
  if (existing) {
    return existing;
  }

  const token =
    typeof crypto !== "undefined" && "randomUUID" in crypto
      ? crypto.randomUUID()
      : `web-${Date.now()}-${Math.random().toString(36).slice(2)}`;

  window.localStorage.setItem(DEVICE_TOKEN_KEY, token);
  return token;
}
