"use strict";(self.webpackChunkui=self.webpackChunkui||[]).push([[28],{57:function(e,t,s){var r=this&&this.__awaiter||function(e,t,s,r){return new(s||(s=Promise))((function(l,n){function a(e){try{c(r.next(e))}catch(e){n(e)}}function u(e){try{c(r.throw(e))}catch(e){n(e)}}function c(e){var t;e.done?l(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(a,u)}c((r=r.apply(e,t||[])).next())}))},l=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.Search=function({onClose:e,useSharedState:t,isMobileSearchRoute:s}){const l=(0,c.useRef)(null),[o,p]=(0,c.useState)(""),[f,x]=(0,c.useState)(""),[v,j]=(0,c.useState)(),[S,m]=(0,c.useState)(),y=(0,c.useCallback)((()=>r(this,void 0,void 0,(function*(){var e;const t=yield(0,i.getLazyState)("popularProducts",{featured_products_section_selector:null!==(e=(0,i.getAssignment)("search-featured-products"))&&void 0!==e?e:""});m(t)}))),[m]),R=(0,c.useCallback)((e=>r(this,void 0,void 0,(function*(){const t=yield(0,i.getLazyState)("search",{query:e});j(t)}))),[j]),b=(0,c.useCallback)((()=>{var e;p(""),null===(e=l.current)||void 0===e||e.focus()}),[p]);return(0,c.useEffect)((()=>{var e;null===(e=l.current)||void 0===e||e.focus(),y()}),[]),(0,c.useEffect)((()=>{const e=setTimeout((()=>{x(o.trim())}),200);return()=>clearTimeout(e)}),[o]),(0,c.useEffect)((()=>{f&&R(f)}),[f]),(0,n.jsx)(a.Fragment,{children:(0,n.jsxs)("div",{class:u.default.container,children:[(0,n.jsxs)("div",{class:u.default.bar,children:[(0,n.jsxs)("form",{class:u.default.inputContainer,onSubmit:e=>{if(e.preventDefault(),o.trim()){const e=`/search?q=${encodeURIComponent(o.trim())}`;s&&location.replace(e),window.location.href=e}},children:[(0,n.jsx)("input",{type:"search",placeholder:"Search",value:o,onInput:e=>p(e.currentTarget.value),ref:l,autofocus:!0}),o&&(0,n.jsx)("button",{type:"button",onClick:b,children:"Clear"}),(0,n.jsx)("button",{type:"submit",hidden:!0,children:"Search"})]}),(0,n.jsx)("button",{type:"button",onClick:e,class:u.default.closeButton,"aria-label":"Close search",children:(0,n.jsx)(d.CloseIcon,{iconClass:u.default.closeButton,size:"18px"})})]}),(0,n.jsx)(h,{searchQuery:o,searchResults:v,initialPopularResults:S,useSharedState:t,isMobileSearchRoute:s})]})})};const n=s(0),a=s(3),u=l(s(204)),c=s(2),i=s(1),o=s(46),d=s(68);function h({searchQuery:e,searchResults:t,initialPopularResults:s,useSharedState:r,isMobileSearchRoute:l}){return(0,n.jsxs)("div",{class:u.default.results,children:[(0,n.jsx)(p,{searchQuery:e,searchResults:t,initialPopularResults:s,isMobileSearchRoute:l}),(0,n.jsx)(f,{searchQuery:e,searchResults:t,initialPopularResults:s,useSharedState:r,isMobileSearchRoute:l})]})}function p({searchQuery:e,searchResults:t,initialPopularResults:s,isMobileSearchRoute:r}){var l,c;return(0,n.jsxs)("div",{class:u.default.queries,children:[e&&t&&(null===(l=null==t?void 0:t.queries)||void 0===l?void 0:l.length)>0&&(0,n.jsxs)(a.Fragment,{children:[(0,n.jsx)("h6",{children:"Suggestions"}),(0,n.jsx)("ul",{class:u.default.suggestions,children:t.queries.map((e=>{const t=`/search?q=${encodeURIComponent(e.text)}`;return(0,n.jsx)("li",{children:(0,n.jsx)("a",{title:e.text,href:t,onClick:e=>{r&&(e.preventDefault(),location.replace(t))},dangerouslySetInnerHTML:{__html:e.styledText}})},e.styledText)}))})]}),s&&(null===(c=s.queries)||void 0===c?void 0:c.length)>0&&(0,n.jsxs)(a.Fragment,{children:[(0,n.jsx)("h6",{children:"Trending Search Terms"}),(0,n.jsx)("ul",{children:s.queries.map((e=>(0,n.jsx)("li",{children:(0,n.jsx)("a",{href:`/search?q=${encodeURIComponent(e.text)}`,dangerouslySetInnerHTML:{__html:e.styledText}})},e.styledText)))})]})]})}function f({searchQuery:e,searchResults:t,initialPopularResults:s,useSharedState:r,isMobileSearchRoute:l}){var c,i;return(0,n.jsxs)("div",{class:u.default.productsContainer,children:[e&&t&&(0,n.jsx)(a.Fragment,{children:t.products.length>0?(0,n.jsxs)(a.Fragment,{children:[(0,n.jsxs)("div",{class:u.default.productsHeader,children:[(0,n.jsx)("h6",{children:"Products"}),(0,n.jsxs)("a",{href:`/search?q=${encodeURIComponent(e)}`,onClick:t=>{l&&(t.preventDefault(),location.replace(`/search?q=${encodeURIComponent(e)}`))},children:["View All ",t.productsTotalCount,' Results for "',e,'"']})]}),(0,n.jsx)("div",{class:u.default.productsGrid,children:t.products.map((e=>(0,n.jsx)(o.ProductCard,{product:e,collectionHandle:"search",sharedStateStore:r,replaceStateEnabled:l},e.id)))})]}):(0,n.jsxs)("div",{class:u.default.productsHeaderEmpty,children:[(0,n.jsx)("h6",{children:"No Results Found"}),(0,n.jsxs)("span",{children:["Sorry, we couldn't find \"",t.query,'"']})]})}),(!e||!(null===(c=null==t?void 0:t.products)||void 0===c?void 0:c.length))&&(null===(i=null==s?void 0:s.products)||void 0===i?void 0:i.length)&&(0,n.jsxs)(a.Fragment,{children:[(0,n.jsxs)("div",{class:u.default.productsHeader,children:[(0,n.jsx)("h6",{children:"Popular Products"}),s.productsViewAllUrl&&(0,n.jsx)("a",{href:s.productsViewAllUrl,children:"View All"})]}),(0,n.jsx)("div",{class:u.default.productsGrid,children:s.products.map((e=>(0,n.jsx)(o.ProductCard,{product:e,collectionHandle:"search",sharedStateStore:r,replaceStateEnabled:l},e.id)))})]})]})}}}]);