Liferay.Loader.define("commerce-frontend-taglib@19.1.14/search_bar/SearchBar.es",["module","exports","require","frontend-taglib-clay$clay-icon","frontend-js-metal-web$metal-component","frontend-js-metal-web$metal-soy","./SearchBar.soy"],(function(e,t,r){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.default=t.SearchBar=void 0,r("frontend-taglib-clay$clay-icon");var 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={},n=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var i in e)if(Object.prototype.hasOwnProperty.call(e,i)){var a=n?Object.getOwnPropertyDescriptor(e,i):null;a&&(a.get||a.set)?Object.defineProperty(r,i,a):r[i]=e[i]}r.default=e,t&&t.set(e,r);return r}(r("frontend-js-metal-web$metal-soy")),a=_interopRequireDefault(r("./SearchBar.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}}class SearchBar extends n.default{created(){this._handleDocumentKeypress=this._handleDocumentKeypress.bind(this),this._handleClickOutside=this._handleClickOutside.bind(this),this.toggle=this.toggle.bind(this),document.addEventListener("keydown",this._handleDocumentKeypress),this._addOpenButtonListener()}detached(){document.removeEventListener("keydown",this._handleDocumentKeypress)}_addOpenButtonListener(){return Array.from(document.querySelectorAll(".js-toggle-search")).map((e=>e.addEventListener("click",this.toggle)))}_removeOpenButtonListener(){return Array.from(document.querySelectorAll(".js-toggle-search")).map((e=>e.removeEventListener("click",this.toggle)))}_handleClickOutside(e){const t=document.querySelector(".commerce-suggestions");this.element.contains(e.target)||t&&t.contains(e.target)||this.toggle()}_handleDocumentKeypress(e){this.active&&"Escape"===e.key&&this.close();const t=!Array.from(document.querySelectorAll(".modal.show")).length;!this.active&&"/"===e.key&&"INPUT"!==e.target.tagName&&t&&(e.preventDefault(),this.open())}_handleResetQuery(e){this._updateQuery("")}_handleKeyDown(e){"ArrowDown"!==e.key&&"ArrowUp"!==e.key||e.preventDefault()}_handleKeyUp(e){this._updateQuery(e.target.value)}_handleSubmit(e){e.preventDefault(),window.Liferay.fire("search-term-submit",{term:this.query})}_updateQuery(e){e!==this.query&&(this.open(),this.query=e,window.Liferay.fire("search-term-update",{term:e}))}syncActive(){this.active?(window.addEventListener("click",this._handleClickOutside),setTimeout((()=>{this._removeOpenButtonListener(),this.refs.searchInput.focus()}),0)):(window.removeEventListener("click",this._handleClickOutside),setTimeout((()=>{this._addOpenButtonListener(),this.refs.searchInput.blur()}),0)),this.emit("toggled",this.active)}open(){this.active=!0}close(){this.active=!1}toggle(){return this.active?this.close():this.open()}}t.SearchBar=SearchBar,i.default.register(SearchBar,a.default),SearchBar.STATE={active:i.Config.bool(),placeholder:i.Config.value(""),query:i.Config.string()};var o=SearchBar;t.default=o}}));
//# sourceMappingURL=SearchBar.es.js.map