import type { SearchApiService, SearchProduct, SearchHint } from '../types' /** * Composable для работы с API поиска */ export function useSearchApi(apiService: SearchApiService) { /** * Выполняет поиск по каталогу * @param query - Поисковый запрос * @param isFastResult - Флаг быстрого результата (по умолчанию true для автопоиска) * @returns Результаты поиска */ const searchCatalog = async (query: string, isFastResult: boolean = true) => { if (!query.trim()) return null return await apiService.searchCatalog({ filter: { location_id: apiService.getLocationId(), query: query }, include: ['products', 'filters', 'correction', 'product_hints', 'full_hints', 'brands', 'categories', 'full_categories', 'full_brands'], is_fast_result: isFastResult, load_full_products: { location_id: apiService.getLocationId(), include: ['properties'] }, pagination: { limit_products: 5, limit_categories: 3, limit_brands: 3, offset_products: 0 } }) } /** * Загружает историю запросов * @returns История запросов */ const loadHistoryQueries = async () => { const response = await apiService.getHistoryQueries({ filter: { customer_id: apiService.getCustomerId() } }) return response.data || [] } /** * Загружает популярные запросы * @returns Популярные запросы */ const loadPopularQueries = async () => { const response = await apiService.getTopQueries({ include: ['popular-successful'], pagination: { limit_popular_automatic: 5 } }) return response.data?.popular_successful_queries || [] } /** * Загружает популярные товары * @returns Популярные товары */ const loadPopularProducts = async () => { const response = await apiService.getPopularProducts({ include: ['automatic'], pagination: { limit_automatic: 5 }, load_full_products: { location_id: apiService.getLocationId(), include: ['properties'] } }) return response.data?.full_products || [] } /** * Отслеживает использование запроса * @param query - Запрос для отслеживания */ const trackQueryUse = async (query: string) => { try { await apiService.trackQueryUse(query) } catch (err) { // Игнорируем ошибки отслеживания } } /** * Отслеживает использование функции поиска * @param functionName - Название функции */ const trackSearchFunction = async (functionName: string) => { try { await apiService.useSearchFunction(functionName) } catch (err) { // Игнорируем ошибки отслеживания } } /** * Отслеживает использование подсказки * @param query - Исходный запрос * @param hint - Использованная подсказка */ const trackHintUse = async (query: string, hint: string) => { try { await apiService.trackHintUse(query, hint) } catch (err) { // Игнорируем ошибки отслеживания } } /** * Отслеживает конверсию товара * @param product - Товар * @param source - Источник * @param position - Позиция * @param query - Запрос */ const trackProductConversion = async ( product: SearchProduct, source: 'search' | 'popular_auto_products' | 'popular_manual_products' | 'similar_products' | 'up_sell_products' | 'cross_sell_products' | 'recently_watched_products' | 'recommended_products' | 'recommended_query_products', position?: number, query?: string ) => { try { await apiService.trackProductConversion({ product_id: product.id, conversion: 'click', source, position, query }) } catch (err) { // Игнорируем ошибки отслеживания } } /** * Удаляет запрос из истории * @param query - Запрос для удаления */ const deleteHistoryQuery = async (query: string) => { try { await apiService.deleteHistoryQuery({ customer_id: apiService.getCustomerId(), query }) return true } catch (err) { return false } } /** * Очищает всю историю */ const clearAllHistory = async () => { try { await apiService.deleteAllHistoryQueries({ customer_id: apiService.getCustomerId() }) return true } catch (err) { return false } } return { searchCatalog, loadHistoryQueries, loadPopularQueries, loadPopularProducts, trackQueryUse, trackSearchFunction, trackHintUse, trackProductConversion, deleteHistoryQuery, clearAllHistory } }