import App from 'next/app'
import { ApolloProvider } from '@apollo/client'

import Page from '../src/app/components/Page'
import client from '../src/lib/apollo'
import { StoreProvider } from '../src/lib/store'

import circuit2 from '../circuit2'
import circuit1 from '../circuit1'
import homework from '../homework'
import corona1 from '../corona1'
import { createContext } from 'react'

const trainings = [corona1, homework, circuit1, circuit2]

export const TrainingContext = createContext(trainings)

class MyApp extends App {
  static async getInitialProps({ Component, ctx }: any) {
    let pageProps: any = {}

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx)
    }

    // Add the query object to the pageProps
    // https://github.com/wesbos/Advanced-React/blob/master/finished-application/frontend/pages/_app.js
    pageProps.query = ctx.query
    return { pageProps }
  }

  render() {
    const { Component, pageProps } = this.props

    return (
      <ApolloProvider client={client}>
        <TrainingContext.Provider value={trainings}>
          <Page>
            <Component {...pageProps} />
          </Page>
        </TrainingContext.Provider>
      </ApolloProvider>
    )
  }
}

export default MyApp
