@nuskin/react-search-components 1.0.0-comx-3067.5 → 1.0.0-comx-3067.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
- import React, { useState, useEffect, useRef, useId, useCallback } from 'react';
1
+ import React, { useState, useEffect, useId, useRef, useCallback, useMemo } from 'react';
2
2
  import styled6 from '@emotion/styled';
3
3
  import { nuskinTheme } from '@nuskin/foundation-theme';
4
4
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
- import { createChatService, isCartActionDisabled, useSearchHistory, useSearchSuggestions, getVisibleSuggestions, isSuggestionDisabled, useSearchHistoryStore, useVoiceSearch } from '@nuskin/react-search-sdk';
5
+ import { createChatService, isCartActionDisabled, useSearchHistory, useSearchSuggestions, getVisibleSuggestions, trackChatEntryPoint, trackSearchType, SEARCH_GTM_SEARCH_TYPES, trackProductSearch, trackErrorPopup, isSuggestionDisabled, useSearchHistoryStore, useVoiceSearch } from '@nuskin/react-search-sdk';
6
6
  export { createSearchService } from '@nuskin/react-search-sdk';
7
7
  import { createPortal } from 'react-dom';
8
8
  import { ProductCarousel, PRODUCT_CAROUSEL_ERROR_CODES } from '@nuskin/react-product-components';
@@ -12000,6 +12000,7 @@ function SearchMessage({ sender, text: text4, ...props }) {
12000
12000
 
12001
12001
  // src/components/Icon/iconConstants.ts
12002
12002
  var ICON_TYPES = {
12003
+ ARROW_DOWN: "arrowDown",
12003
12004
  ARROW_LEFT: "arrowLeft",
12004
12005
  ARROW_RIGHT: "arrowRight",
12005
12006
  ARROW_UP: "arrowUp",
@@ -12045,6 +12046,7 @@ var ICON_COLOR_BY_TYPE = {
12045
12046
  [ICON_TYPES.STAR_GREY]: "#c9c9c9"
12046
12047
  };
12047
12048
  var TEMPORARY_GLYPH_BY_ICON_TYPE = {
12049
+ [ICON_TYPES.ARROW_DOWN]: "\u2193",
12048
12050
  [ICON_TYPES.ARROW_LEFT]: "\u2190",
12049
12051
  [ICON_TYPES.ARROW_RIGHT]: "\u2192",
12050
12052
  [ICON_TYPES.ARROW_UP]: "\u2191",
@@ -12669,7 +12671,7 @@ var SearchPanelRoot = styled6.section`
12669
12671
  ${({ $chatMode }) => $chatMode && `
12670
12672
  display: flex;
12671
12673
  flex-direction: column;
12672
- height: 805px;
12674
+ max-height: 805px;
12673
12675
  overflow: hidden;
12674
12676
  `}
12675
12677
  `;
@@ -13311,6 +13313,33 @@ var AIStatusCursor = styled6.span`
13311
13313
  }
13312
13314
  }
13313
13315
  `;
13316
+ var ChatScrollIndicator = styled6.button`
13317
+ align-self: center;
13318
+ appearance: none;
13319
+ background: ${colors6.hover};
13320
+ border: 0;
13321
+ border-radius: 50%;
13322
+ color: ${colors6.text};
13323
+ cursor: pointer;
13324
+ display: grid;
13325
+ place-items: center;
13326
+ block-size: 2rem;
13327
+ inline-size: 2rem;
13328
+ margin-block: 0.25rem 0.75rem;
13329
+ padding: 0;
13330
+ position: sticky;
13331
+ inset-block-end: 0.75rem;
13332
+ z-index: 1;
13333
+
13334
+ &:hover {
13335
+ background: ${colors6.borderLight};
13336
+ }
13337
+
13338
+ &:focus-visible {
13339
+ outline: 2px solid ${colors6.text};
13340
+ outline-offset: 2px;
13341
+ }
13342
+ `;
13314
13343
  var SuggestionSectionHeading = styled6.div`
13315
13344
  padding: 12px 24px 8px;
13316
13345
  margin-bottom: 4px;
@@ -13457,7 +13486,7 @@ styled6.span`
13457
13486
  font-size: 0.8125rem;
13458
13487
  font-weight: 600;
13459
13488
  `;
13460
- var ScrollIndicator = styled6.button`
13489
+ styled6.button`
13461
13490
  align-items: center;
13462
13491
  align-self: center;
13463
13492
  appearance: none;
@@ -13587,7 +13616,7 @@ function stripPricingText(text4) {
13587
13616
  );
13588
13617
  }
13589
13618
  function toCarouselItem(product, index2, showPricing, history) {
13590
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
13619
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
13591
13620
  const slotKey = `ai-${(_b = (_a = product.id) != null ? _a : product.sku) != null ? _b : index2}-${index2}`;
13592
13621
  const productName = (_f = (_d = (_c = product.name) == null ? void 0 : _c.trim()) != null ? _d : product.sku) != null ? _f : `Product ${(_e = product.id) != null ? _e : index2}`;
13593
13622
  if (!productName) {
@@ -13602,12 +13631,13 @@ function toCarouselItem(product, index2, showPricing, history) {
13602
13631
  };
13603
13632
  }
13604
13633
  const href = (_i = product.url) != null ? _i : "#";
13605
- const imageUrl = (_j = product.imageUrl) != null ? _j : product.image_url;
13606
- const priceText = (_k = product.price) != null ? _k : product.pv != null ? `${product.pv} PV` : void 0;
13634
+ const routeHref = (_j = product.routeUrl) != null ? _j : product.sku ? `/product/${product.sku}` : href;
13635
+ const imageUrl = (_k = product.imageUrl) != null ? _k : product.image_url;
13636
+ const priceText = (_l = product.price) != null ? _l : product.pv != null ? `${product.pv} PV` : void 0;
13607
13637
  return {
13608
13638
  status: "ready",
13609
13639
  slotKey,
13610
- productId: String((_m = (_l = product.id) != null ? _l : product.sku) != null ? _m : index2),
13640
+ productId: String((_n = (_m = product.id) != null ? _m : product.sku) != null ? _n : index2),
13611
13641
  cardProps: {
13612
13642
  mode: "cta",
13613
13643
  title: productName,
@@ -13617,10 +13647,11 @@ function toCarouselItem(product, index2, showPricing, history) {
13617
13647
  price: showPricing ? priceText : void 0,
13618
13648
  addToCartLabel: "View Product",
13619
13649
  addToCartAriaLabel: `View ${productName}`,
13620
- onProductClick: () => {
13650
+ onProductClick: ({ event } = {}) => {
13621
13651
  if (href === "#") return;
13652
+ event == null ? void 0 : event.preventDefault();
13622
13653
  if (history && typeof history.push === "function") {
13623
- history.push(href);
13654
+ history.push(routeHref);
13624
13655
  }
13625
13656
  }
13626
13657
  }
@@ -13633,14 +13664,6 @@ function SearchChatResult({
13633
13664
  productsAriaLabel = "AI recommended products"
13634
13665
  }) {
13635
13666
  var _a;
13636
- const carouselWrapperRef = useRef(null);
13637
- function scrollDown() {
13638
- var _a2;
13639
- (_a2 = carouselWrapperRef.current) == null ? void 0 : _a2.scrollIntoView({
13640
- behavior: "smooth",
13641
- block: "nearest"
13642
- });
13643
- }
13644
13667
  const products = (_a = result.products) != null ? _a : [];
13645
13668
  const carouselItems = products.map((p, i) => toCarouselItem(p, i, showPricing, history));
13646
13669
  const summaryText = showPricing ? result.aiResponseText : stripPricingText(result.aiResponseText);
@@ -13661,42 +13684,11 @@ function SearchChatResult({
13661
13684
  "data-testid": "chat-ai-response"
13662
13685
  }
13663
13686
  ),
13664
- carouselItems.length > 0 && /* @__PURE__ */ jsxs(ProductCarouselWrapper, { ref: carouselWrapperRef, "data-testid": "chat-product-carousel", children: [
13665
- /* @__PURE__ */ jsx(ProductCarousel, { items: carouselItems, ariaLabel: productsAriaLabel }),
13666
- /* @__PURE__ */ jsx(
13667
- ScrollIndicator,
13668
- {
13669
- type: "button",
13670
- "aria-label": "Scroll to products",
13671
- onClick: scrollDown,
13672
- "data-testid": "chat-scroll-indicator",
13673
- children: /* @__PURE__ */ jsxs(
13674
- "svg",
13675
- {
13676
- xmlns: "http://www.w3.org/2000/svg",
13677
- viewBox: "0 0 24 24",
13678
- fill: "none",
13679
- stroke: "currentColor",
13680
- strokeWidth: 2,
13681
- strokeLinecap: "round",
13682
- strokeLinejoin: "round",
13683
- width: 16,
13684
- height: 16,
13685
- "aria-hidden": true,
13686
- children: [
13687
- /* @__PURE__ */ jsx("line", { x1: 12, y1: 5, x2: 12, y2: 19 }),
13688
- /* @__PURE__ */ jsx("polyline", { points: "19 12 12 19 5 12" })
13689
- ]
13690
- }
13691
- )
13692
- }
13693
- )
13694
- ] })
13687
+ carouselItems.length > 0 && /* @__PURE__ */ jsx(ProductCarouselWrapper, { "data-testid": "chat-product-carousel", children: /* @__PURE__ */ jsx(ProductCarousel, { items: carouselItems, ariaLabel: productsAriaLabel }) })
13695
13688
  ] });
13696
13689
  }
13697
13690
 
13698
13691
  // src/components/SearchModal/SearchModalConstants.ts
13699
- var DEFAULT_AI_AUTO_TRIGGER_DELAY_MS = 4e3;
13700
13692
  var DEFAULT_AI_STATUS_LABELS = {
13701
13693
  analyzing: "Analyzing your question...",
13702
13694
  pulling: "Pulling up your analytics...",
@@ -13743,9 +13735,6 @@ function getUniqueRecentSearches(entries) {
13743
13735
  ) === idx
13744
13736
  );
13745
13737
  }
13746
- function getAITriggerDelayMs(delayMs) {
13747
- return typeof delayMs === "number" && delayMs >= 0 ? delayMs : DEFAULT_AI_AUTO_TRIGGER_DELAY_MS;
13748
- }
13749
13738
  function getProductId(rawId) {
13750
13739
  return typeof rawId === "string" || typeof rawId === "number" ? String(rawId) : JSON.stringify(rawId);
13751
13740
  }
@@ -13770,6 +13759,11 @@ function SearchModalChatContent({
13770
13759
  analyzingLabel,
13771
13760
  chatEndRef
13772
13761
  }) {
13762
+ const hasChatResults = aiChatHistory.length > 0;
13763
+ function scrollToBottom() {
13764
+ var _a;
13765
+ (_a = chatEndRef.current) == null ? void 0 : _a.scrollIntoView({ behavior: "smooth", block: "end" });
13766
+ }
13773
13767
  return /* @__PURE__ */ jsxs(Fragment, { children: [
13774
13768
  aiChatHistory.map((result, idx) => /* @__PURE__ */ jsx(
13775
13769
  "div",
@@ -13795,6 +13789,23 @@ function SearchModalChatContent({
13795
13789
  typedStatus || analyzingLabel,
13796
13790
  /* @__PURE__ */ jsx(AIStatusCursor, { "aria-hidden": "true" })
13797
13791
  ] }),
13792
+ hasChatResults && /* @__PURE__ */ jsx(
13793
+ ChatScrollIndicator,
13794
+ {
13795
+ type: "button",
13796
+ "aria-label": "Scroll to bottom",
13797
+ onClick: scrollToBottom,
13798
+ "data-testid": "chat-scroll-indicator",
13799
+ children: /* @__PURE__ */ jsx(
13800
+ Icon,
13801
+ {
13802
+ cursor: ICON_CURSORS.POINTER,
13803
+ size: ICON_SIZES.SMALL,
13804
+ type: ICON_TYPES.ARROW_DOWN
13805
+ }
13806
+ )
13807
+ }
13808
+ ),
13798
13809
  /* @__PURE__ */ jsx("div", { ref: chatEndRef })
13799
13810
  ] });
13800
13811
  }
@@ -13943,10 +13954,11 @@ function SearchModal({
13943
13954
  fallbackImageUrl = "",
13944
13955
  enableAIChat = false,
13945
13956
  aiChatConfig,
13957
+ gtmContext,
13946
13958
  showPricing = true,
13947
13959
  aiChatHeaderContent
13948
13960
  }) {
13949
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
13961
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
13950
13962
  const listboxId = useId();
13951
13963
  const footerRef = useRef(null);
13952
13964
  const suggestionRowRefs = useRef({});
@@ -13975,7 +13987,6 @@ function SearchModal({
13975
13987
  const [aiChatHistory, setAiChatHistory] = useState([]);
13976
13988
  const [pendingQuery, setPendingQuery] = useState("");
13977
13989
  const [aiLoading, setAiLoading] = useState(false);
13978
- const [isWaitingForAIFallback, setIsWaitingForAIFallback] = useState(false);
13979
13990
  const isInChatMode = aiChatHistory.length > 0 || pendingQuery !== "" || aiLoading;
13980
13991
  const [aiStatus, setAiStatus] = useState("");
13981
13992
  const [typedStatus, setTypedStatus] = useState("");
@@ -13987,11 +13998,34 @@ function SearchModal({
13987
13998
  aiChatConfigRef.current = aiChatConfig;
13988
13999
  const aiChatHeaderContentRef = useRef(aiChatHeaderContent);
13989
14000
  aiChatHeaderContentRef.current = aiChatHeaderContent;
14001
+ const gtmContextRef = useRef(gtmContext);
14002
+ gtmContextRef.current = gtmContext;
13990
14003
  const historyEntriesRef = useRef(historyEntries);
13991
14004
  historyEntriesRef.current = historyEntries;
13992
14005
  const chatEndRef = useRef(null);
13993
- const aiFallbackTimeoutRef = useRef(null);
13994
14006
  const aiRequestCounterRef = useRef(0);
14007
+ const [suggestionsRequestFailed, setSuggestionsRequestFailed] = useState(false);
14008
+ const suggestionAwareService = useMemo(
14009
+ () => ({
14010
+ ...service,
14011
+ async fetchSuggestions(searchTerm) {
14012
+ setSuggestionsRequestFailed(false);
14013
+ try {
14014
+ const response = await service.fetchSuggestions(searchTerm);
14015
+ setSuggestionsRequestFailed(false);
14016
+ return response;
14017
+ } catch (error) {
14018
+ setSuggestionsRequestFailed(true);
14019
+ throw error;
14020
+ }
14021
+ }
14022
+ }),
14023
+ [service]
14024
+ );
14025
+ const searchSuggestions = useSearchSuggestions({
14026
+ service: suggestionAwareService,
14027
+ enabled: isOpen && !isInChatMode
14028
+ });
13995
14029
  const {
13996
14030
  didYouMean,
13997
14031
  emptyFetched,
@@ -14001,12 +14035,16 @@ function SearchModal({
14001
14035
  query,
14002
14036
  suggestions,
14003
14037
  updateQuery
14004
- } = useSearchSuggestions({ service, enabled: isOpen && !isInChatMode });
14038
+ } = searchSuggestions;
14039
+ const suggestionFetchFailed = ((_a = searchSuggestions.suggestionFetchFailed) != null ? _a : false) || suggestionsRequestFailed;
14005
14040
  const [searchInputValue, setSearchInputValue] = useState(query);
14006
14041
  const searchInputValueRef = useRef(query);
14007
14042
  const voiceInputBaseValueRef = useRef(query);
14008
14043
  const setSearchInput = useCallback(
14009
- (value, updateVoiceBase = true) => {
14044
+ (value, updateVoiceBase = true, resetSuggestionFailure = true) => {
14045
+ if (resetSuggestionFailure) {
14046
+ setSuggestionsRequestFailed(false);
14047
+ }
14010
14048
  searchInputValueRef.current = value;
14011
14049
  if (updateVoiceBase) {
14012
14050
  voiceInputBaseValueRef.current = value;
@@ -14048,20 +14086,14 @@ function SearchModal({
14048
14086
  const hasDidYouMean = didYouMean.length > 0;
14049
14087
  const showDidYouMean = hasDidYouMean && !enableAIChat;
14050
14088
  const visibleSuggestions = enableAIChat ? suggestions : getVisibleSuggestions({ didYouMean, suggestions });
14051
- const visibleSuggestionsRef = useRef(visibleSuggestions);
14052
- visibleSuggestionsRef.current = visibleSuggestions;
14053
- const hasDidYouMeanRef = useRef(hasDidYouMean);
14054
- hasDidYouMeanRef.current = hasDidYouMean;
14055
- const loadingRef = useRef(loading);
14056
- loadingRef.current = loading;
14057
14089
  const safeUseVoiceSearch = useVoiceSearch;
14058
14090
  const voiceSearchRaw = safeUseVoiceSearch({
14059
14091
  onInterimResult: previewVoiceResult,
14060
14092
  onResult: appendVoiceResult
14061
14093
  });
14062
14094
  const voiceSearch = isVoiceSearchState(voiceSearchRaw) ? voiceSearchRaw : null;
14063
- const voiceListening = (_a = voiceSearch == null ? void 0 : voiceSearch.listening) != null ? _a : false;
14064
- const voiceSupported = (_b = voiceSearch == null ? void 0 : voiceSearch.supported) != null ? _b : false;
14095
+ const voiceListening = (_b = voiceSearch == null ? void 0 : voiceSearch.listening) != null ? _b : false;
14096
+ const voiceSupported = (_c = voiceSearch == null ? void 0 : voiceSearch.supported) != null ? _c : false;
14065
14097
  const startVoice = voiceSearch == null ? void 0 : voiceSearch.start;
14066
14098
  const stopVoice = voiceSearch == null ? void 0 : voiceSearch.stop;
14067
14099
  const toggleVoice = useCallback(() => {
@@ -14072,10 +14104,9 @@ function SearchModal({
14072
14104
  voiceInputBaseValueRef.current = searchInputValueRef.current.trimEnd();
14073
14105
  startVoice == null ? void 0 : startVoice();
14074
14106
  }, [startVoice, stopVoice, voiceListening]);
14075
- const showPricingFromHeader = ((_d = (_c = aiChatHeaderContentRef.current) == null ? void 0 : _c.configurations) == null ? void 0 : _d.enablepricingpvsv) !== "false";
14107
+ const showPricingFromHeader = ((_e = (_d = aiChatHeaderContentRef.current) == null ? void 0 : _d.configurations) == null ? void 0 : _e.enablepricingpvsv) !== "false";
14076
14108
  const resolvedShowPricing = aiChatHeaderContent ? showPricingFromHeader : showPricing;
14077
14109
  const aiStatusLabels = getAIStatusLabels(copy);
14078
- const aiAutoTriggerDelayMs = getAITriggerDelayMs(aiChatConfig == null ? void 0 : aiChatConfig.aiAutoTriggerDelayMs);
14079
14110
  function voiceSearchProps() {
14080
14111
  var _a2;
14081
14112
  if (!voiceSupported || !startVoice) return {};
@@ -14087,12 +14118,22 @@ function SearchModal({
14087
14118
  onVoiceInput: toggleVoice
14088
14119
  };
14089
14120
  }
14121
+ const buildGtmContext = useCallback(() => {
14122
+ var _a2;
14123
+ return (_a2 = gtmContextRef.current) != null ? _a2 : {};
14124
+ }, []);
14125
+ const chatEntryFiredRef = useRef(false);
14090
14126
  useEffect(() => {
14091
- if (isOpen) return;
14092
- if (aiFallbackTimeoutRef.current) {
14093
- clearTimeout(aiFallbackTimeoutRef.current);
14094
- aiFallbackTimeoutRef.current = null;
14127
+ if (!isOpen) {
14128
+ chatEntryFiredRef.current = false;
14129
+ return;
14095
14130
  }
14131
+ if (!enableAIChat || chatEntryFiredRef.current) return;
14132
+ chatEntryFiredRef.current = true;
14133
+ trackChatEntryPoint(buildGtmContext());
14134
+ }, [isOpen, enableAIChat, buildGtmContext]);
14135
+ useEffect(() => {
14136
+ if (isOpen) return;
14096
14137
  updateQuery("");
14097
14138
  setAiChatHistory([]);
14098
14139
  setPendingQuery("");
@@ -14149,46 +14190,21 @@ function SearchModal({
14149
14190
  document.body.style.overflow = "";
14150
14191
  };
14151
14192
  }, [isOpen, onClose]);
14152
- useEffect(() => {
14153
- if (aiFallbackTimeoutRef.current) {
14154
- clearTimeout(aiFallbackTimeoutRef.current);
14155
- aiFallbackTimeoutRef.current = null;
14156
- }
14157
- setIsWaitingForAIFallback(false);
14158
- if (!enableAIChat || !isOpen || isInChatMode || !hasQuery) return void 0;
14159
- const trimmedQuery = query.trim();
14160
- if (!trimmedQuery) return void 0;
14161
- const alreadyAnswered = historyEntriesRef.current.some(
14162
- (e) => e.isAI && e.query.toLowerCase() === trimmedQuery.toLowerCase()
14163
- );
14164
- if (alreadyAnswered) return void 0;
14165
- setIsWaitingForAIFallback(true);
14166
- aiFallbackTimeoutRef.current = setTimeout(() => {
14167
- aiFallbackTimeoutRef.current = null;
14168
- setIsWaitingForAIFallback(false);
14169
- if (!loadingRef.current && visibleSuggestionsRef.current.length > 0 && !hasDidYouMeanRef.current) {
14170
- return;
14171
- }
14172
- sendAIMessage(trimmedQuery);
14173
- }, aiAutoTriggerDelayMs);
14174
- return () => {
14175
- if (aiFallbackTimeoutRef.current) {
14176
- clearTimeout(aiFallbackTimeoutRef.current);
14177
- aiFallbackTimeoutRef.current = null;
14178
- }
14179
- setIsWaitingForAIFallback(false);
14180
- };
14181
- }, [enableAIChat, isOpen, isInChatMode, hasQuery, query]);
14182
14193
  const sendAIMessage = useCallback(
14183
14194
  (queryText) => {
14184
- var _a2;
14185
14195
  const config = aiChatConfigRef.current;
14186
14196
  if (!config || !queryText.trim()) return;
14187
- setSearchInput("");
14197
+ setSearchInput("", true, false);
14188
14198
  setPendingQuery(queryText.trim());
14189
14199
  setAiStatus(aiStatusLabels.analyzing);
14200
+ const trimmedQuery = queryText.trim();
14201
+ trackSearchType({
14202
+ ...buildGtmContext(),
14203
+ searchType: SEARCH_GTM_SEARCH_TYPES.AI_AGENT,
14204
+ searchResponse: trimmedQuery
14205
+ });
14190
14206
  const requestId = aiRequestCounterRef.current;
14191
- const basePath = (_a2 = config.productBasePath) != null ? _a2 : "/product";
14207
+ const productBasePath = `/${config.market.toLowerCase()}/${config.language.toLowerCase()}/product`;
14192
14208
  const chatService = createChatService({
14193
14209
  conversationsUrl: config.conversationsUrl,
14194
14210
  messagesUrl: config.messagesUrl,
@@ -14214,8 +14230,8 @@ function SearchModal({
14214
14230
  ).then((result) => {
14215
14231
  if (!result || requestId !== aiRequestCounterRef.current) return;
14216
14232
  const products = result.products.map((p, i) => {
14217
- var _a3, _b2;
14218
- const productId = getProductId((_b2 = (_a3 = p.id) != null ? _a3 : p.sku) != null ? _b2 : i);
14233
+ var _a2, _b2;
14234
+ const productId = getProductId((_b2 = (_a2 = p.id) != null ? _a2 : p.sku) != null ? _b2 : i);
14219
14235
  const productName = getProductName(p, productId);
14220
14236
  return {
14221
14237
  id: productId,
@@ -14226,7 +14242,10 @@ function SearchModal({
14226
14242
  ...p.retail_price != null ? { retail_price: p.retail_price } : {},
14227
14243
  ...p.description ? { description: p.description } : {},
14228
14244
  ...p.badge ? { badge: p.badge } : {},
14229
- ...p.sku != null ? { url: `${basePath}/${p.sku}` } : {}
14245
+ ...p.sku != null ? {
14246
+ url: `${productBasePath}/${p.sku}`,
14247
+ routeUrl: `/product/${p.sku}`
14248
+ } : {}
14230
14249
  };
14231
14250
  });
14232
14251
  setAiStatus("");
@@ -14239,14 +14258,24 @@ function SearchModal({
14239
14258
  const priorHistory = historyEntriesRef.current.filter((e) => e.isAI && e.aiResult != null).slice().reverse().map((e) => e.aiResult);
14240
14259
  setAiChatHistory([...priorHistory, newResult]);
14241
14260
  historyAddEntry({ query: queryText.trim(), isAI: true, aiResult: newResult });
14261
+ trackProductSearch({
14262
+ ...buildGtmContext(),
14263
+ searchType: SEARCH_GTM_SEARCH_TYPES.AI_AGENT,
14264
+ productResults: products.length,
14265
+ productResultsData: products
14266
+ });
14242
14267
  }).catch(() => {
14243
14268
  setPendingQuery("");
14244
14269
  setAiStatus("");
14270
+ trackErrorPopup({
14271
+ ...buildGtmContext(),
14272
+ searchType: SEARCH_GTM_SEARCH_TYPES.AI_AGENT
14273
+ });
14245
14274
  }).finally(() => {
14246
14275
  if (requestId === aiRequestCounterRef.current) setAiLoading(false);
14247
14276
  });
14248
14277
  },
14249
- [aiStatusLabels, historyAddEntry, setSearchInput]
14278
+ [aiStatusLabels, historyAddEntry, setSearchInput, buildGtmContext]
14250
14279
  );
14251
14280
  const selectOption = useCallback(
14252
14281
  (option) => {
@@ -14254,9 +14283,16 @@ function SearchModal({
14254
14283
  if (isSuggestionDisabled(option)) return;
14255
14284
  const term = String((_b2 = (_a2 = option["value"]) != null ? _a2 : option["label"]) != null ? _b2 : "");
14256
14285
  historyAddEntry({ query: term, isAI: false });
14286
+ if (term.trim()) {
14287
+ trackProductSearch({
14288
+ ...buildGtmContext(),
14289
+ searchType: SEARCH_GTM_SEARCH_TYPES.SEMANTIC,
14290
+ productResultsData: [option]
14291
+ });
14292
+ }
14257
14293
  const optionHref = service.getSuggestionHref(option);
14258
- if (optionHref && history && typeof history.push === "function") {
14259
- history.push(optionHref);
14294
+ if (optionHref) {
14295
+ pushSearchPath(history, optionHref);
14260
14296
  onClose();
14261
14297
  return;
14262
14298
  }
@@ -14267,7 +14303,7 @@ function SearchModal({
14267
14303
  if (!enableAIChat) setLegacyRecentSearches((_d2 = (_c2 = service.getRecentSearches) == null ? void 0 : _c2.call(service)) != null ? _d2 : []);
14268
14304
  onClose();
14269
14305
  },
14270
- [enableAIChat, service, onClose, historyAddEntry, history]
14306
+ [buildGtmContext, enableAIChat, service, onClose, historyAddEntry, history]
14271
14307
  );
14272
14308
  const runSuggestionAction = useCallback(
14273
14309
  (option) => {
@@ -14287,6 +14323,13 @@ function SearchModal({
14287
14323
  setHighlightedQuery(entry.query);
14288
14324
  } else {
14289
14325
  historyAddEntry({ query: entry.query, isAI: false });
14326
+ if (entry.query.trim()) {
14327
+ trackProductSearch({
14328
+ ...buildGtmContext(),
14329
+ searchType: SEARCH_GTM_SEARCH_TYPES.SEMANTIC,
14330
+ productResultsData: { query: entry.query.trim() }
14331
+ });
14332
+ }
14290
14333
  const url = service.submitProductSearch(entry.query);
14291
14334
  if (url && history && typeof history.push === "function") {
14292
14335
  history.push(url);
@@ -14295,7 +14338,7 @@ function SearchModal({
14295
14338
  onClose();
14296
14339
  }
14297
14340
  },
14298
- [enableAIChat, service, onClose, historyAddEntry, history]
14341
+ [buildGtmContext, enableAIChat, service, onClose, historyAddEntry, history]
14299
14342
  );
14300
14343
  const handleClearRecentSearches = useCallback(() => {
14301
14344
  clearRecentSearches();
@@ -14308,22 +14351,44 @@ function SearchModal({
14308
14351
  const handleSearchSubmit = useCallback(
14309
14352
  (value) => {
14310
14353
  var _a2, _b2;
14354
+ if (enableAIChat && suggestionFetchFailed) {
14355
+ sendAIMessage(value);
14356
+ return;
14357
+ }
14311
14358
  historyAddEntry({ query: value, isAI: false });
14359
+ const trimmed = value.trim();
14360
+ if (trimmed) {
14361
+ trackProductSearch({
14362
+ ...buildGtmContext(),
14363
+ searchType: SEARCH_GTM_SEARCH_TYPES.SEMANTIC,
14364
+ productResultsData: visibleSuggestions.length > 0 ? visibleSuggestions : { query: trimmed }
14365
+ });
14366
+ }
14312
14367
  const url = service.submitProductSearch(value);
14313
14368
  pushSearchPath(history, url);
14314
14369
  if (!enableAIChat) setLegacyRecentSearches((_b2 = (_a2 = service.getRecentSearches) == null ? void 0 : _a2.call(service)) != null ? _b2 : []);
14315
14370
  onClose();
14316
14371
  },
14317
- [enableAIChat, history, historyAddEntry, onClose, service]
14372
+ [
14373
+ buildGtmContext,
14374
+ enableAIChat,
14375
+ history,
14376
+ historyAddEntry,
14377
+ onClose,
14378
+ sendAIMessage,
14379
+ service,
14380
+ suggestionFetchFailed,
14381
+ visibleSuggestions
14382
+ ]
14318
14383
  );
14319
14384
  if (!isOpen) return null;
14320
- const placeholder = (_e = copy == null ? void 0 : copy.extended_search_textbox_desktop) != null ? _e : "Search";
14321
- const ariaLabel = (_f = copy == null ? void 0 : copy.extended_search_box_aria_label) != null ? _f : placeholder;
14322
- const didYouMeanText = (_g = copy == null ? void 0 : copy.did_you_mean_text) != null ? _g : "Did You Mean?";
14323
- const noSuggestionsText = (_h = copy == null ? void 0 : copy.no_suggestions_text) != null ? _h : "No suggestions";
14324
- const seeMoreText = (_i = copy == null ? void 0 : copy.see_more_results_text) != null ? _i : "See More Results";
14385
+ const placeholder = (_f = copy == null ? void 0 : copy.extended_search_textbox_desktop) != null ? _f : "Search";
14386
+ const ariaLabel = (_g = copy == null ? void 0 : copy.extended_search_box_aria_label) != null ? _g : placeholder;
14387
+ const didYouMeanText = (_h = copy == null ? void 0 : copy.did_you_mean_text) != null ? _h : "Did You Mean?";
14388
+ const noSuggestionsText = (_i = copy == null ? void 0 : copy.no_suggestions_text) != null ? _i : "No suggestions";
14389
+ const seeMoreText = (_j = copy == null ? void 0 : copy.see_more_results_text) != null ? _j : "See More Results";
14325
14390
  const showRecentSearches = !hasQuery && !aiLoading && !isInChatMode && recentSearches.length > 0;
14326
- const showNoResults = !isInChatMode && !loading && emptyFetched && visibleSuggestions.length === 0 && !aiLoading && !(enableAIChat && isWaitingForAIFallback);
14391
+ const showNoResults = !isInChatMode && !loading && emptyFetched && visibleSuggestions.length === 0 && !aiLoading;
14327
14392
  const searchInput = /* @__PURE__ */ jsx(
14328
14393
  SearchMessageInput,
14329
14394
  {
@@ -14409,7 +14474,7 @@ function SearchModal({
14409
14474
  query,
14410
14475
  showRecentSearches,
14411
14476
  handleClearRecentSearches,
14412
- clearAllLabel: (_j = copy == null ? void 0 : copy.clear_all_label) != null ? _j : "Clear all",
14477
+ clearAllLabel: (_k = copy == null ? void 0 : copy.clear_all_label) != null ? _k : "Clear all",
14413
14478
  showAllRecent,
14414
14479
  recentSearches,
14415
14480
  handleRecentSearchSelect,