"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("react/jsx-runtime"),h=require("react"),m=require("@devgateway/wp-react-lib"),M=require("./CustomSemanticSearch.js"),C=require("react-dom"),N=require("semantic-ui-react"),w=require("react-intl"),v=(s,r)=>{if(!s||!r)return s||"";const c=new RegExp(`(${r})`,"gi");return s.replace(c,"<strong>$1</strong>")},y=w.injectIntl(({ID:s,title:r,slug:c,parent_title:i,parent_slug:t,parent_link:n,extract:a,type:l,link:d,terms:j,subtype:f,bread_crumbs:u=[],metadata:o,intl:{locale:S},searchTerm:x})=>{let p=n?m.utils.replaceLink(n,S)+`#${c}`:m.utils.replaceLink(d,S);const g=o==null?void 0:o.redirect_url;p=g?g+`#${c}`:p;const R=v(String(r),x),I=v(a,x);return e.jsx("div",{className:"search-results-wrapper searching-results",style:{display:"flex",flexDirection:"column"},children:e.jsxs("div",{className:"has-standard-12-font-size",onClick:P=>document.location.href=p,children:[e.jsx("h5",{className:"breadcrumbs-search",dangerouslySetInnerHTML:{__html:u&&u.length>0?v(u.join(" / "),x):""}}),e.jsx("div",{className:"has-standard-14-font-size",children:e.jsx("h4",{className:"search-title",dangerouslySetInnerHTML:{__html:R}})}),e.jsx("div",{className:"search-content",dangerouslySetInnerHTML:{__html:m.utils.replaceHTMLinks(I,S)}})]})})}),E=({results:s,meta:r,perPage:c,intl:i,searchTerm:t})=>{const n=r?r["x-wp-total"]:0;return r&&r["x-wp-totalpages"],e.jsxs("div",{id:"float-results-container",children:[e.jsx("span",{className:"float-results-header",children:i.formatMessage({id:"search.results.summary",defaultMessage:"{count} of {total} Results"},{count:n<c?n:c,total:n})}),s.map(a=>e.jsx(y,{...a,searchTerm:t},a.ID))]})},_=({onSearch:s,perPage:r,loading:c,results:i,meta:t,intl:n,searchTerm:a})=>(t&&t["x-wp-total"],t&&t["x-wp-totalpages"],e.jsx("input",{placeholder:n.formatMessage({id:"search.placeholder",defaultMessage:"Search..."}),type:"text",className:"input search",name:"search",value:a,onChange:l=>{s(l.target.value)}})),L=({onSearch:s,onSetSelected:r,perPage:c,loading:i,results:t,meta:n,locale:a,intl:l,searchTerm:d,selected:j})=>{const[f,u]=h.useState(!1),o=h.useRef(null),S=()=>{u(!1)};h.useEffect(()=>{const p=document.createElement("div");p.setAttribute("id","float-input-container"),p.setAttribute("class","input container"),p.style.display="none";const g=document.getElementById("root");return g&&(g.appendChild(p),o.current=p),()=>{o.current&&o.current.parentNode&&o.current.parentNode.removeChild(o.current)}},[]),h.useEffect(()=>{o.current&&(o.current.style.display=f?"block":"none")},[f]);const x=()=>{u(!0)};return e.jsxs(e.Fragment,{children:[e.jsx("div",{id:"ui-float-search",className:`ui float-search${f?" ui-float-search-active":""}`,onMouseOver:x,children:e.jsx(N.Icon,{name:"search",size:"small"})}),o.current&&C.createPortal(f?e.jsxs("div",{onMouseLeave:S,children:[e.jsx("div",{className:"float-search-container",children:e.jsx(_,{onSearch:s,perPage:c,loading:i,results:t,meta:n,intl:l,searchTerm:d})}),t&&t.length>0&&e.jsx(w.IntlProvider,{locale:a,children:e.jsx(E,{results:t,meta:n,perPage:c,intl:l,searchTerm:d})})]}):null,o.current)]})},b=({onSearch:s,perPage:r,loading:c,results:i,meta:t,intl:n})=>{const a=t?t["x-wp-total"]:0,l=t?t["x-wp-totalpages"]:0,[d,j]=h.useState("");h.useEffect(()=>{const u=setTimeout(()=>{s(d)},300);return()=>clearTimeout(u)},[d]);const f=u=>e.jsx(y,{...u,searchTerm:d});return e.jsx(M.default,{value:d,loading:c,placeholder:n.formatMessage({id:"search.placeholder",defaultMessage:"Search..."}),onResultSelect:(u,o)=>null,total:a,perPage:r,totalPages:l,onSearchChange:(u,o)=>{j(o.value)},resultRenderer:f,results:i,showNoResults:!1,intl:n,searchTerm:d})},q=w.injectIntl(s=>{const{intl:r,onSetSelected:c}=s,[i,t]=h.useState(""),[n,a]=h.useState(!1);return h.useEffect(()=>{const l=()=>{a(window.innerWidth<=1365)};return l(),window.addEventListener("resize",l),()=>window.removeEventListener("resize",l)},[]),e.jsx(m.SearchProvider,{search:i,perPage:5,locale:r.locale,children:e.jsx(T,{...s,onSetSelected:c,setQuery:t,query:i,isSmallScreen:n,intl:r})})}),T=({onSetSelected:s,selected:r,setQuery:c,query:i,isSmallScreen:t,intl:n,...a})=>{const l=h.useContext(m.SearchContext);return a.settings.react_search_type==="floating"||t?e.jsx(L,{onSetSelected:s,selected:r,onSearch:c,perPage:5,intl:n,...a,...l,searchTerm:i}):e.jsx(b,{onSetSelected:s,onSearch:c,perPage:5,intl:n,...a,...l})};exports.default=q;
