!function(){"use strict";var e={n:function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,{a:n}),n},d:function(t,n){for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r:function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t);var n={};e.r(n),e.d(n,{setResult:function(){return m},setTab:function(){return d},setValue:function(){return l},setVisible:function(){return u}});var r={};e.r(r),e.d(r,{getResults:function(){return p},getTab:function(){return v},getValue:function(){return h},isVisible:function(){return f}});var s=window.wp.element,a=window.wp.data,i=window.nisje.settings,o=window.wp.dataControls;const c={visible:!1,value:"",results:!1,tab:""};function u(e){return{type:"VIEW_SEARCH",visible:e}}function l(e){return{type:"SET_VALUE",value:e}}function m(e){return{type:"SET_RESULT",value:e}}function d(e){return{type:"SET_TAB",tab:e}}function f(e){return e.visible}function h(e){return e.value}function p(e){return e.results}function v(e){return e.tab}(0,a.registerStore)("nisje/search",{reducer:(e=c,t)=>{switch(t.type){case"VIEW_SEARCH":return{...e,visible:t.visible};case"SET_VALUE":return t.value===e.value?e:{...e,value:t.value,results:!1};case"SET_RESULT":return{...e,results:t.value};case"SET_TAB":return{...e,tab:t.tab};default:return e}},controls:o.controls,actions:n,selectors:r});var w=window.wp.i18n,E=window.nisje.interface,b=window.nisje.track,g=window.nisje.icons,_=function(){const{setVisible:e}=(0,a.useDispatch)("nisje/search");return(0,s.createElement)(E.ToolbarMenuControls,null,(0,s.createElement)(E.ToolbarMenuButton,{icon:g.search,label:(0,w.__)("Search"),onClick:()=>{e(!0),(0,b.trackEvent)("view_search","engagement")}}))},j=window.lodash,y=window.React,S=window.wp.components,T=window.nisje.format,C=window.wp.hooks,R=window.wp.apiFetch,V=e.n(R),k=window.nisje.components;const A=(0,window.nisje.styled.styled)(k.ExperimentalButton)`
	border-bottom: 2px solid
		${({isActive:e})=>e?i.ui.colors.primary:"transparent"};
	border-radius: 0;
	font-size: 0.875rem;
	height: 2.75rem;
	margin-bottom: -1px;
	margin-right: 1rem;
	padding: 0 0.25rem;
`;function N(){return(0,j.map)((0,i.getSearchTypes)(),(({render:e,name:t,title:n})=>({Component:e,context:{title:n,name:t}})))}var x=function({value:e,onRequestClose:t}){const[n,r]=(0,y.useState)(N()),{results:i,tab:o}=(0,a.useSelect)((e=>{const{getResults:t,getTab:n}=e("nisje/search");return{results:t(),tab:n()}})),{setResult:c,setTab:u}=(0,a.useDispatch)("nisje/search"),{receiveRecords:l}=(0,a.useDispatch)("nisje/core");function m(){r(N())}(0,y.useEffect)((()=>((0,C.addAction)("search.typesRegistered","core/search/types-registered",m),()=>{(0,C.removeAction)("search.typesRegistered","core/search/types-registered")})),[]),(0,y.useEffect)((()=>{V()({path:`/nisje/v1/search?search=${e}`}).then((e=>{l(e["user-content"].items),c(e)}))}),[e]),(0,y.useEffect)((()=>{!i||o&&0!==i[o].total||u((0,j.findKey)(i,(e=>0!==e.total)))}),[i,u,o]);const d=n.some((({context:e})=>i[e.name]&&0!==i[e.name].total));return i&&!d?(0,s.createElement)("div",{className:"search-no-results"},(0,s.createElement)(k.TextDeprecated,{as:"h2",variant:"title-small"},(0,w.sprintf)((0,w.__)("No results for “%s”"),e))):(0,s.createElement)(s.Fragment,null,(0,s.createElement)("div",{className:"search-tabs"},n.map((({context:e})=>{const t=`search-${e.name}`;return i?i[e.name]&&0!==i[e.name].total?(0,s.createElement)(A,{key:t,onClick:()=>u(e.name),isActive:e.name===o},e.title," ",i[e.name].total):null:(0,s.createElement)("span",{key:t,className:"search-tab-loader nisje-post-loader__stroke"})}))),(0,s.createElement)("div",{className:"search-content"},!i&&(0,s.createElement)("div",{className:"search-content__loader"},(0,s.createElement)(k.Loader,{size:24})),i&&n.map((({context:e,Component:n})=>{var r;if(!i[e.name]||0===i[e.name].total)return null;const a=`search-${e.name}`;return(0,s.createElement)(n,{key:a,items:null===(r=i[e.name])||void 0===r?void 0:r.items,isActive:e.name===o,onClick:t})}))))};function D({value:e,onChange:t,handleSubmit:n}){const r=(0,y.useRef)(),{setValue:i}=(0,a.useDispatch)("nisje/search");return(0,y.useEffect)((()=>{r.current.focus()}),[]),(0,s.createElement)(s.Fragment,null,(0,s.createElement)(g.Icon,{icon:g.search,size:20}),(0,s.createElement)("form",{onSubmit:function(t){t.preventDefault(),n(e)},className:"nisje-search__form"},(0,s.createElement)("input",{className:"nisje-search__input",value:e,onChange:e=>{i(""),t(e.target.value)},ref:r})))}var M=function(){const{setVisible:e,setValue:t}=(0,a.useDispatch)("nisje/search"),n=(0,a.useSelect)((e=>e("nisje/search").getValue())),[r,i]=(0,y.useState)(n),o=!(0,j.isEmpty)(n),c=!o&&r;function u(e){t(e),(0,b.trackEvent)("search","engagement",e)}return(0,s.createElement)(S.Modal,{onRequestClose:()=>e(!1),className:(0,T.classnames)("nisje-search",{"has-value":o,"has-border":o||c}),title:(0,s.createElement)(D,{value:r,onChange:i,handleSubmit:u})},c&&(0,s.createElement)("button",{className:"nisje-search__trigger",onClick:()=>u(r),type:"button"},
/* translators: %s: search string */
(0,w.sprintf)((0,w.__)("Search for “%s”"),r)),o&&(0,s.createElement)(x,{value:n,onRequestClose:()=>e(!1)}))};(0,i.registerPlugin)("search",{render(){const e=(0,a.useSelect)((e=>e("nisje/search").isVisible()));return(0,s.createElement)(s.Fragment,null,(0,s.createElement)(_,null),e&&(0,s.createElement)(M,null))}}),(window.nisje=window.nisje||{}).search=t}();