export const AUTH_EXPIRED_EVENT = "dms:auth-expired";

export interface AuthExpiredDetail {
  requestId: string;
}

type AuthExpiredListener = (detail: AuthExpiredDetail) => void;

const listeners = new Set<AuthExpiredListener>();
let authExpiredPublished = false;

export function subscribeAuthExpired(listener: AuthExpiredListener): () => void {
  listeners.add(listener);
  return () => listeners.delete(listener);
}

export function publishAuthExpired(detail: AuthExpiredDetail): boolean {
  if (authExpiredPublished) return false;
  authExpiredPublished = true;
  for (const listener of listeners) listener(detail);
  return true;
}

export function resetAuthExpiredNotification(): void {
  authExpiredPublished = false;
}
