/**
 * @deprecated 当前无组件使用此 composable，SearchView 直接使用 useSearchStore。
 *             保留以防后续有复用场景，如确认不再需要可安全删除。
 * @deprecated Not currently used by any component. SearchView uses useSearchStore directly.
 *             Kept in case of future reuse; safe to remove if confirmed unnecessary.
 *
 * 搜索组合式函数
 * 对 SearchStore 做薄封装，向组件暴露搜索、翻页、筛选重置等操作方法，
 * 以及解构后的响应式状态 refs，简化组件中的使用方式。
 */
import { useSearchStore } from '@/stores/search'
import { useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'

export function useSearch() {
  const store = useSearchStore()
  const router = useRouter()
  const { query, filters, results, total, page, pageSize, loading, aggregations } = storeToRefs(store)

  function handleSearch(value?: string) {
    if (value !== undefined) {
      store.query = value
    }
    store.search(true)
  }

  function handlePageChange(p: number) {
    store.changePage(p)
  }

  function handleFilterChange() {
    store.search(true)
  }

  function handleResetFilters() {
    store.resetFilters()
    store.search(true)
  }

  function goToDocument(docId: string) {
    router.push(`/doc/${docId}`)
  }

  return {
    query, filters, results, total, page, pageSize, loading, aggregations,
    handleSearch, handlePageChange, handleFilterChange, handleResetFilters,
    goToDocument,
  }
}
