import { computed } from 'vue'
import { buildPublicLocaleSwitchContext, useRegisterPublicLocaleSwitchContext } from '~/composables/usePublicLocaleSwitchContext'
import { useResolvedPage } from '~/content/pages'
import { buildResolvedPageSeo } from '~/content/utils'

export function buildFaqPageSeo(page, origin = '') {
  return buildResolvedPageSeo(page, origin, {
    ogImage: page?.heroImage || page?.featuredImage || '',
  })
}

export function useFaqContent() {
  const faqContentPage = useResolvedPage('faq')
  const localeSwitchContext = computed(() => buildPublicLocaleSwitchContext(faqContentPage.value, { kind: 'list' }))

  useRegisterPublicLocaleSwitchContext(localeSwitchContext)

  const faqCategories = computed(() => [...(faqContentPage.value?.structuredContent?.defaultCategories || [])])

  const faqUi = computed(() => ({
    searchPlaceholder: faqContentPage.value?.structuredContent?.ui?.searchPlaceholder,
    categoriesHeading: faqContentPage.value?.structuredContent?.ui?.categoriesHeading,
    supportTitle: faqContentPage.value?.structuredContent?.ui?.supportTitle,
    supportDescription: faqContentPage.value?.structuredContent?.ui?.supportDescription,
    supportPhone: faqContentPage.value?.structuredContent?.ui?.supportPhone,
    supportCtaLabel: faqContentPage.value?.structuredContent?.ui?.supportCtaLabel,
    searchResultsTitle: faqContentPage.value?.structuredContent?.ui?.searchResultsTitle,
    emptySearchTitle: faqContentPage.value?.structuredContent?.ui?.emptySearchTitle,
    emptySearchDescription: faqContentPage.value?.structuredContent?.ui?.emptySearchDescription,
    clearSearchLabel: faqContentPage.value?.structuredContent?.ui?.clearSearchLabel,
    footerTitle: faqContentPage.value?.structuredContent?.ui?.footerTitle,
    footerDescription: faqContentPage.value?.structuredContent?.ui?.footerDescription,
    footerEmail: faqContentPage.value?.structuredContent?.ui?.footerEmail,
    footerEmailLabel: faqContentPage.value?.structuredContent?.ui?.footerEmailLabel,
    footerBookingLabel: faqContentPage.value?.structuredContent?.ui?.footerBookingLabel,
  }))

  function formatFaqSearchResults(count, query) {
    return String(faqContentPage.value?.structuredContent?.ui?.searchResultsTemplate || 'Found :count result(s) for ":query"')
      .replace(':count', String(count))
      .replace(':query', query)
  }

  function formatFaqEmptySearch(query) {
    return String(faqUi.value.emptySearchDescription || '')
      .replace(':query', query)
  }

  return {
    faqContentPage,
    faqCategories,
    faqUi,
    formatFaqSearchResults,
    formatFaqEmptySearch,
  }
}
