define(['jquery','ko','uiRegistry','amsearch_helpers','uiComponent','amsearch_color_helper','mage/translate','amsearchProductLinksStorage','Amasty_Base/vendor/slick/slick.min'],function($,ko,registry,helpers,Component,colorHelper,$t,productLinksStorage){'use strict';return Component.extend({defaults:{isMobile:$(window).width()"%search_query%"')},selectors:{wrapper:'[data-amsearch-wrapper="block"]',}},initialize:function(){this._super();registry.get(this.components,function(){helpers.initComponentsArray(arguments,this);this._initInput();this._initResize();this._initOverlay();}.bind(this));return this;},initObservable:function(){var focusSubscriber;this._super().observe({loading:false,focused:false,opened:false,preload:false,readyForSearch:false,searchProducts:false,inputValue:'',resultSectionStyles:false,resized:false,searchItems:[],match:false,message:''});if(this.data.preloadEnabled){focusSubscriber=this.focused.subscribe(function(value){if(value){focusSubscriber.dispose();this.updatePreload();}},this);} return this;},onEnter:function(UiClass,event){if(event.keyCode===13){productLinksStorage.saveLinks($(this.selectors.wrapper));this.search();return false;} return true;},search:function(){window.location=this.data.url_result+'?q='+this.inputValue().replace(/\&/g,'%26');},close:function(){this.opened(false);this.inputValue('');this.amsearch_overlay_section.hide();},updateHtml:function(node){helpers.applyBindings(node,this);},initCssVariables:function(node){Object.keys(this.data.color_settings).forEach(function(key){node.style.setProperty('--amsearch-color-'+key,'#'+this.data.color_settings[key]);node.style.setProperty('--amsearch-color-'+key+'-focus',colorHelper.getDarken('#'+this.data.color_settings[key],0.1));node.style.setProperty('--amsearch-color-'+key+'-hover',colorHelper.getDarken('#'+this.data.color_settings[key],0.05));}.bind(this));},updatePreload:function(){$.ajax({url:this.data.url.slice(0,-1)+'recent',data:{uenc:this.data.currentUrlEncoded},dataType:'html',success:function(html){this.preload(html);}.bind(this)});},searchProcess:function(value){this.loading(true);return $.get(this.data.url,{q:value,uenc:this.data.currentUrlEncoded,form_key:$.mage.cookies.get('form_key')},$.proxy(function(data){this.opened(true);this.match(true);this.loading(false);this._parseSearchData(data);},this));},initResultSection:function(node,isBaseSearch){if(!isBaseSearch){var subscriber=this.opened.subscribe(function(value){if(value){helpers.setNodePositionByViewport(node);subscriber.dispose();}}.bind(this));} this.nodes.results=node;this.resultSectionStyles({background:this.data.color_settings.background,borderColor:this.data.color_settings.border,color:this.data.color_settings.text});},getProductsBlockClasses:function(){return this.data.popup_display?'amsearch-products-section -list':'amsearch-products-section -grid'},initInputValue:function(node){var value=node.value;if(this.data.isSaveSearchInputValueEnabled){var query=this.getSearchQuery();} if(value&&value.length){this.inputValue(node.value);}else if(query){this.inputValue(query);}},_initInput:function(){this.readyForSearch=ko.computed(function(){return this.inputValue().length>=this.data.minChars;}.bind(this));this.inputValue.extend({rateLimit:{method:'notifyWhenChangesStop',timeout:this.data.delay}}).subscribe(function(value){if(this.getSearchQuery()===value){return false;} var isSearch=value.length>=this.data.minChars,strippedValue=helpers.stripTags(value);if(isSearch&&strippedValue){this.inputValue.silentUpdate(strippedValue);return false;} this.message(false);if(isSearch){this.searchProcess(value);}else{this.searchItems.removeAll();this.searchProducts(false);this.match(false);}}.bind(this));},_initOverlay:function(){this.focused.subscribe(function(value){if(value){this.amsearch_overlay_section.show();this.opened(true);helpers.initProductAddToCart(this.nodes.results);}}.bind(this));this.amsearch_overlay_section.opened.subscribe(function(value){if(!value){this.opened(false);}}.bind(this));},_initResize:function(){if(this.isMobile){return false;} this.resized=ko.computed(function(){return this.readyForSearch()&&this.data.width&&this.opened();}.bind(this));},_parseSearchData:function(data){var searchItems=[];Object.keys(data).forEach(function(key){if(data[key].type==='product'){const isEmptyData=jQuery.isEmptyObject($.parseHTML(data[key].html.trim())),isProductsSeparateSection=this.isNeedHorizontalView();if(isEmptyData){this.searchProducts([]);this.message(this.messages.emptyProductSearch.replace('%search_query%',this.inputValue()));return;} if(isProductsSeparateSection){this.searchProducts(data[key].html);}else{searchItems.push(data[key]);} return;} if(data[key].html===undefined||data[key].html.length<=1){return;} searchItems.push(data[key]);}.bind(this));this.searchItems(searchItems);},isNeedHorizontalView:function(){return this.data.fullWidth||this.data.width>=700&&window.innerWidth>=768;},getSearchQuery:function(){if(this.data.isSeoUrlsEnabled){var currentUrl=window.location.href,seoKey='/'+this.data.seoKey+'/';if(currentUrl.includes(seoKey)){return decodeURIComponent(currentUrl.split('/').pop()?.replace(/\+/g,' ')??'');}} return new URLSearchParams(window.location.search).get('q');}});});