// currently `useErrorMonitor` is not always available - see https://wix.slack.com/archives/G011G2JCGGM/p1630590305001100
import { ErrorMonitor } from '@wix/bex-core';
import {
  ComponentHooksFlowAPI,
  useErrorMonitor,
  useSentry,
} from '@wix/yoshi-flow-bm';

export function useErrorMonitorCompat(): ErrorMonitor {
  let sentry: ErrorMonitor | null = null;
  let errorMonitor: ErrorMonitor | null = null;
  try {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    errorMonitor = useErrorMonitor();
  } catch (e) {}

  try {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    sentry = useSentry();
  } catch (e) {}

  return (errorMonitor ?? sentry)!;
}

export function getErrorMonitorCompat(
  flowAPI: ComponentHooksFlowAPI,
): ErrorMonitor {
  let sentry: ErrorMonitor | null = null;
  let errorMonitor: ErrorMonitor | null = null;
  try {
    errorMonitor = flowAPI.errorMonitor;
  } catch (e) {}

  try {
    sentry = flowAPI.sentry;
  } catch (e) {}

  return (errorMonitor ?? sentry)!;
}
