{"version":3,"file":"DDc84BmS.js","sources":["../../../../../../../../node_modules/lucide-svelte/dist/icons/star.svelte","../../../../../../src/lib/stores/favoriteStore.ts"],"sourcesContent":["\n\n\n \n\n","import { writable, get, derived } from \"svelte/store\";\nimport { auth } from \"$lib/stores/auth\";\nimport { STORAGE_KEYS, createNamespacedStore } from '$lib/config/localForage.config';\n\n// Internal store for favorites state\ninterface FavoriteState {\n favorites: Set;\n walletId: string;\n ready: boolean;\n}\n\nfunction createFavoriteStore() {\n // Create the storage namespace for favorite tokens\n const FAVORITE_TOKENS_KEY = STORAGE_KEYS.FAVORITE_TOKENS;\n const storage = createNamespacedStore(STORAGE_KEYS.FAVORITE_TOKENS);\n \n // Create a writable store with initial state\n const initialState: FavoriteState = {\n favorites: new Set(),\n walletId: \"anonymous\",\n ready: false\n };\n \n const store = writable(initialState);\n \n /**\n * Get the current wallet ID\n */\n const getCurrentWalletId = (): string => {\n const wallet = get(auth);\n return wallet?.account?.owner?.toString() || \"anonymous\";\n };\n \n /**\n * Key for storing favorites for a specific wallet\n */\n const getStorageKey = (walletId: string): string => {\n return `${FAVORITE_TOKENS_KEY}_${walletId}`;\n };\n \n /**\n * Load favorites from storage into the store\n */\n const loadFavorites = async (): Promise => {\n const currentWalletId = getCurrentWalletId();\n \n // Check if we're already using the correct wallet\n const { walletId: storedWalletId, favorites, ready } = get(store);\n \n if (currentWalletId === storedWalletId && ready && favorites.size > 0) {\n return Array.from(favorites);\n }\n \n // If anonymous, just return empty and update store\n if (currentWalletId === \"anonymous\") {\n store.update(state => ({\n ...state,\n walletId: currentWalletId,\n favorites: new Set(),\n ready: true\n }));\n return [];\n }\n \n try {\n const key = getStorageKey(currentWalletId);\n const storedFavorites = await storage.getItem<{canister_id: string, timestamp: number}[]>(key) || [];\n const ids = storedFavorites.map(fav => fav.canister_id);\n \n // Update the store with loaded data\n store.update(state => ({\n ...state,\n favorites: new Set(ids),\n walletId: currentWalletId,\n ready: true\n }));\n \n return ids;\n } catch (error) {\n console.error(\"Error loading favorites:\", error);\n store.update(state => ({\n ...state,\n walletId: currentWalletId,\n ready: true\n }));\n return [];\n }\n };\n \n /**\n * Add a token to favorites\n */\n const addFavorite = async (canisterId: string): Promise => {\n const currentWalletId = getCurrentWalletId();\n \n if (currentWalletId === \"anonymous\") {\n return false;\n }\n \n try {\n const key = getStorageKey(currentWalletId);\n const storedFavorites = await storage.getItem<{canister_id: string, timestamp: number}[]>(key) || [];\n \n // Check if already exists\n const existing = storedFavorites.find(fav => fav.canister_id === canisterId);\n if (existing) {\n return true; // Already favorited\n }\n \n // Add new favorite\n storedFavorites.push({\n canister_id: canisterId,\n timestamp: Date.now(),\n });\n \n // Save back to storage\n await storage.setItem(key, storedFavorites);\n \n // Update the store\n store.update(state => {\n const newFavorites = new Set(state.favorites);\n newFavorites.add(canisterId);\n return {\n ...state,\n favorites: newFavorites,\n walletId: currentWalletId\n };\n });\n \n return true;\n } catch (error) {\n console.error(\"Error adding favorite:\", error);\n return false;\n }\n };\n \n /**\n * Remove a token from favorites\n */\n const removeFavorite = async (canisterId: string): Promise => {\n const currentWalletId = getCurrentWalletId();\n \n if (currentWalletId === \"anonymous\") {\n return false;\n }\n \n try {\n const key = getStorageKey(currentWalletId);\n const storedFavorites = await storage.getItem<{canister_id: string, timestamp: number}[]>(key) || [];\n \n // Find if exists\n const index = storedFavorites.findIndex(fav => fav.canister_id === canisterId);\n if (index === -1) {\n return false; // Not found\n }\n \n // Remove from array\n storedFavorites.splice(index, 1);\n \n // Save back to storage\n await storage.setItem(key, storedFavorites);\n \n // Update the store\n store.update(state => {\n const newFavorites = new Set(state.favorites);\n newFavorites.delete(canisterId);\n return {\n ...state,\n favorites: newFavorites\n };\n });\n \n return true;\n } catch (error) {\n console.error(\"Error removing favorite:\", error);\n return false;\n }\n };\n \n /**\n * Toggle favorite status for a token\n */\n const toggleFavorite = async (canisterId: string): Promise => {\n const { favorites } = get(store);\n if (favorites.has(canisterId)) {\n const success = await removeFavorite(canisterId);\n return !success;\n } else {\n return addFavorite(canisterId);\n }\n };\n \n /**\n * Check if a token is favorited\n */\n const isFavorite = async (canisterId: string): Promise => {\n const { ready, favorites, walletId } = get(store);\n const currentWalletId = getCurrentWalletId();\n \n if (currentWalletId === \"anonymous\") {\n return false;\n }\n \n // If we have loaded favorites for the current wallet, use those\n if (ready && walletId === currentWalletId) {\n return favorites.has(canisterId);\n }\n \n // Otherwise, load favorites first\n await loadFavorites();\n return get(store).favorites.has(canisterId);\n };\n \n // Create a derived store for the count\n const favoriteCount = derived(store, ($store) => $store.favorites.size);\n \n // Automatically load favorites when auth changes\n auth.subscribe(() => {\n const currentWalletId = getCurrentWalletId();\n const { walletId } = get(store);\n \n if (currentWalletId !== walletId) {\n loadFavorites();\n }\n });\n \n return {\n subscribe: store.subscribe,\n loadFavorites,\n addFavorite,\n removeFavorite,\n toggleFavorite,\n isFavorite,\n favoriteCount,\n // Convenient accessors to get the current state without async\n getFavorites: () => Array.from(get(store).favorites),\n hasFavorite: (canisterId: string) => get(store).favorites.has(canisterId),\n getCount: () => get(store).favorites.size,\n isReady: () => get(store).ready\n };\n}\n\nexport const favoriteStore = createFavoriteStore(); "],"names":["iconNode","d","favoriteStore","FAVORITE_TOKENS_KEY","STORAGE_KEYS","FAVORITE_TOKENS","storage","createNamespacedStore","store","writable","favorites","Set","walletId","ready","getCurrentWalletId","wallet","get","auth","_b","_a","account","owner","toString","getStorageKey","loadFavorites","async","currentWalletId","storedWalletId","size","Array","from","update","state","key","ids","getItem","map","fav","canister_id","error","console","addFavorite","canisterId","storedFavorites","find","push","timestamp","Date","now","setItem","newFavorites","add","removeFavorite","index","findIndex","splice","delete","favoriteCount","derived","$store","subscribe","toggleFavorite","has","isFavorite","getFavorites","hasFavorite","getCount","isReady","createFavoriteStore"],"mappings":"mRAmCyCA,SAd3B,EAAK,QAAUC,EAAK,6cC6N3B,MAAMC,EAvOb,WAEE,MAAMC,EAAsBC,EAAaC,gBACnCC,EAAUC,EAAsBH,EAAaC,iBAS7C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