File size: 3,376 Bytes
3fa9572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { create } from 'zustand';
import { persist } from 'zustand/middleware';

interface UserPreferencesState {
  searchHistory: string[];
  categoryScores: Record<string, number>;
  
  addSearchHistory: (term: string) => void;
  incrementCategoryScore: (categoryId: string, amount?: number) => void;
  clearHistory: () => void;
  getSortedCategories: <T extends { id: string }>(categories: T[]) => T[];
}

// Simple keyword mapping for scoring
// Note: These should match the category IDs in Home.tsx
const categoryKeywords: Record<string, string[]> = {
  electronics: ['phone', 'mobile', 'laptop', 'watch', 'tech', 'smart', 'screen', 'audio', 'sound', 'camera', 'إلكترونيات', 'هاتف', 'جوال', 'ساعة', 'ذكية', 'لابتوب', 'كاميرا', 'سماعة'],
  fashion: ['shirt', 'pant', 'dress', 'shoe', 'cloth', 'wear', 'fashion', 'style', 'bag', 'ملابس', 'أزياء', 'موضة', 'قميص', 'بنطلون', 'فستان', 'حذاء', 'شنطة'],
  new: ['new', 'arrival', 'latest', 'جديد', 'وصل', 'حديثاً'],
  under5k: ['cheap', 'deal', 'offer', 'رخيص', 'عرض', 'تخفيض', 'أقل']
};

export const useUserPreferencesStore = create<UserPreferencesState>()(
  persist(
    (set, get) => ({
      searchHistory: [],
      categoryScores: {},

      addSearchHistory: (term: string) => {
        if (!term || !term.trim()) return;
        
        const state = get();
        const searchHistory = state.searchHistory;
        const categoryScores = state.categoryScores;
        
        // Update history (max 10 items)
        const newHistory = [term, ...searchHistory.filter(t => t !== term)].slice(0, 10);
        
        // Update scores based on keywords
        const newScores = { ...categoryScores };
        const lowerTerm = term.toLowerCase();
        
        let matched = false;
        Object.entries(categoryKeywords).forEach(([catId, keywords]) => {
          if (keywords.some(k => lowerTerm.includes(k.toLowerCase()))) {
            newScores[catId] = (newScores[catId] || 0) + 5; // Big boost for explicit search
            matched = true;
          }
        });

        set({ searchHistory: newHistory, categoryScores: newScores });
      },

      incrementCategoryScore: (categoryId: string, amount = 1) => {
        const state = get();
        const currentScore = state.categoryScores[categoryId] || 0;
        
        set({
          categoryScores: {
            ...state.categoryScores,
            [categoryId]: currentScore + amount
          }
        });
      },

      clearHistory: () => set({ searchHistory: [], categoryScores: {} }),

      getSortedCategories: (categories) => {
        const state = get();
        const scores = state.categoryScores;
        
        // Create a shallow copy to sort
        return [...categories].sort((a, b) => {
          // Always keep 'all' at top
          if (a.id === 'all') return -1;
          if (b.id === 'all') return 1;
          
          const scoreA = scores[a.id] || 0;
          const scoreB = scores[b.id] || 0;
          
          // Sort by score descending
          if (scoreB !== scoreA) {
            return scoreB - scoreA;
          }
          
          return 0; 
        });
      }
    }),
    {
      name: 'user-preferences-storage', // name of the item in the storage (must be unique)
    }
  )
);