import { gql } from 'graphql-request'
import fetchAPI from '@/lib/api/fetchAPI'

type FormField = {
  type: string
  label: string
  placeholder: string
}

type Confirmation = {
  message: string
}

export type NewsletterForm = {
  id: string
  submitButton: {
    text: string
  }
  title: string
  formFields: {
    nodes: FormField[]
  }
  confirmations: Confirmation[]
}

type NewsletterGfForm = {
  gfForm: NewsletterForm
}

/**
 * Query for the Newsletter Form from Gravity Forms
 */
export async function getFooterNewsletterForm() {
  const data: NewsletterGfForm = await fetchAPI(
    gql`
      query FooterNewsletterFrom {
        gfForm(id: "1", idType: DATABASE_ID) {
          id
          submitButton {
            text
          }
          title
          formFields {
            nodes {
              type
              ... on EmailField {
                label
                placeholder
              }
            }
          }
          confirmations {
            message
          }
        }
      }
    `
  )

  return data?.gfForm
}
