import type { Purchase } from '@/types/nostr' import { objectCache } from './objectCache' import { parseObjectId } from './urlGenerator' export async function getPurchaseById(purchaseId: string, _timeoutMs: number = 5000): Promise { const parsed = parseObjectId(purchaseId) const hash = parsed.hash ?? purchaseId // Read only from IndexedDB cache const cached = await objectCache.get('purchase', hash) if (cached) { return cached as Purchase } // Also try by ID if hash lookup failed const cachedById = await objectCache.getById('purchase', purchaseId) if (cachedById) { return cachedById as Purchase } // Not found in cache - return null (no network request) return null } export async function getPurchasesForArticle(articleId: string, _timeoutMs: number = 5000): Promise { // Read only from IndexedDB cache const allPurchases = await objectCache.getAll('purchase') const purchases = allPurchases as Purchase[] // Filter by articleId const articlePurchases = purchases.filter((purchase) => purchase.articleId === articleId) // Sort by creation date descending return articlePurchases.sort((a, b) => b.createdAt - a.createdAt) } export async function getPurchasesByPayer(payerPubkey: string, _timeoutMs: number = 5000): Promise { // Read only from IndexedDB cache const allPurchases = await objectCache.getAll('purchase') const purchases = allPurchases as Purchase[] // Filter by payerPubkey const payerPurchases = purchases.filter((purchase) => purchase.payerPubkey === payerPubkey) // Sort by creation date descending return payerPurchases.sort((a, b) => b.createdAt - a.createdAt) }