export default defineNuxtPlugin(async () => {
  const { token } = useAuth();
  const anonymousClientId = useState<string | null>("anonymous-push-client-id", () => null);
  const { syncServiceWorkerContext, syncNativeSubscription, isNativeApp } = usePush();

  if (isNativeApp()) {
    await syncNativeSubscription();
  } else {
    await syncServiceWorkerContext();
  }

  watch([token, anonymousClientId], () => {
    if (isNativeApp()) {
      void syncNativeSubscription();
      return;
    }

    void syncServiceWorkerContext();
  });
});
