1 import { CardProps } from "../../components/Card"
2 import { medusaSuggestions } from "./suggestions"
3
4 const PUNCTIONATION = /[.,?;!]/g
5
6 type UseSuggestionsProps = {
7 keywords: string | string[]
8 }
9
10 export const useMedusaSuggestions = ({ keywords }: UseSuggestionsProps) => {
11 if (!keywords.length) {
12 return null
13 }
14
15 const keywordsArray = Array.isArray(keywords)
16 ? keywords.map(formatWord)
17 : keywords.split(" ").map(formatWord)
18
19 let matchedSuggestion: CardProps | null = null
20
21 keywordsArray.some((word) => {
22 if (medusaSuggestions.has(word)) {
23 matchedSuggestion = medusaSuggestions.get(word) || null
24 }
25
26 return matchedSuggestion !== null
27 })
28
29 return matchedSuggestion as CardProps | null
30 }
31
32 const formatWord = (word: string) => {
33 return word.toLowerCase().trim().replace(PUNCTIONATION, "")
34 }