import type {
  DefinitionInject,
  Questionnaire,
} from '@inject/graphql/fragment-types'

export type AutoInject = DefinitionInject | Questionnaire

export const getAutoInjectName = (autoInject: AutoInject) => {
  switch (autoInject.__typename) {
    case 'DefinitionInjectType':
      return autoInject.name
    case 'QuestionnaireType':
      return autoInject.title
  }
}

export const getAutoInjectType = (autoInject: AutoInject) => {
  switch (autoInject.__typename) {
    case 'DefinitionInjectType':
      return autoInject.type
    case 'QuestionnaireType':
      return 'FORM'
  }
}

export const getAutoInjectTime = (autoInject: AutoInject) => {
  switch (autoInject.__typename) {
    case 'DefinitionInjectType':
      return autoInject.time + autoInject.delay
    case 'QuestionnaireType':
      return autoInject.time
  }
}

export const getAutoInjectFilter =
  (
    displayAutoInjects: boolean,
    displayAutoEmails: boolean,
    displayAutoQuestionnaires: boolean
  ) =>
  (autoInject: DefinitionInject | Questionnaire) => {
    switch (autoInject.__typename) {
      case 'DefinitionInjectType': {
        switch (autoInject.type) {
          case 'INFO':
            return displayAutoInjects
          case 'EMAIL':
            return displayAutoEmails
          default:
            return false
        }
      }
      case 'QuestionnaireType':
        return displayAutoQuestionnaires
    }
  }
