import{useCallback as b,useEffect as A,useState as E,useRef as w}from"react";import{SEARCH_DEBOUNCE_TIME_MS as Q}from"@redocly/theme/core/constants";import{getBaseDataAttributes as q,withPathPrefix as O}from"@redocly/theme/core/utils";import{ServerRoutes as D}from"../../../constants/api";import{SEARCH_GROUP_FACET_FIELD as v,SEARCH_PRODUCT_FIELD as L,SEARCH_VERSION_FIELD as M}from"../../../constants/common";import{debounce as U}from"../../../utils/time/debounce.js";import{useSearchSession as B}from"@redocly/theme/core/contexts";import{useGlobalData as J,useI18nConfig as H}from"../hooks";import{usePageVersions as K}from"../../providers/page-data/hooks";import{useFacets as $}from"./useFacets";import{telemetry as z}from"../telemetry/index.js";const W=(o,c,r,a,s,u,f,n,l)=>{const{searchSessionId:y}=B(),{currentLocale:F}=H(),m=w(""),d=w(null),p=w(async(e,i)=>{try{u(null);const t=await fetch(O(D.SEARCH),i);if(!t.ok)throw new Error(t.statusText||`Search failed (${t.status})`);if(R(e)){const S=await t.json();a(S.facets),r(S.documents),d.current=null,e.trim().length>1&&z.sendSearchQueryMessage([{...q("searchQuery","search"),query:e,resultCount:Object.keys(S.documents).length.toString(),searchSessionId:y,...n&&{product:n}}]),s(!1)}}catch(t){if(!R(e))return;d.current={query:e,requestParams:i},r({}),s(!1),u(t instanceof Error?t.message:"An unknown error occurred")}}),R=e=>m.current===e,h=b(U(async(e,i,t,S)=>{if(!e.trim().length&&!i.length){r({}),a({}),s(!1),u(null),d.current=null;return}const I={method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({query:e,filter:T(i,S,f),locale:t})};await p.current(e,I)},Q),[f?.version,f?.folderId,n]);return A(()=>{l||(s(!0),h(o,c,F,n),m.current=o)},[l,c,F,n,o,h,s]),{retrySearch:b(async()=>{const e=d.current;e&&(s(!0),m.current=e.query,await p.current(e.query,e.requestParams))},[s])}},X=(o,c,r,a,s,u,f,n,l)=>{const{currentLocale:y}=H();A(()=>{F(r,o,c,y,a,s,n,l)},[a,n,l?.version,l?.folderId]);const F=b(async(m,d,p,R,h,C,e,i)=>{if(h){const{groupKey:t}=h;f(!0);const I={method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({query:d,filter:(()=>{const g=C.find(_=>_.field===v);let P=p;return g&&(p.find(N=>N.field===g.field)||(P=[...p,{field:g.field,values:[t]}])),T(P,e,i)})(),loadMore:h,locale:R})},j=await(await fetch(O(D.SEARCH),I)).json(),k=m[t]||[],x=j.documents[t],G={...m,[t]:[...k,...x]};u(G),f(!1)}},[]);return{}};function le(o,c){const{searchFeatures:r}=J(),[a,s]=E(""),[u,f]=E([]),[n,l]=E({}),[y,F]=E({}),[m,d]=E(),[p,R]=E(!1),[h,C]=E(null),{facets:e}=$(y),{versions:i=[]}=K()||{},t=i.find(I=>I?.active),{retrySearch:S}=W(a,u,l,F,R,C,t,o,c);return X(a,u,n,m,e,l,R,o,t),{query:a,setQuery:s,filter:u,setFilter:f,items:n,isSearchLoading:p,searchError:h,facets:e,groupField:v,setLoadMore:d,retrySearch:S,advancedSearch:r?.advanced?.enabled,askAi:r?.ai?.enabled}}function T(o,c,r){return[...o,...c?[{field:L,values:[c]}]:[],...r?[{field:M,values:[...r.default?["default"]:[r.folderId,r.version]]}]:[]]}export{le as useSearch};
