define(['ko','jquery','underscore','mage/template','Magento_Catalog/js/price-utils','Magento_Ui/js/lib/knockout/template/loader','Magento_Ui/js/modal/modal','mage/translate','Magento_Search/js/form-mini'],function(ko,$,_,mageTemplate,priceUtil,templateLoader){'use strict';$.widget('smileEs.quickSearch',$.mage.quickSearch,{options:{autocomplete:'off',minSearchLength:2,responseFieldElements:'dl dd',selectClass:'selected',submitBtn:'button[type="submit"]',searchLabel:'[data-role=minisearch-label]'},_create:function(){this.templateCache=[];this.currentRequest=null;this._initTemplates();this._initTitleRenderer();this._super();this._blur();},_initTemplates:function(){for(var template in this.options.templates){if({}.hasOwnProperty.call(this.options.templates,template)){this._loadTemplate(template);}}},_initTitleRenderer:function(){this.titleRenderers={};for(var typeIdentifier in this.options.templates){if(this.options.templates[typeIdentifier]['titleRenderer']){require([this.options.templates[typeIdentifier]['titleRenderer']],function(renderer){this.component.titleRenderers[this.type]=renderer;}.bind({component:this,type:typeIdentifier}));}}},_loadTemplate:function(type){var templateFile=this.options.templates[type]['template'];templateLoader.loadTemplate(templateFile).done(function(renderer){this.options.templates[type]['template']=renderer;}.bind(this));},_getTemplate:function(element){var source=this.options.template;var type=element.type?element.type:'undefined';if(this.templateCache[type]){return this.templateCache[type];}
if(element.type&&this.options.templates&&this.options.templates[element.type]){source=this.options.templates[element.type].template;}
this.templateCache[type]=mageTemplate(source);return this.templateCache[type];},_renderItem:function(element,index){var template=this._getTemplate(element);element.index=index;if(element.price&&(!isNaN(element.price))){element.price=priceUtil.formatPrice(element.price,this.options.priceFormat);}
return template({data:element});},_getResultWrapper:function(){return $('
');},_getSectionHeader:function(type,data){var title='';var header=$('
');if(type!==undefined){title=this._getSectionTitle(type,data);header.append(title);}
return header;},_getSectionTitle:function(type,data){var title='';if(this.titleRenderers&&this.titleRenderers[type]){title=$(''+this.titleRenderers[type].render(data)+'');}else if(this.options.templates&&this.options.templates[type].title){title=$(''+this.options.templates[type].title+'');}
return title;},_isEmpty:function(value){return value===null||value.trim().length===0;},_onPropertyChange:_.debounce(function(){var searchField=this.element,clonePosition={position:'absolute',width:searchField.outerWidth()},value=this.element.val();this.submitBtn.disabled=this._isEmpty(value);if(value.trim().length>=parseInt(this.options.minSearchLength,10)){this.searchForm.addClass('processing');this.currentRequest=$.ajax({method:"GET",url:this.options.url,cache:true,dataType:'json',data:{q:value},beforeSend:function(){if(this.currentRequest!==null){this.currentRequest.abort();}}.bind(this),success:$.proxy(function(data){var self=this;var lastElement=false;var content=this._getResultWrapper();var sectionDropdown=this._getSectionHeader();$.each(data,function(index,element){if(!lastElement||(lastElement&&lastElement.type!==element.type)){sectionDropdown=this._getSectionHeader(element.type,data);}
var elementHtml=this._renderItem(element,index);sectionDropdown.append(elementHtml);if(!lastElement||(lastElement&&lastElement.type!==element.type)){content.append(sectionDropdown);}
lastElement=element;}.bind(this));this.responseList.indexList=this.autoComplete.html(content).css(clonePosition).show().find(this.options.responseFieldElements+':visible');this._resetResponseList(false);this.element.removeAttr('aria-activedescendant');if(this.responseList.indexList.length){this._updateAriaHasPopup(true);}else{this._updateAriaHasPopup(false);}
this.responseList.indexList.on('click vclick',function(e){self.responseList.selected=$(this);if(self.responseList.selected.attr("href")){window.location.href=self.responseList.selected.attr("href");e.stopPropagation();return false;}
self.searchForm.trigger('submit');}).on('mouseenter',function(e){self.responseList.indexList.removeClass(self.options.selectClass);$(this).addClass(self.options.selectClass);self.responseList.selected=$(e.target);self.element.attr('aria-activedescendant',$(e.target).attr('id'));}).on('mouseleave',function(e){$(this).removeClass(self.options.selectClass);self._resetResponseList(false);}).on('mouseout',function(){if(!self._getLastElement()&&self._getLastElement().hasClass(self.options.selectClass)){$(this).removeClass(self.options.selectClass);self._resetResponseList(false);}});},this),complete:$.proxy(function(){this.searchForm.removeClass('processing');},this)});}else{this._resetResponseList(true);this.autoComplete.hide();this._updateAriaHasPopup(false);this.element.removeAttr('aria-activedescendant');}},250),_onKeyDown:function(e){var keyCode=e.keyCode||e.which;switch(keyCode){case $.ui.keyCode.HOME:this._selectElement(this._getFirstVisibleElement());break;case $.ui.keyCode.END:this._selectElement(this._getLastElement());break;case $.ui.keyCode.ESCAPE:this._resetResponseList(true);this.autoComplete.hide();break;case $.ui.keyCode.ENTER:this._validateElement(e);break;case $.ui.keyCode.DOWN:this._navigateDown();break;case $.ui.keyCode.UP:this._navigateUp();break;default:return true;}},_validateElement:function(event){var selected=this.responseList.selected;if(selected&&selected.attr('href')!==undefined){window.location=selected.attr('href');event.preventDefault();return false;}
this.searchForm.trigger('submit');},_navigateDown:function(){if(this.responseList.indexList){if(!this.responseList.selected){this._getFirstVisibleElement().addClass(this.options.selectClass);this.responseList.selected=this._getFirstVisibleElement();}
else if(!this._getLastElement().hasClass(this.options.selectClass)){var nextElement=this._getNextElement();this.responseList.selected.removeClass(this.options.selectClass);this.responseList.selected=nextElement.addClass(this.options.selectClass);}else{this.responseList.selected.removeClass(this.options.selectClass);this._getFirstVisibleElement().addClass(this.options.selectClass);this.responseList.selected=this._getFirstVisibleElement();}
this._activateElement();}},_navigateUp:function(){if(this.responseList.indexList!==null){if(!this._getFirstVisibleElement().hasClass(this.options.selectClass)){var prevElement=this._getPrevElement();this.responseList.selected.removeClass(this.options.selectClass);this.responseList.selected=prevElement.addClass(this.options.selectClass);}else{this.responseList.selected.removeClass(this.options.selectClass);this._getLastElement().addClass(this.options.selectClass);this.responseList.selected=this._getLastElement();}
this._activateElement();}},_selectElement:function(element){element.addClass(this.options.selectClass);this.responseList.selected=element;},_activateElement:function(){this.element.val(this.responseList.selected.find('.qs-option-name').text());this.element.attr('aria-activedescendant',this.responseList.selected.attr('id'));},_getNextElement:function(){var nextElement=this.responseList.selected.next('dd');if(nextElement.length===0){nextElement=this.responseList.selected.parent('dl').next('dl').find('dd').first();}
return nextElement;},_getPrevElement:function(){var prevElement=this.responseList.selected.prev('dd');this.responseList.selected.removeClass(this.options.selectClass);if(prevElement.length===0){prevElement=this.responseList.selected.parent('dl').prev('dl').find('dd').last();}
return prevElement;},_blur:function(){this.element.on('blur',$.proxy(function(){if(!this.searchLabel.hasClass('active')){return;}
setTimeout($.proxy(function(){if(this.autoComplete.is(':hidden')){this.setActiveState(false);}else{this.element.trigger('focus');}
this.autoComplete.hide();$('#search').trigger('blur');this._updateAriaHasPopup(false);},this),250);},this));}});return $.smileEs.quickSearch;});