define(['jquery','algoliaAnalytics','algoliaBundle','algoliaCommon','mage/cookies'],function($,algoliaAnalyticsWrapper,algoliaBundle){algoliaAnalytics=algoliaAnalyticsWrapper.default;window.algoliaInsights={config:null,defaultIndexName:null,isTracking:false,hasAddedParameters:false,useCookie:false,determineUserToken(){return algoliaAnalytics.getAuthenticatedUserToken()??algoliaAnalytics.getUserToken();},track:function(algoliaConfig,partial=false){this.config=algoliaConfig;this.defaultIndexName=algoliaConfig.indexName+'_products';this.useCookie=this.config.cookieConfiguration.cookieRestrictionModeEnabled?!!getCookie(this.config.cookieConfiguration.consentCookieName):true;if(this.isTracking||this.useCookie===false){return;} if(algoliaConfig.ccAnalytics.enabled||algoliaConfig.personalization.enabled){this.initializeAnalytics(partial);this.addSearchParameters();this.bindData();this.bindEvents();this.isTracking=true;}},initializeAnalytics:function(partial=false){let useCookie=this.config.cookieConfiguration.cookieRestrictionModeEnabled?!!getCookie(this.config.cookieConfiguration.consentCookieName):true;if(partial){algoliaAnalytics.init({appId:this.config.applicationId,apiKey:this.config.apiKey,partial:true,useCookie:useCookie,cookieDuration:Number(this.config.cookieConfiguration.cookieDuration)});}else{algoliaAnalytics.init({appId:this.config.applicationId,apiKey:this.config.apiKey,useCookie:useCookie,cookieDuration:Number(this.config.cookieConfiguration.cookieDuration)});} var userAgent='insights-js-in-magento ('+this.config.extensionVersion+')';algoliaAnalytics.addAlgoliaAgent(userAgent);var userToken=getCookie('aa-search');var unsetAuthenticationToken=getCookie('unset_authentication_token');if(userToken&&userToken!==''){algoliaAnalytics.setAuthenticatedUserToken(userToken);}else if(unsetAuthenticationToken&&unsetAuthenticationToken!==''){algoliaAnalytics.setAuthenticatedUserToken('undefined');$.mage.cookies.clear('unset_authentication_token');}},applyInsightsToSearchParams(params={}){if(algoliaConfig.ccAnalytics.enabled){params.clickAnalytics=true;} if(algoliaConfig.personalization.enabled){params.enablePersonalization=true;params.userToken=this.determineUserToken();} return params;},addSearchParameters:function(){if(this.hasAddedParameters){return;} algolia.registerHook('beforeWidgetInitialization',(allWidgetConfiguration)=>{allWidgetConfiguration.configure=algoliaInsights.applyInsightsToSearchParams(allWidgetConfiguration.configure);return allWidgetConfiguration;});algolia.registerHook('afterAutocompleteProductSourceOptions',(options)=>{return algoliaInsights.applyInsightsToSearchParams(options);});this.hasAddedParameters=true;},bindData:function(){var persoConfig=this.config.personalization;if(persoConfig.enabled&&persoConfig.clickedEvents.productRecommended.enabled){$(persoConfig.clickedEvents.productRecommended.selector).each(function(index,element){if($(element).find('[data-role="priceBox"]').length){var objectId=$(element).find('[data-role="priceBox"]').data('product-id');$(element).attr('data-objectid',objectId);}});}},bindEvents:function(){this.bindClickedEvents();this.bindViewedEvents();algolia.triggerHooks('afterInsightsBindEvents',this);},bindClickedEvents:function(){var self=this;$(function($){$(self.config.autocomplete.selector).on('autocomplete:selected',function(e,suggestion){var eventData=self.buildEventData('Clicked',suggestion.objectID,suggestion.__indexName,suggestion.__position,suggestion.__queryID);self.trackClick(eventData);});});if(this.config.ccAnalytics.enabled){$(document).on('click',this.config.ccAnalytics.ISSelector,function(){var $this=$(this);if($this.data('clicked'))return;var eventData=self.buildEventData('Clicked',$this.data('objectid'),$this.data('indexname'),$this.data('position'),$this.data('queryid'));self.trackClick(eventData);$this.attr('data-clicked',true);});} if(this.config.personalization.enabled){var clickEvents=Object.keys(this.config.personalization.clickedEvents);for(var i=0;i