import ExerciseContext from '@/analyst/ExerciseContext'
import { useTypedQuery } from '@inject/graphql/graphql'
import {
  GetAutoInjects,
  GetExerciseQuestionnaires,
} from '@inject/graphql/queries'
import { useContext, useMemo } from 'react'
import type { AutoInject } from '../Overview/AutoInject/utils'

const useAutoInjects = (): AutoInject[] => {
  const { exercise } = useContext(ExerciseContext)

  const [{ data: autoInjectsData }] = useTypedQuery({
    query: GetAutoInjects,
    variables: {
      exerciseId: exercise.id,
    },
    context: useMemo(
      () => ({
        suspense: true,
      }),
      []
    ),
  })
  const [{ data: questionnairesData }] = useTypedQuery({
    query: GetExerciseQuestionnaires,
    variables: {
      exerciseId: exercise.id,
    },
    context: useMemo(
      () => ({
        suspense: true,
      }),
      []
    ),
  })

  return [
    ...(autoInjectsData?.injects || []),
    ...(questionnairesData?.questionnaires || []),
  ]
}

export default useAutoInjects
