import React, { useState, useEffect, useCallback } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { ShoppingCart, Store, LogIn, PlusCircle, X, Smartphone, Percent, Image, Loader2, CheckCircle, AlertTriangle, } from 'lucide-react'; import { motion, AnimatePresence } from 'framer-motion'; import { cn } from '@/lib/utils'; // Mock Firebase (for demonstration - replace with actual Firebase in a real app) const mockFirebase = { initializeApp: () => {}, firestore: () => ({ collection: (collectionName: string) => ({ orderBy: () => ({ onSnapshot: (callback: (snapshot: any) => void, errorCallback: (error: any) => void) => { // Simulate data fetching if (collectionName === 'products') { const mockProducts = [ { id: '1', name: 'Handmade Pottery Vase', description: 'A beautiful, unique vase, perfect for any room.', basePrice: 25.0, imageUrl: 'https://placehold.co/400x300/EEE/31343C', timestamp: Date.now() - 100000, }, { id: '2', name: 'Traditional Woven Rug', description: 'Handwoven rug with traditional Bangladeshi patterns.', basePrice: 150.0, imageUrl: 'https://placehold.co/400x300/EEE/31343C', timestamp: Date.now() - 200000, }, { id: '3', name: 'Organic Cotton Scarf', description: 'Soft and breathable organic cotton scarf, available in various colors.', basePrice: 15.0, imageUrl: 'https://placehold.co/400x300/EEE/31343C', timestamp: Date.now() - 300000, }, ]; setTimeout(() => { callback({ docs: mockProducts.map((p) => ({ id: p.id, data: () => p, })), }); }, 500); // Simulate network latency } return () => {}; // Mock unsubscribe }, limit: () => ({ onSnapshot: (callback: (snapshot: any) => void) => { if (collectionName === 'products') { const mockProducts = [ { id: '1', name: 'New Product 1', description: 'Description for New Product 1', basePrice: 10, imageUrl: 'https://placehold.co/400x300/EEE/31343C', timestamp: Date.now() - 1000, }, { id: '2', name: 'New Product 2', description: 'Description for New Product 2', basePrice: 20, imageUrl: 'https://placehold.co/400x300/EEE/31343C', timestamp: Date.now() - 2000, }, ]; setTimeout(() => { callback({ docs: mockProducts.map((p) => ({ id: p.id, data: () => p, })), }); }, 500); } }, }), }), doc: (docId: string) => ({ get: () => new Promise((resolve) => { setTimeout(() => { const mockProducts = [ { id: '1', name: 'Handmade Pottery Vase', description: 'A beautiful, unique vase, perfect for any room.', basePrice: 25.0, imageUrl: 'https://placehold.co/400x300/EEE/31343C', timestamp: Date.now() - 100000, }, { id: '2', name: 'Traditional Woven Rug', description: 'Handwoven rug with traditional Bangladeshi patterns.', basePrice: 150.0, imageUrl: 'https://placehold.co/400x300/EEE/31343C', timestamp: Date.now() - 200000, }, { id: '3', name: 'Organic Cotton Scarf', description: 'Soft and breathable organic cotton scarf, available in various colors.', basePrice: 15.0, imageUrl: 'https://placehold.co/400x300/EEE/31343C', timestamp: Date.now() - 300000, }, ]; const product = mockProducts.find((p) => p.id === docId); resolve({ data: () => product, }); }, 200); }), }), add: (data: any) => new Promise((resolve) => { setTimeout(() => { resolve({ id: 'new-product-id', }); }, 500); }), }), }), storage: () => ({ ref: (path: string) => ({ put: () => new Promise((resolve) => { setTimeout(() => { resolve({ // Mock uploadTaskSnapshot }); }, 500); }), getDownloadURL: () => new Promise((resolve) => { setTimeout(() => { resolve('https://placehold.co/400x300/EEE/31343C'); // Mock URL }, 200); }), }), }), auth: () => ({ currentUser: null, signInAnonymously: () => new Promise((resolve) => { setTimeout(() => { resolve({ user: { uid: 'mock-anonymous-user-id' } }); }, 500); }), onAuthStateChanged: (callback: (user: any) => void) => { // Simulate auth state setTimeout(() => { callback(null); // Or a mock user }, 100); return () => {}; }, signInWithEmailAndPassword: (email: string, password: string) => new Promise((resolve, reject) => { if (email === 'test@example.com' && password === 'password') { setTimeout(() => { resolve({ user: { uid: 'mock-seller-user-id' } }); }, 500); } else { setTimeout(() => { reject(new Error('Invalid credentials')); }, 200); } }), }), dynamicLinks: () => ({ buildShortLink: async () => 'https://yourdomain.com/app', // Mock }), FieldValue: { serverTimestamp: Date.now, }, }; // =============================== // Types & Interfaces // =============================== interface Product { id: string; name: string; description: string; basePrice: number; imageUrl: string; timestamp: number; } interface CartItem extends Product { quantity: number; } // =============================== // Constants // =============================== const CURRENCY = '৳'; // Bangladeshi Taka // =============================== // Helper Functions // =============================== const calculateDynamicPrice = (basePrice: number) => { const timeVariation = Math.sin(Date.now() / 100000) * 0.15; const demandVariation = Math.random() * 0.2; return Number((basePrice * (1 + timeVariation + demandVariation)).toFixed(2)); }; // =============================== // Components // =============================== const Header = ({ onCartClick, onSellerClick, cartCount }: { onCartClick: () => void; onSellerClick: () => void; cartCount: number; }) => (

🇧🇩 {' '} Desi Bazaar

); const ProductCard = ({ product, onAddToCart }: { product: Product; onAddToCart: (productId: string) => void; }) => { const [showFullDescription, setShowFullDescription] = useState(false); const dynamicPrice = calculateDynamicPrice(product.basePrice); return ( {product.name}

{product.name}

{showFullDescription ? product.description : product.description.substring(0, 100)} {product.description.length > 100 && ( )}

{CURRENCY} {dynamicPrice}
); }; const CartPanel = ({ cartItems, onCheckout, onClose, }: { cartItems: CartItem[]; onCheckout: () => void; onClose: () => void; }) => { const total = cartItems.reduce((acc, item) => acc + calculateDynamicPrice(item.basePrice) * item.quantity, 0); return (

Shopping Cart

{cartItems.length === 0 ? (

Your cart is empty.

) : (
{cartItems.map((item) => (
{item.name}

{item.name}

Quantity: {item.quantity} x {CURRENCY} {calculateDynamicPrice(item.basePrice).toFixed(2)}

Total: {CURRENCY} {(calculateDynamicPrice(item.basePrice) * item.quantity).toFixed(2)}

))}
)}

Total: {CURRENCY} {total.toFixed(2)}

); }; const SellerPanel = ({ onAddProduct, onClose, onLogin, isLoggedIn }: { onAddProduct: (product: Omit) => void; onClose: () => void; onLogin: () => void; isLoggedIn: boolean; }) => { const [productName, setProductName] = useState(''); const [productDescription, setProductDescription] = useState(''); const [basePrice, setBasePrice] = useState(''); const [productImage, setProductImage] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); const [imagePreview, setImagePreview] = useState(null); const [suggestedTags, setSuggestedTags] = useState([]); const [formError, setFormError] = useState(''); const handleImageChange = (e: React.ChangeEvent ) => { const file = e.target.files?.[0]; if (file) { setProductImage(file); const reader = new FileReader(); reader.onloadend = () => { setImagePreview(reader.result as string); }; reader.readAsDataURL(file); } }; const handleProductSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!isLoggedIn) { setFormError('Please log in to add a product.'); return; } if (!productName || !productDescription || !basePrice || !productImage) { setFormError('Please fill in all fields and select an image.'); return; } const price = Number(basePrice); if (isNaN(price) || price <= 0) { setFormError('Price must be a valid number greater than zero.'); return; } setIsSubmitting(true); setFormError(''); const newProduct: Omit = { name: productName, description: productDescription, basePrice: price, imageUrl: imagePreview || 'https://placehold.co/400x300/EEE/31343C', // Use preview or default }; onAddProduct(newProduct); setIsSubmitting(false); setProductName(''); setProductDescription(''); setBasePrice(''); setProductImage(null); setImagePreview(null); setSuggestedTags([]); onClose(); }; const handleGenerateDescription = () => { if (!productName) return; const templates = [ `${productName} is a high-quality product made with care.`, `Experience the beauty of ${productName}, crafted by skilled artisans.`, `Get your ${productName} today!`, ]; setProductDescription(templates[Math.floor(Math.random() * templates.length)]); }; const handleAnalyzeImage = () => { const tags = ['Handmade', 'Traditional', 'Modern', 'Eco-friendly', 'Unique']; const suggested = tags.sort(() => 0.5 - Math.random()).slice(0, 2); setSuggestedTags(suggested); }; return (

Add New Product

{formError &&

{formError}

} {!isLoggedIn ? ( <>

Please log in to add products.

) : (
setProductName(e.target.value)} required disabled={isSubmitting} />