import{useCallback as y,useEffect as u,useState as n}from"react";import{withPathPrefix as S}from"@redocly/theme/core/utils";import{ServerRoutes as d}from"../../../constants/api";import{useI18nConfig as b}from"../hooks";function h(e){const[o,c]=n([]),[s,a]=n(null),[t,r]=n(null),[p,i]=n(!1),{currentLocale:f}=b();u(()=>{i(!0),m()},[t]);const m=y(async()=>{if(e&&t===null){i(!1);return}let l={locale:f,field:e};t&&(l={...l,...t});const F=await(await fetch(S(d.SEARCH_FACETS),{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(l)})).json();e?a(F):c(F),i(!1)},[f,t,e]);return{initialFacets:o,searchFacets:s,isFacetsLoading:p,setSearchFacetQuery:r}}function E(e){const[o,c]=n(null),{searchFacets:s,setSearchFacetQuery:a}=h(e);return u(()=>{const t=s?s.find(r=>r.field===e)??null:null;c(t)},[s,e]),{searchFacet:o,setSearchFacet:c,setSearchFacetQuery:a}}const L=e=>{const[o,c]=n([]),{initialFacets:s}=h();return u(()=>{if(e&&Object.keys(e).length){const a=[];for(const t of s)a.push({...t,values:e[t.field]?.map(r=>({...r,isCounterVisible:!0}))||[]});c(a)}else c(s)},[s,e]),{facets:o}};export{E as useFacetQuery,L as useFacets};
