import { QueryClient } from '@tanstack/react-query';

import { NavProps } from '../../components/Routes/Routes';
import { UNAUTHORIZED_ERROR } from '../../constants/custom';
import { showFeedback } from '../feedback/showFeedback';

export const client = new QueryClient({
  defaultOptions: {
    queries: {
      retry: 0,
      staleTime: 1000 * 60 * 60 * 24,
    },
  },
});

export const updateQueryClient = (navigation: NavProps) => {
  client.setDefaultOptions({
    ...client.getDefaultOptions(),
    queries: {
      ...client.getDefaultOptions().queries,
      onError: (err: any) => {
        if (err?.includes('Uživatel byl odhlášen')) {
          navigation.replace('Landing');
        }
        if (err?.includes(UNAUTHORIZED_ERROR)) {
          showFeedback({
            title: '',
            type: 'danger',
            err,
          });
        }
      },
    },
  });
};
