"use client"

import { useRouter } from "next/navigation"
import { type SearchParams } from "@/lib/mail-search/search-params"
import { useBuildSearchUrl } from "@/hooks/use-build-search-url"
import { useAdvancedSearchForm } from "@/lib/mail-search/use-advanced-search-form"
import { AdvancedSearchPanelDesktop } from "@/components/gmail/mail-search/advanced-search-fields"

export function AdvancedSearchPanel({
  onClose,
  initialQuery,
  currentParams,
}: {
  onClose: () => void
  initialQuery: string
  currentParams: SearchParams | null
}) {
  const router = useRouter()
  const buildSearchUrl = useBuildSearchUrl()
  const form = useAdvancedSearchForm(initialQuery, currentParams)

  const handleSubmit = () => {
    router.push(buildSearchUrl(form.buildParams()))
    onClose()
  }

  return (
    <AdvancedSearchPanelDesktop form={form} onSubmit={handleSubmit} onClose={onClose} />
  )
}
