import {useEffect} from 'react';
import {AppProps} from 'next/app';
import {ApolloProvider} from '@apollo/client';
import {ThemeProvider} from '@material-ui/core/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import {MuiPickersUtilsProvider} from '@material-ui/pickers';
import {SessionProvider} from 'next-auth/react';
import moment from 'moment';
import MomentUtils from '@date-io/moment';
import {useApollo} from '../lib/apolloClient';
import Metas from '../containers/Metas';
import Toasts from '../components/Toasts';
import theme from '../theme';
import useProfile from '../hooks/useProfile';
import useLocale from '../hooks/useLocale';
import {I18nextProvider} from 'react-i18next';
import i18n, {initI18Next} from '../lib/i18n';

const App = function (props: AppProps) {
  const {Component, pageProps} = props;
  const apolloClient = useApollo(pageProps);
  const {locale} = useLocale();

  useEffect(() => {
    // Remove the server-side injected CSS.
    const jssStyles = document.querySelector('#jss-server-side');
    if (jssStyles) {
      jssStyles.parentElement!.removeChild(jssStyles);
    }
  }, []);

  initI18Next(locale);

  return (
    <I18nextProvider i18n={i18n}>
      <ApolloProvider client={apolloClient}>
        <Metas metas={pageProps.metas} />
        <ThemeProvider theme={theme}>
          <MuiPickersUtilsProvider
            libInstance={moment}
            utils={MomentUtils}
            locale={locale === 'fr' ? 'fr-ch' : 'en'}
          >
            <CssBaseline />
            <Component {...pageProps} />
            <Toasts />
          </MuiPickersUtilsProvider>
        </ThemeProvider>
      </ApolloProvider>
    </I18nextProvider>
  );
};

const AppWrapper = (props: AppProps) => (
  <SessionProvider session={props?.pageProps.session} basePath="/api/nauth">
    <App {...props} />
  </SessionProvider>
);

export default AppWrapper;
