export const SORT_OPTIONS = [
  'Recommended',
  'Price: Low to High',
  'Price: High to Low',
  'Rating',
]

export const DEFAULT_PRICE_MIN = 200
export const DEFAULT_PRICE_MAX = 3000

export function formatSearchType(value, labels = {}) {
  const normalized = String(value || '').trim()

  if (!normalized) {
    return labels.typeVehicleLabel || 'Vehicle'
  }

  const lowered = normalized.toLowerCase()

  const mappedLabels = {
    vehicle: labels.typeVehicleLabel || 'Vehicle',
    economy: labels.typeEconomyLabel || 'Economy',
    standard: labels.typeStandardLabel || 'Standard',
    sedan: labels.typeSedanLabel || 'Sedan',
    suv: labels.typeSuvLabel || 'SUV',
    vans: labels.typeVansLabel || 'Vans',
    van: labels.typeVansLabel || 'Vans',
    premium: labels.typeLuxuryLabel || 'Luxury',
    luxury: labels.typeLuxuryLabel || 'Luxury',
  }

  if (mappedLabels[lowered]) {
    return mappedLabels[lowered]
  }

  return normalized
    .split(/[\s_-]+/)
    .filter(Boolean)
    .map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
    .join(' ')
}

export function resolveSearchMarketingBadgeLabel(item, labels = {}) {
  const badgeKey = String(item?.marketingBadgeKey || '').trim().toLowerCase()

  if (badgeKey === 'featured') {
    return labels.featuredLabel || 'Featured'
  }

  if (badgeKey === 'luxury') {
    return labels.luxuryBadgeLabel || 'Luxury'
  }

  if (badgeKey === 'new_arrival') {
    return labels.newArrivalBadgeLabel || 'New Arrival'
  }

  if (badgeKey === 'hot_deal') {
    return labels.hotDealBadgeLabel || 'Hot Deal'
  }

  return String(item?.marketingBadgeLabel || item?.marketingBadge || '').trim() || null
}

export function resolveSearchDeliveryBadgeLabel(item, labels = {}) {
  const badgeKey = String(item?.deliveryBadgeKey || '').trim().toLowerCase()

  if (badgeKey === 'transfer_service') {
    return labels.transferServiceLabel || 'Transfer service'
  }

  if (badgeKey === 'one_way_available') {
    return labels.oneWayAvailableLabel || 'One-way available'
  }

  return String(item?.deliveryBadgeLabel || item?.deliveryBadge || '').trim() || null
}

export function buildSearchScarcity(input, labels = {}) {
  const availableCount = Number(input?.availableCount || 0)
  const available = Boolean(input?.available)
  const reasonCode = String(input?.reasonCode || '').trim().toLowerCase()
  const reason = String(input?.reason || '').trim()

  if (!available) {
    if (reasonCode === 'no_vehicles_available' || availableCount <= 0) {
      return labels.soldOutSelectedDatesLabel || 'No vehicles are currently available for the selected rental window.'
    }

    return reason || labels.soldOutSelectedDatesLabel || 'No vehicles are currently available for the selected rental window.'
  }

  if (availableCount === 1) {
    return labels.onlyOneLeftTemplate || 'Only 1 left for your dates!'
  }

  if (availableCount > 1 && availableCount <= 3) {
    return String(labels.onlyFewLeftTemplate || 'Only {count} left for your dates!')
      .replace('{count}', String(availableCount))
  }

  return null
}
