import { BrowserRouter } from 'react-router-dom'
import ConfirmContextProvider from '../components/PopupConfirm/ConfirmContextProvider'
import MuiThemeProvider from '../theme/MuiThemeProvider'
import QueryClientProvider from './QueryClientProvider'

import Cookies from 'js-cookie'
import { ReactNode } from 'react'
import { ToastContainer } from '../components'
import DialogProvider from '../components/DrawerWrapper/DrawerWrapper'
import GlobalNetworkLoadingIndicator from '../components/ErrorBoundary/GlobalNetworkLoadingIndicator'
import ErrorModalProvider from '../components/ErrorModalWrapper/ErrorModalWrapper'
import RootModal from './RootModal'

export const campxTenantKey = Cookies.get('campx_tenant')
export const urlTenantKey = Cookies.get('campx_tenant')
export const instituitionKey = window.location.pathname.split('/')[2]

export default function Providers({ children }: { children: ReactNode }) {
  return (
    <BrowserRouter>
      <QueryClientProvider>
        <MuiThemeProvider>
          <ConfirmContextProvider>
            <DialogProvider>
              <ErrorModalProvider>{children}</ErrorModalProvider>
              <GlobalNetworkLoadingIndicator />
              <RootModal />
              <ToastContainer />
            </DialogProvider>
          </ConfirmContextProvider>
        </MuiThemeProvider>
      </QueryClientProvider>
    </BrowserRouter>
  )
}
