Liferay.Loader.define("commerce-frontend-taglib@19.1.14/search_results/SearchResults.es",["module","exports","require","frontend-js-web","frontend-js-metal-web$metal-component","frontend-js-metal-web$metal-soy","./SearchResults.soy"],(function(e,t,r){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.default=t.SearchResults=void 0;var s=r("frontend-js-web"),n=_interopRequireDefault(r("frontend-js-metal-web$metal-component")),i=function _interopRequireWildcard(e){if(e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var t=_getRequireWildcardCache();if(t&&t.has(e))return t.get(e);var r={},s=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var n in e)if(Object.prototype.hasOwnProperty.call(e,n)){var i=s?Object.getOwnPropertyDescriptor(e,n):null;i&&(i.get||i.set)?Object.defineProperty(r,n,i):r[n]=e[n]}r.default=e,t&&t.set(e,r);return r}(r("frontend-js-metal-web$metal-soy")),o=_interopRequireDefault(r("./SearchResults.soy"));function _getRequireWildcardCache(){if("function"!=typeof WeakMap)return null;var e=new WeakMap;return _getRequireWildcardCache=function _getRequireWildcardCache(){return e},e}function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);t&&(s=s.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,s)}return r}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(r),!0).forEach((function(t){_defineProperty(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}class SearchResults extends n.default{created(){this.search=(0,s.debounce)(this.search.bind(this),500),this.handleKeyDown=this.handleKeyDown.bind(this),window.Liferay.on("search-term-update",this.updateQuery,this),window.Liferay.on("search-term-submit",this.goToSelected,this)}detached(){window.Liferay.detach("search-term-update",this.updateQuery,this),window.Liferay.detach("search-term-submit",this.goToSelected,this),document.removeEventListener("keydown",this.handleKeyDown)}getFirstSuggestion(){const e=this.results.filter((e=>"label"!==e.type));return e.length?e[0].pos:-1}getLastSuggestion(){const e=this.results.filter((e=>"label"!==e.type)).reverse();return e.length?e[0].pos:-1}goToSelected(){const e=this.results.filter((e=>e.selected));e.length&&e[0].url&&(Liferay.SPA?Liferay.SPA.app.navigate(e[0].url):window.location.href=e[0].url)}handleKeyDown(e){"ArrowDown"===e.key?this.selectNext():"ArrowUp"===e.key&&this.selectPrevious()}handleMouseEnter(e){this.selectedIndex=parseInt(e.delegateTarget.dataset.pos,10)}handleMouseLeave(e){this.selectedIndex=this.getFirstSuggestion()}rendered(){this.refs&&this.refs.selected&&this.refs.selected.scrollIntoView({behavior:"smooth",block:"nearest"})}search(){this.lock||(this.lock=!0,(0,s.fetch)(this.searchAPI+themeDisplay.getPlid()+"?commerceAccountId="+this.commerceAccountId+"&groupId="+themeDisplay.getScopeGroupId()+"&q="+this.queryString).then((e=>e.json())).then((e=>{this.loading=!1,this.lock=!1,this.queryValue=this.queryString,this.results=e,this.selectedIndex=-1,this.selectNext()})))}selectNext(){const e=this.results.filter((e=>e.pos>this.selectedIndex&&"label"!==e.type));this.selectedIndex=e.length?e[0].pos:this.getFirstSuggestion()}selectPrevious(){const e=this.results.filter((e=>e.pos<this.selectedIndex&&"label"!==e.type)).reverse();this.selectedIndex=e.length?e[0].pos:this.getLastSuggestion()}setSelected(e){return e=(e+1+this.results.length+1)%(this.results.length+1)-1,this.results=this.results.map(((t,r)=>_objectSpread(_objectSpread({},t),{},{pos:r,selected:r===e}))),e}updateQuery(e){this.loading=!0,this.queryString=e.term,this.visible=""!==e.term,this.search()}willUpdate({visible:e}){e&&(e.newVal?document.addEventListener("keydown",this.handleKeyDown):document.removeEventListener("keydown",this.handleKeyDown))}_handleClick(){this.goToSelected()}}t.SearchResults=SearchResults,i.default.register(SearchResults,o.default),SearchResults.STATE={commerceAccountId:i.Config.oneOfType([i.Config.number(),i.Config.string()]),loading:i.Config.bool().value(!1),queryString:i.Config.string().value(""),queryValue:i.Config.string().value(""),results:{value:[]},searchAPI:i.Config.string().required(),selectedIndex:{setter:"setSelected",value:-1},spritemap:i.Config.string().required(),visible:i.Config.bool().value(!1)};var l=SearchResults;t.default=l}}));
//# sourceMappingURL=SearchResults.es.js.map