import { ConfigProvider } from 'antd'
import en_GB from 'antd/lib/locale-provider/en_GB'
import 'moment/locale/en-gb'
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { BrowserRouter as Router } from 'react-router-dom'
import store from 'redux/store'
import App from './App'
import * as serviceWorker from './serviceWorker'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { LinkRedirectProvider } from 'context/LinkRedirectContext'
import {
  ConfirmationModalProvider,
  KomiModalsProvider
} from '@komi-app/components'

import config from 'config'

const queryClient = new QueryClient()
import { Context } from '@growthbook/growthbook-react'
import { FEATURE_LIST_LOCAL_STORAGE } from 'constants/feature-flags'

import { FeatureProvider } from '@komi-app/flags-sdk'

const featureFlagsConfig: Context = {
  apiHost: config.flags.host,
  clientKey: config.flags.key,
  enableDevMode: config.service.env !== 'production',
  trackingCallback: (experiment, result) => {
    // TODO: Use your real analytics tracking system
    console.log('Viewed Experiment', {
      experimentId: experiment.key,
      variationId: result.variationId
    })
  },
  attributes: {
    domain: window.location.hostname,
    featureList: localStorage.getItem(FEATURE_LIST_LOCAL_STORAGE)?.split(',')
  }
}

ReactDOM.render(
  <FeatureProvider context={featureFlagsConfig}>
    <Provider store={store}>
      <QueryClientProvider client={queryClient}>
        <Router>
          <ConfigProvider locale={en_GB}>
            <ConfirmationModalProvider>
              <LinkRedirectProvider>
                <KomiModalsProvider>
                  <App />
                </KomiModalsProvider>
              </LinkRedirectProvider>
            </ConfirmationModalProvider>
          </ConfigProvider>
        </Router>
      </QueryClientProvider>
    </Provider>
  </FeatureProvider>,
  document.getElementById('root')
)
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister()
