import React, { createContext, useContext } from 'react'
import { RJT_CONFIG } from '../core'

const context = createContext<RJT_CONFIG | null>(null)
export const RJTProvider: React.FC<{ config: RJT_CONFIG, children: any }> = ({ config, children }) => {
  return (
    <context.Provider value={config}>
      {children}
    </context.Provider>
  )
}

export const useRJTConfig = (): RJT_CONFIG => {
  const config = useContext(context)
  if (config === null) {
    throw new Error('RJT is not configured, make sure to use this hook inside a RJTProvider !')
  }

  return config
}
