59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { Country } from "@scandic-hotels/common/constants/country"
|
|
import { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
import { ApiCountry } from "../../../types/country"
|
|
|
|
import type { DestinationsData } from "../../../types/destinationsData"
|
|
|
|
/**
|
|
* Sorts destination data based on language preference:
|
|
* - en: alphabetical order
|
|
* - de: Germany first, then alphabetical
|
|
* - da: Denmark first, then alphabetical
|
|
* - no: Norway first, then alphabetical
|
|
* - sv: Sweden first, then alphabetical
|
|
* - fi: Finland first, then alphabetical
|
|
*
|
|
* @param destinations DestinationsData
|
|
* @param language Lang
|
|
* @returns Sorted array of destinations
|
|
*/
|
|
export function getSortedDestinationsByLanguage(
|
|
destinations: DestinationsData,
|
|
language: Lang
|
|
) {
|
|
const destinationsToSort = [...destinations]
|
|
const firstCountryByLanguage: Record<Lang, Country | null> = {
|
|
[Lang.de]: Country.Germany,
|
|
[Lang.da]: Country.Denmark,
|
|
[Lang.no]: Country.Norway,
|
|
[Lang.sv]: Country.Sweden,
|
|
[Lang.fi]: Country.Finland,
|
|
[Lang.en]: null,
|
|
}
|
|
|
|
const firstCountry = firstCountryByLanguage[language]
|
|
|
|
// If no country is defined for this language, sort alphabetically
|
|
if (!firstCountry) {
|
|
return destinationsToSort.sort((a, b) =>
|
|
a.country.localeCompare(b.country, language)
|
|
)
|
|
}
|
|
|
|
// Get the localized name of the first country
|
|
const localizedCountryName = ApiCountry[language][firstCountry]
|
|
|
|
return destinationsToSort.sort((a, b) => {
|
|
if (a.country === localizedCountryName) {
|
|
return -1
|
|
}
|
|
|
|
if (b.country === localizedCountryName) {
|
|
return 1
|
|
}
|
|
|
|
return a.country.localeCompare(b.country, language)
|
|
})
|
|
}
|