import { ref, computed } from "vue"; import type { SearchProduct, SearchHint } from "../types"; /** * Composable для управления состоянием поиска */ export function useSearchState() { // Состояние const searchQuery = ref(""); const showSuggestions = ref(false); const isLoading = ref(false); const isFastLoading = ref(false); // Локальная загрузка для быстрых запросов const error = ref(""); // Данные const historyQueries = ref([]); const popularQueries = ref([]); const popularProducts = ref([]); const searchResults = ref([]); const productHints = ref([]); const fullHints = ref([]); const hasSearchResults = ref(false); // Вычисляемые свойства const hasHistory = computed(() => historyQueries.value.length > 0); const hasPopularQueries = computed(() => popularQueries.value.length > 0); const hasPopularProducts = computed(() => popularProducts.value.length > 0); const hasSearchResultsData = computed(() => searchResults.value.length > 0); const hasProductHints = computed(() => productHints.value.length > 0); const hasFullHints = computed(() => fullHints.value.length > 0); const isEmptyQuery = computed(() => searchQuery.value.trim().length === 0); /** * Сбрасывает состояние поиска */ const resetSearchState = () => { searchResults.value = []; productHints.value = []; fullHints.value = []; hasSearchResults.value = false; error.value = ""; }; /** * Обновляет поисковый запрос */ const updateSearchQuery = (query: string) => { searchQuery.value = query; }; /** * Показывает/скрывает предложения */ const toggleSuggestions = (show: boolean) => { showSuggestions.value = show; }; /** * Устанавливает состояние быстрой загрузки */ const setFastLoading = (loading: boolean) => { isFastLoading.value = loading; }; return { // Состояние searchQuery, showSuggestions, isLoading, isFastLoading, error, // Данные historyQueries, popularQueries, popularProducts, searchResults, productHints, fullHints, hasSearchResults, // Вычисляемые свойства hasHistory, hasPopularQueries, hasPopularProducts, hasSearchResultsData, hasProductHints, hasFullHints, isEmptyQuery, // Методы resetSearchState, updateSearchQuery, toggleSuggestions, setFastLoading, }; }