import { createStaticLocationsSnapshot } from '~/content/locations'
import { resolvePublicUiValue } from '~/i18n/public-ui'

function detectType(location) {
  const rawType = String(location?.type || '').toLowerCase()

  if (rawType === 'airport' || location?.isAirport) {
    return 'airport'
  }

  if (rawType === 'office' || rawType === 'delivery') {
    return rawType
  }

  const rawCode = String(location?.code || '').toUpperCase()

  if (rawCode.includes('OFFICE')) {
    return 'office'
  }

  if (rawCode.includes('DELIVERY')) {
    return 'delivery'
  }

  return 'city'
}

function resolveLocalizedLocationContent(location, locale = 'en') {
  const localizedContent = location?.localizedContent && typeof location.localizedContent === 'object'
    ? location.localizedContent
    : {}
  const normalizedLocale = String(locale || 'en').toLowerCase()

  return localizedContent[normalizedLocale] || localizedContent.en || {}
}

function deriveDisplayCode(location, locale = 'en') {
  const localizedContent = resolveLocalizedLocationContent(location, locale)
  const localizedLabel = String(localizedContent?.publicCodeLabel || location?.publicCodeLabel || '').trim()

  if (localizedLabel) {
    return localizedLabel
  }

  const rawCode = String(location?.code || '').trim().toUpperCase()
  const airportLabel = resolvePublicUiValue(locale, 'locationsPage.typeAirport', 'Airport')
  const officeLabel = resolvePublicUiValue(locale, 'locationsPage.typeOffice', 'Office')
  const deliveryLabel = resolvePublicUiValue(locale, 'locationsPage.typeDelivery', 'Delivery')

  if (!rawCode) {
    return detectType(location) === 'airport' ? airportLabel : deliveryLabel
  }

  if (rawCode.includes('AIRPORT')) {
    return airportLabel
  }

  if (rawCode.includes('OFFICE')) {
    return officeLabel
  }

  if (rawCode.includes('DELIVERY')) {
    return deliveryLabel
  }

  return rawCode
}

function toUiLocation(location, locale = 'en') {
  const localizedContent = resolveLocalizedLocationContent(location, locale)

  return {
    id: String(location.id),
    backendId: location.id ?? null,
    name: localizedContent.name || location.name,
    type: detectType(location),
    code: deriveDisplayCode(location, locale),
    rawCode: location.code || '',
    city: localizedContent.cityName || location.cityName || location.city || '',
  }
}

function sortLocations(left, right) {
  const order = {
    airport: 10,
    office: 20,
    delivery: 30,
    city: 40,
  }

  if (left.type !== right.type) {
    return (order[left.type] ?? 99) - (order[right.type] ?? 99)
  }

  const leftKey = `${left.city} ${left.name}`.trim()
  const rightKey = `${right.city} ${right.name}`.trim()

  return leftKey.localeCompare(rightKey)
}

export function usePublicSiteLocations() {
  const { locale } = usePublicLocale()
  const exportedLocations = createStaticLocationsSnapshot()

  const locations = computed(() => exportedLocations.map((location) => toUiLocation(location, locale.value)).sort(sortLocations))
  const isLoaded = computed(() => true)
  const isLoading = computed(() => false)

  async function loadLocations() {
    return locations.value
  }

  return {
    locations,
    isLoaded,
    isLoading,
    loadLocations,
  }
}
