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

export function buildDrivingGuidePageSeo(page, origin = '') {
  return buildResolvedPageSeo(page, origin, {
    ogImage: page?.heroImage || page?.structuredContent?.media?.heroImage || '',
  })
}

export function useDrivingGuideContent() {
  const drivingGuidePage = useResolvedPage('driving-guide')
  const structuredContent = computed(() => drivingGuidePage.value?.structuredContent || {})
  const ui = computed(() => structuredContent.value.ui || {})
  const media = computed(() => structuredContent.value.media || {})
  const localeSwitchContext = computed(() => buildPublicLocaleSwitchContext(drivingGuidePage.value, { kind: 'list' }))

  useRegisterPublicLocaleSwitchContext(localeSwitchContext)

  const drivingGuideHero = computed(() => ({
    badge: drivingGuidePage.value?.subtitle || ui.value.heroBadge || '',
    title: drivingGuidePage.value?.title || '',
    description: drivingGuidePage.value?.excerpt || '',
    image: media.value.heroImage || drivingGuidePage.value?.heroImage || '',
  }))

  const drivingGuideQuickRules = computed(() => sortByOrder(structuredContent.value.quickRules || []))

  const drivingGuideMountain = computed(() => ({
    label: ui.value.mountainLabel || '',
    heading: ui.value.mountainHeading || '',
    description: ui.value.mountainDescription || '',
    image: media.value.mountainImage || '',
    imageAlt: media.value.mountainImageAlt || ui.value.mountainHeading || drivingGuidePage.value?.title || '',
    points: sortByOrder(structuredContent.value.mountainPoints || []),
  }))

  const drivingGuidePanels = computed(() => ({
    roadUseTitle: ui.value.roadUseTitle || '',
    borderTitle: ui.value.borderTitle || '',
    roadUse: sortByOrder(structuredContent.value.roadUse || []),
    borderRules: sortByOrder(structuredContent.value.borderRules || []),
  }))

  return {
    drivingGuidePage,
    drivingGuideHero,
    drivingGuideQuickRules,
    drivingGuideMountain,
    drivingGuidePanels,
  }
}
