// OneSearch Main Application Script // Application State Management class OneSearchApp { constructor() { this.user = null; this.currentSearch = null; this.searchResults = []; this.trustScores = new Map(); this.walletBalance = 47.23; this.isAuthenticated = false; this.init(); } init() { this.setupEventListeners(); this.initializeComponents(); this.loadInitialData(); this.setupWebSocketConnection(); console.log('OneSearch app initialized'); } setupEventListeners() { // Search functionality const searchBtn = document.getElementById('search-btn'); const problemInput = document.getElementById('problem-input'); if (searchBtn) { searchBtn.addEventListener('click', () => this.performSearch()); } if (problemInput) { problemInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') { this.performSearch(); } }); } // QR Generation const generateQrBtn = document.getElementById('generate-qr'); if (generateQrBtn) { generateQrBtn.addEventListener('click', () => this.showQRModal()); } const createQrBtn = document.getElementById('create-qr-btn'); if (createQrBtn) { createQrBtn.addEventListener('click', () => this.showQRModal()); } const closeQrModal = document.getElementById('close-qr-modal'); if (closeQrModal) { closeQrModal.addEventListener('click', () => this.hideQRModal()); } // Authentication const authButton = document.getElementById('auth-button'); if (authButton) { authButton.addEventListener('click', () => this.handleAuth()); } // Navigation this.setupNavigation(); } setupNavigation() { // Smooth scrolling for navigation links document.querySelectorAll('a[href^="#"]').forEach(link => { link.addEventListener('click', (e) => { e.preventDefault(); const target = document.querySelector(link.getAttribute('href')); if (target) { target.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); } async performSearch() { const problemInput = document.getElementById('problem-input'); const query = problemInput?.value.trim(); if (!query) { this.showNotification('Please enter a problem description', 'warning'); return; } try { this.setLoading(true); // Simulate AI-powered problem interpretation const searchResults = await this.interpretProblem(query); this.searchResults = searchResults; this.currentSearch = query; this.updateMindMap(searchResults); this.displaySearchResults(searchResults); this.showNotification('Search completed successfully', 'success'); } catch (error) { console.error('Search error:', error); this.showNotification('Search failed. Please try again.', 'error'); } finally { this.setLoading(false); } } async interpretProblem(query) { // Simulate AI/ML interpretation with mock data await new Promise(resolve => setTimeout(resolve, 1500)); // Simulate API call const mockProducts = [ { id: '1', title: 'Stackable Storage Containers Set', description: 'Apex Kitchen Solutions - Set of 6 BPA-free containers with airtight seals', merchant: 'Apex Kitchen Solutions', category: 'Storage Solutions', price: 32.99, currency: 'USD', score: 82.6, scoreBreakdown: { recommenderReputation: 0.68, provenance: 0.95, usageDuration: 0.6, conversionRate: 0.3, afterSales: 0.7, locality: 0.9, costSatisfaction: 0.64, qualityReviews: 0.92 }, reviewCount: 124, averageRating: 4.2, qrConversions: 23, wageTransparency: true, livingWageCompliant: true, tags: ['kitchen', 'storage', 'organization', 'space-saving'] }, { id: '2', title: 'Modular Cabinet System', description: 'FlexStorage Pro - Adjustable cabinet system for small spaces', merchant: 'FlexStorage Pro', category: 'Storage Solutions', price: 89.99, currency: 'USD', score: 78.3, scoreBreakdown: { recommenderReputation: 0.72, provenance: 0.88, usageDuration: 0.75, conversionRate: 0.25, afterSales: 0.65, locality: 0.85, costSatisfaction: 0.58, qualityReviews: 0.85 }, reviewCount: 89, averageRating: 4.0, qrConversions: 18, wageTransparency: false, livingWageCompliant: false, tags: ['kitchen', 'storage', 'cabinet', 'modular'] }, { id: '3', title: 'Magnetic Organization System', description: 'MagOrganize - Magnetic strips and containers for vertical storage', merchant: 'MagOrganize Co', category: 'Storage Solutions', price: 24.99, currency: 'USD', score: 75.8, scoreBreakdown: { recommenderReputation: 0.65, provenance: 0.82, usageDuration: 0.55, conversionRate: 0.4, afterSales: 0.72, locality: 0.9, costSatisfaction: 0.71, qualityReviews: 0.88 }, reviewCount: 156, averageRating: 4.1, qrConversions: 31, wageTransparency: true, livingWageCompliant: true, tags: ['kitchen', 'storage', 'magnetic', 'vertical'] } ]; return mockProducts; } updateMindMap(results) { const mindMapContainer = document.getElementById('mindmap-container'); if (!mindMapContainer || !window.MindMap) return; // Prepare mind map data const mindMapData = { nodes: [ { id: 'root', label: this.currentSearch, level: 0 }, { id: 'category', label: 'Storage Solutions', level: 1 }, { id: 'budget', label: '$25-$90', level: 1 }, { id: 'size', label: 'Small Space', level: 1 } ], edges: [ { from: 'root', to: 'category' }, { from: 'root', to: 'budget' }, { from: 'root', to: 'size' } ] }; // Add product nodes results.forEach((product, index) => { const nodeId = `product_${product.id}`; mindMapData.nodes.push({ id: nodeId, label: product.title, level: 2, score: product.score }); mindMapData.edges.push({ from: 'category', to: nodeId }); }); window.MindMap.render(mindMapContainer, mindMapData); } displaySearchResults(results) { const resultsContainer = document.getElementById('search-results'); if (!resultsContainer) return; resultsContainer.innerHTML = ''; results.forEach(product => { const resultElement = this.createResultElement(product); resultsContainer.appendChild(resultElement); }); } createResultElement(product) { const result = document.createElement('div'); result.className = 'search-result-item'; result.innerHTML = `
${product.description}