
import wixData from 'wix-data'; $w.onReady(function () { $w('#html1').onMessage(async (event) => { const message = event.data; if (!message || message.type !== 'catalog-search') return; const term = String(message.term || '').trim().slice(0, 80); if (term.length < 2) return; try { const byName = wixData.query('Stores/Products').contains('name', term); const bySku = wixData.query('Stores/Products').contains('sku', term); const byDescription = wixData.query('Stores/Products').contains('description', term); const result = await byName.or(bySku).or(byDescription).eq('visible', true).ascending('name').limit(40).find(); const items = result.items.map((product) => ({ name: product.name, sku: product.sku || '', description: cleanText(product.description).slice(0, 150), url: normalizeProductUrl(product.productPageUrl) })); $w('#html1').postMessage({ type: 'catalog-results', id: message.id, items }); } catch (error) { console.error('Catalog search failed', error); $w('#html1').postMessage({ type: 'catalog-results', id: message.id, items: [] }); } }); }); function cleanText(value) { return String(value || '').replace(/<[^>]*>/g, ' ').replace(/ /g, ' ').replace(/\s+/g, ' ').trim(); } function normalizeProductUrl(value) { const path = String(value || '').trim(); if (!path) return ''; if (/^https?:\/\//i.test(path)) return path; return 'https://www.metricscrewandtool.com' + (path.startsWith('/') ? '' : '/') + path; }





