JavaScript · 1595 bytes Raw Blame History
1 import { roastDatabase } from '../data/roastDatabase';
2
3 export const getRoastsForLocation = (location) => {
4 if (!location) return [];
5
6 let roasts = [];
7 const { city, state, country } = location;
8
9 // Normalize location names
10 const cityLower = city?.toLowerCase().replace(/\s+/g, '_');
11 const stateLower = state?.toLowerCase().replace(/\s+/g, '_');
12 const countryLower = country?.toLowerCase().replace(/\s+/g, '_');
13
14 // Check for city-specific roasts
15 if (cityLower) {
16 // Look through all states for this city
17 for (const [stateKey, stateData] of Object.entries(roastDatabase)) {
18 if (stateData[cityLower]) {
19 roasts = roasts.concat(stateData[cityLower]);
20 }
21 }
22 }
23
24 // Check for state/region roasts
25 if (stateLower && roastDatabase[stateLower]?.generic) {
26 roasts = roasts.concat(roastDatabase[stateLower].generic);
27 }
28
29 // Check for country roasts
30 if (countryLower) {
31 const countryMappings = {
32 'united_states': 'usa',
33 'united_states_of_america': 'usa',
34 'us': 'usa',
35 'united_kingdom': 'uk',
36 'great_britain': 'uk',
37 'england': 'uk',
38 'scotland': 'uk',
39 'wales': 'uk',
40 'northern_ireland': 'uk'
41 };
42
43 const countryKey = countryMappings[countryLower] || countryLower;
44 if (roastDatabase[countryKey]?.generic) {
45 roasts = roasts.concat(roastDatabase[countryKey].generic);
46 }
47 }
48
49 // If no specific roasts found, use defaults
50 if (roasts.length === 0) {
51 roasts = roastDatabase.default || [];
52 }
53
54 // Remove duplicates
55 return [...new Set(roasts)];
56 };