import { Component } from '@angular/core'; import { Router } from '@angular/router'; import { ProductService } from 'src/app/services/product.service'; @Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent { searchTerm = ''; resultados: any[] = []; keywordRutas: { [key: string]: string } = { 'samsung': 'products/celulares', 'oppo': 'products/celulares', 'xiaomi': 'products/celulares', 'motorola': 'products/celulares', 'celular': 'products/celulares', 'celulares': 'products/celulares', 'tv': 'products/televisores', 'televisor': 'products/televisores', 'televisores': 'products/televisores', 'portatil': 'products/portatiles', 'portátiles': 'products/portatiles', 'notebook': 'products/portatiles', 'laptop': 'products/portatiles', }; constructor(private productService: ProductService, private router: Router) {} buscar() { const termino = this.searchTerm.trim().toLowerCase(); // Buscar coincidencia exacta for (const keyword in this.keywordRutas) { if (termino.includes(keyword)) { const ruta = this.keywordRutas[keyword]; this.router.navigate([ruta]); return; } } // Si no encontró keyword, intentar buscar por productos const resultados = this.productService.search(termino); if (resultados.length === 1) { const destino = resultados[0].categoria; this.router.navigate([`products/${destino}`]); } else if (resultados.length > 1) { this.router.navigate(['/resultados'], { queryParams: { q: this.searchTerm } }); } else { alert('No se encontraron resultados.'); } } }