import { computed } from 'vue'
import { siteSettings } from '~/generated/site/site'
import { usePublicLocale } from '~/composables/usePublicLocale'

const footerSocialOrder = ['instagram', 'facebook', 'linkedin']

function sortByOrder(items = []) {
  return [...items].sort((left, right) => Number(left?.sortOrder || 0) - Number(right?.sortOrder || 0))
}

function resolveLocalizedBusiness(locale) {
  return siteSettings?.business?.localized?.[locale] || siteSettings?.business || {}
}

export function useSiteContent() {
  const { locale } = usePublicLocale()

  const socialProfiles = computed(() => sortByOrder(siteSettings?.socialProfiles || []))
  const footerSocialLinks = computed(() => {
    return footerSocialOrder
      .map((id) => socialProfiles.value.find((profile) => profile.id === id))
      .filter(Boolean)
  })
  const storeButtons = computed(() => sortByOrder(siteSettings?.storeButtons || []))
  const business = computed(() => resolveLocalizedBusiness(locale.value))

  return {
    siteSettings: computed(() => siteSettings),
    socialProfiles,
    footerSocialLinks,
    storeButtons,
    business,
  }
}
