@nosto/search-js 2.12.1 → 2.13.0

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.
@@ -1,5 +1,5 @@
1
1
  import { u as f } from "./jsxRuntime.module-Bzuv3cXw.js";
2
- import { a, b as l } from "./useLoadMore-jrN1VGDU.js";
2
+ import { a, b as l } from "./useLoadMore-CzqfcD1v.js";
3
3
  import { p as d } from "./pick-DReBictn.js";
4
4
  import { useRef as m, useEffect as b } from "preact/hooks";
5
5
  import { Fragment as v } from "preact";
@@ -1 +1 @@
1
- "use strict";const l=require("./jsxRuntime.module-B3sGoTIU.cjs"),p=require("./useLoadMore-BFEnHdOH.cjs"),a=require("./pick-rYi1lc2m.cjs"),d=require("preact/hooks"),b=require("preact");function f(s,i){var o,e,r;const t=((o=i.products)==null?void 0:o.total)??0,c=((e=s.products)==null?void 0:e.from)??0,n=((r=s.products)==null?void 0:r.size)??0;return t>0&&t>c+n}function h(){return!!(window.IntersectionObserver&&window.IntersectionObserverEntry&&"intersectionRatio"in window.IntersectionObserverEntry.prototype)}function O({children:s,pageSize:i}){const t=d.useRef(null),{query:c,response:n}=p.useNostoAppState(e=>a.pick(e,"query","response")),{loadMore:o}=p.useLoadMore(i);return d.useEffect(()=>{let e,r;return f(c,n)&&(e=t.current,r=new IntersectionObserver(v=>{const u=v[0];u!=null&&u.isIntersecting&&o()}),e&&r.observe(e)),()=>{e&&r.unobserve(e)}},[n]),l.u(b.Fragment,{children:[s,l.u("div",{ref:t,style:{height:"1px"}})]})}exports.InfiniteScrollWithObserver=O;exports.hasMoreResults=f;exports.intersectionObserverSupported=h;
1
+ "use strict";const l=require("./jsxRuntime.module-B3sGoTIU.cjs"),p=require("./useLoadMore-BJWBvbZ1.cjs"),a=require("./pick-rYi1lc2m.cjs"),d=require("preact/hooks"),b=require("preact");function f(s,i){var o,e,r;const t=((o=i.products)==null?void 0:o.total)??0,c=((e=s.products)==null?void 0:e.from)??0,n=((r=s.products)==null?void 0:r.size)??0;return t>0&&t>c+n}function h(){return!!(window.IntersectionObserver&&window.IntersectionObserverEntry&&"intersectionRatio"in window.IntersectionObserverEntry.prototype)}function O({children:s,pageSize:i}){const t=d.useRef(null),{query:c,response:n}=p.useNostoAppState(e=>a.pick(e,"query","response")),{loadMore:o}=p.useLoadMore(i);return d.useEffect(()=>{let e,r;return f(c,n)&&(e=t.current,r=new IntersectionObserver(v=>{const u=v[0];u!=null&&u.isIntersecting&&o()}),e&&r.observe(e)),()=>{e&&r.unobserve(e)}},[n]),l.u(b.Fragment,{children:[s,l.u("div",{ref:t,style:{height:"1px"}})]})}exports.InfiniteScrollWithObserver=O;exports.hasMoreResults=f;exports.intersectionObserverSupported=h;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("../index.es-DlUp67LT.cjs"),o=require("../search-DJuT47P8.cjs");async function d(r,a,t=1){await e.L(a,void 0,t),await e.s(async c=>await c.recordSearchAddToCart(r,a))}exports.search=o.search;exports.addToCart=d;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("../index.es-DlUp67LT.cjs"),o=require("../search-DapMuqyR.cjs");async function d(r,a,t=1){await e.L(a,void 0,t),await e.s(async c=>await c.recordSearchAddToCart(r,a))}exports.search=o.search;exports.addToCart=d;
@@ -1,5 +1,5 @@
1
1
  import { L as c, s as d } from "../index.es-B8mbAxS4.js";
2
- import { s as f } from "../search-CIiuO9FB.js";
2
+ import { s as f } from "../search-PYPD19kM.js";
3
3
  async function e(r, a, o = 1) {
4
4
  await c(a, void 0, o), await d(async (t) => await t.recordSearchAddToCart(r, a));
5
5
  }
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("../unique-Blp-K9zG.cjs"),t=require("../jsxRuntime.module-B3sGoTIU.cjs"),c=require("../useLoadMore-BFEnHdOH.cjs"),p=require("../pick-rYi1lc2m.cjs"),n=require("../InfiniteScrollWithObserver-DfcZhVtT.cjs"),q=require("preact"),r=require("../configContext-BR7w4VlU.cjs");function b({pageSize:o}){const{loadMore:e}=c.useLoadMore(o);return t.u("button",{onClick:e,children:"More results"})}function u({children:o,loadMoreComponent:e,pageSize:i}){const{loading:s,query:l,response:a}=c.useNostoAppState(S=>p.pick(S,"loading","query","response")),d=!s&&n.hasMoreResults(l,a);return t.u(q.Fragment,{children:[o,d&&(e?t.u(e,{pageSize:i}):t.u(b,{pageSize:i}))]})}const M=!f.isBot()&&n.intersectionObserverSupported()?n.InfiniteScrollWithObserver:u;exports.StoreContext=r.StoreContext;exports.createExtendableStore=r.createExtendableStore;exports.createStore=r.createStore;exports.defaultState=r.defaultState;exports.InfiniteScroll=M;exports.InfiniteScrollWithLink=u;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("../unique-Blp-K9zG.cjs"),t=require("../jsxRuntime.module-B3sGoTIU.cjs"),c=require("../useLoadMore-BJWBvbZ1.cjs"),p=require("../pick-rYi1lc2m.cjs"),n=require("../InfiniteScrollWithObserver-Do05gtr2.cjs"),q=require("preact"),r=require("../configContext-BR7w4VlU.cjs");function b({pageSize:o}){const{loadMore:e}=c.useLoadMore(o);return t.u("button",{onClick:e,children:"More results"})}function u({children:o,loadMoreComponent:e,pageSize:i}){const{loading:s,query:l,response:a}=c.useNostoAppState(S=>p.pick(S,"loading","query","response")),d=!s&&n.hasMoreResults(l,a);return t.u(q.Fragment,{children:[o,d&&(e?t.u(e,{pageSize:i}):t.u(b,{pageSize:i}))]})}const M=!f.isBot()&&n.intersectionObserverSupported()?n.InfiniteScrollWithObserver:u;exports.StoreContext=r.StoreContext;exports.createExtendableStore=r.createExtendableStore;exports.createStore=r.createStore;exports.defaultState=r.defaultState;exports.InfiniteScroll=M;exports.InfiniteScrollWithLink=u;
@@ -1,8 +1,8 @@
1
1
  import { i as l } from "../unique-m1TIDWdl.js";
2
2
  import { u as r } from "../jsxRuntime.module-Bzuv3cXw.js";
3
- import { b as p, a as m } from "../useLoadMore-jrN1VGDU.js";
3
+ import { b as p, a as m } from "../useLoadMore-CzqfcD1v.js";
4
4
  import { p as u } from "../pick-DReBictn.js";
5
- import { h as d, i as f, I as S } from "../InfiniteScrollWithObserver-D_nTat6t.js";
5
+ import { h as d, i as f, I as S } from "../InfiniteScrollWithObserver-CvnCyquA.js";
6
6
  import { Fragment as h } from "preact";
7
7
  import { S as O, a as W, c as A, d as B } from "../configContext-GuZOIX3o.js";
8
8
  function M({ pageSize: e }) {
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("../useLoadMore-BFEnHdOH.cjs"),z=require("../logger-BcHMZObS.cjs"),p=require("preact/hooks"),P=require("../index.es-DlUp67LT.cjs"),N=require("../parseNumber-FsZ8w61u.cjs");function T(e){const r=S.useNostoAppState(s=>s.response);return e&&Array.isArray(e)||e&&z.isPlainObject(e)?e:r}function q(e,r){var d;const s=((d=e.data)==null?void 0:d.filter(t=>t.selected).length)??0,{active:o}={active:s>0,...r},[n,l]=p.useState(o),{toggleProductFilter:c}=S.useActions(),u=p.useCallback(()=>{l(!n)},[n]);return{active:n,selectedFiltersCount:s,toggleActive:u,toggleProductFilter:c}}function L(){const{loading:e,facets:r}=S.useNostoAppState(s=>{var o,n;return{loading:s.loading,facets:((n=(o=s.response)==null?void 0:o.products)==null?void 0:n.facets)??[]}});return{loading:e,facets:r}}function O(e,r){const s=r-e;return!isNaN(s)&&s>0?new Array(r-e).fill(void 0).map((o,n)=>n+e):[]}function j(e){const{query:r,products:s}=S.useNostoAppState(o=>({query:o.query,products:o.response.products}));return p.useMemo(()=>{var F,y,R;if(!s)return{totalPages:0,resultsFrom:0,resultsTo:0,pages:[]};const o=((F=r.products)==null?void 0:F.from)??0,n=(e==null?void 0:e.width)??1/0,l=Math.max(Math.floor(n-1)/2,1),c=s.size>0?Math.floor(o/s.size)+1:1,u=s.size>0?Math.ceil(s.total/s.size):0,d=A=>A>=c-l&&A<=c+l,t=o+1,a=Math.min(o+s.total,s.total),i={from:o,page:c,current:!0},f=A=>({from:(A-1)*s.size,page:A,current:A===c}),h=c>1?f(c-1):void 0,g=c<u?f(c+1):void 0,b=c-l-1>1?f(1):void 0,m=c+l+1<u?f(u):void 0,v=O(1,u+1).filter(d).map(f);return!b&&((y=v[0])==null?void 0:y.page)===2&&v.unshift(f(1)),!m&&((R=v[v.length-1])==null?void 0:R.page)===u-1&&v.push(f(u)),{totalPages:u,resultsFrom:t,resultsTo:a,current:i,prev:h,next:g,first:b,last:m,pages:v}},[r,s,e==null?void 0:e.width])}function V(){const[e,r]=p.useState([]),[s,o]=p.useState([]);return p.useEffect(()=>{P.s(async n=>{const{products:l,segments:c}=await n.getSearchSessionParams();r(c??[]),o((l==null?void 0:l.personalizationBoost)??[])})},[]),{segments:e,boost:s}}function B(){const{facets:e}=S.useNostoAppState(t=>{var a;return{facets:((a=t.response.products)==null?void 0:a.facets)??[]}}),{replaceFilter:r,toggleProductFilter:s}=S.useActions(),o=p.useCallback(t=>{const a=e==null?void 0:e.find(i=>i.type==="stats"&&i.field===t);if(a&&"min"in a&&"max"in a)return a},[e]),n=p.useCallback(t=>{var a;return((a=e==null?void 0:e.find(i=>i.field===t))==null?void 0:a.name)??t},[e]),l=p.useCallback(t=>"field"in t&&(t.value instanceof Array||t.range instanceof Array),[]),c=p.useCallback(t=>{var a;return{...t,range:(a=t.range)==null?void 0:a.map(i=>({gt:i.gt?Number(i.gt):i.gt,gte:i.gte?Number(i.gte):i.gte,lt:i.lt?Number(i.lt):i.lt,lte:i.lte?Number(i.lte):i.lte}))}},[]),u=p.useCallback(t=>(t.value??[]).map(i=>({value:i,field:t.field,name:n(t.field),filter:c(t),remove:()=>{s(t.field,i,!1)}})),[c,n,s]),d=p.useCallback(t=>(t.range??[]).map(i=>{var g,b;const f=i.gte??i.gt??((g=o(t.field))==null?void 0:g.min),h=i.lte??i.lt??((b=o(t.field))==null?void 0:b.max);if(f!==void 0&&h!==void 0)return{value:`${f} - ${h}`,field:t.field,name:n(t.field),filter:c(t),remove:()=>{r(t.field,void 0)}}}).filter(Boolean),[c,o,n,r]);return{selectFilters:l,toValueFilter:u,toRangeFilter:d}}function C(){const{filter:e}=S.useNostoAppState(u=>{var d;return{filter:((d=u.query.products)==null?void 0:d.filter)??[]}}),{updateSearch:r}=S.useActions(),{selectFilters:s,toValueFilter:o,toRangeFilter:n}=B(),l=p.useMemo(()=>e?e.filter(s).flatMap(u=>"value"in u?o(u):"range"in u?n(u):[]).filter(Boolean):[],[e,s,n,o]),c=p.useCallback(()=>{r({products:{filter:[]}})},[r]);return{filters:l,removeAll:c}}function k(e){var i,f,h;const{replaceFilter:r}=S.useActions(),{query:s,products:o}=S.useNostoAppState(g=>({query:g.query,products:g.response.products})),n=(i=o==null?void 0:o.facets)==null?void 0:i.find(g=>g.id===e);if(!n)return{min:0,max:0,range:[0,0],updateRange:()=>{}};const l=(h=(f=s.products)==null?void 0:f.filter)==null?void 0:h.find(g=>g.field===n.field),c=l!=null&&l.range?l.range[0]:void 0,u=typeof c=="object"&&("gte"in c||"lte"in c)?[N.parseNumber(c.gte),N.parseNumber(c.lte)]:[void 0,void 0],d="min"in n?Math.floor(n.min):0,t="max"in n?Math.ceil(n.max):0,a=([g,b])=>{const m=g!==void 0?Math.floor(g):void 0,v=b!==void 0?Math.ceil(b):void 0,F=m!==void 0,y=v!==void 0;(d===m||!F)&&(t===v||!y)?r(n.field,void 0):(d===m||!F)&&y?r(n.field,{lte:v.toString()}):(t===v||!y)&&F?r(n.field,{gte:m.toString()}):F&&y&&r(n.field,{gte:m.toString(),lte:v.toString()})};return{min:d,max:t,range:[u[0]??d,u[1]??t],updateRange:a}}function D(e,r){const{min:s,max:o,range:n,updateRange:l}=k(e),{filters:c}=C(),u=p.useMemo(()=>{var b;const i=c.find(m=>{var v;return(v=m==null?void 0:m.filter)==null?void 0:v.range});let f=null;if(i){const m=(b=i.filter.range)==null?void 0:b[0];f=[N.parseNumber(m==null?void 0:m.gte),N.parseNumber(m==null?void 0:m.lte)]}const h=[];let g=Math.floor(s/r)*r;for(;g<o;){const m=g+r,v=f&&f[0]===g&&f[1]===m;h.push({min:g,max:m,selected:v}),g=m}return h},[c,s,o,r]),d=i=>{l([i,n[1]])},t=i=>{l([n[0],i])},a=s!==n[0]||o!==n[1];return{min:s,max:o,range:n,updateRange:l,ranges:u,handleMinChange:d,handleMaxChange:t,isSelected:a}}function U(){const{products:e,keywords:r}=S.useNostoAppState(s=>s.response);return{products:e??{hits:[],total:0},keywords:r??{hits:[],total:0}}}function E(){const e=S.useNostoAppState(s=>{var o;return(o=s.query.products)==null?void 0:o.filter});return p.useMemo(()=>e?e.reduce((s,o)=>s+(Array.isArray(o.value)?o.value.length:1),0):0,[e])}function M(e){return e&&!Number.isNaN(e)?e:0}function _(e,r){const{from:s,size:o,total:n}=S.useNostoAppState(t=>{var a,i,f,h,g;return{from:M(((a=t.query.products)==null?void 0:a.from)??0),size:M(((f=(i=t.response)==null?void 0:i.products)==null?void 0:f.size)??0),total:M(((g=(h=t.response)==null?void 0:h.products)==null?void 0:g.total)??0)}}),{updateSearch:l}=S.useActions(),c=s+o,u=[...e].reverse().filter(t=>t+r<n);return{from:s,to:c,total:n,size:o,sizeOptions:u,handleSizeChange:t=>{l({products:{size:N.parseNumber(t)}})}}}function $(e,r){return e.length!==r.length?!1:e.every(s=>r.find(o=>s.field===o.field&&s.order===o.order))}function I(e){var l,c;const r=S.useNostoAppState(u=>u.query),{updateSearch:s}=S.useActions();return{activeSort:((l=e.find(u=>{var d;return $(u.value.sort,((d=r.products)==null?void 0:d.sort)||[])}))==null?void 0:l.id)??((c=e[0])==null?void 0:c.id),setSort:u=>{const d=e.find(t=>t.id===u);d&&s({products:{sort:d.value.sort}})}}}const w=window.SpeechRecognition||window.webkitSpeechRecognition,x=!!(w&&typeof w=="function");function G(){return{listening:!1,startListening:()=>{},stopListening:()=>{}}}function H({language:e="en-US",interimResults:r=!1}={}){const[s,o]=p.useState(!1),n=p.useRef(null),l=p.useCallback(({onResult:u,onError:d})=>{const t=new w;t.lang=e,t.interimResults=r,t.onstart=()=>o(!0),t.onresult=a=>{var f,h;const{transcript:i}=(h=(f=a.results)==null?void 0:f[0])==null?void 0:h[0];u(i)},d&&(t.onerror=a=>d(a.error)),t.onend=()=>o(!1),n.current=t,t.start()},[e,r]),c=p.useCallback(()=>{var u;(u=n.current)==null||u.stop()},[n]);return{listening:s,startListening:l,stopListening:c}}const J=x?H:G;function K(e,r){if(!e.length||!r.length)return[];const s=r.reduce((o,n)=>(o[n]={},o),{});return e.forEach(o=>{var n;(n=o.customFields)==null||n.forEach(({key:l,value:c})=>{const u=l.toLowerCase();r.includes(u)&&(s[u][c]=s[u][c]||[],s[u][c].push(o))})}),Object.entries(s).map(([o,n])=>({field:o,options:Object.entries(n).map(([l,c])=>({value:l,skus:c,unavailable:!1,selected:!1}))}))}function Q(e,r){return e.length?e.map(({field:s,options:o})=>({field:s,options:o.map(n=>{var u;const l=!((u=n.skus)!=null&&u.some(d=>Object.entries(r).every(([t,a])=>{var f,h;return t===s?!0:((h=(f=d.customFields)==null?void 0:f.find(g=>g.key.toLowerCase()===t))==null?void 0:h.value)===a}))),c=r[s]===n.value;return{...n,unavailable:l,selected:c}})})):[]}function W(e=[],r=[]){const[s,o]=p.useState({}),n=p.useMemo(()=>Q(K(e,r),s),[e,r,s]),l=p.useCallback((u,d)=>{o(t=>{const a={...t};return a[u]===d?delete a[u]:a[u]=d,a})},[]),c=p.useMemo(()=>Object.keys(s).filter(t=>s[t]).length===0?[]:n.filter(({field:t})=>s[t]).map(({field:t,options:a})=>{const i=s[t],f=a.find(h=>h.value===i);return(f==null?void 0:f.skus)??[]}).reduce((t,a)=>t.filter(i=>a.includes(i))),[n,s]);return{swatches:n,toggleOption:l,matchedSkus:c}}exports.useActions=S.useActions;exports.useLoadMore=S.useLoadMore;exports.useNostoAppState=S.useNostoAppState;exports.speechToTextSupported=x;exports.useDecoratedSearchResults=T;exports.useFacet=q;exports.useFacets=L;exports.usePagination=j;exports.usePersonalization=V;exports.useProductFilters=C;exports.useRange=k;exports.useRangeSelector=D;exports.useResponse=U;exports.useSelectedFiltersCount=E;exports.useSizeOptions=_;exports.useSort=I;exports.useSpeechToText=J;exports.useSwatches=W;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("../useLoadMore-BJWBvbZ1.cjs"),z=require("../logger-BcHMZObS.cjs"),p=require("preact/hooks"),P=require("../index.es-DlUp67LT.cjs"),N=require("../parseNumber-FsZ8w61u.cjs");function T(e){const r=S.useNostoAppState(t=>t.response);return e&&Array.isArray(e)||e&&z.isPlainObject(e)?e:r}function q(e,r){var d;const t=((d=e.data)==null?void 0:d.filter(s=>s.selected).length)??0,{active:n}={active:t>0,...r},[o,i]=p.useState(n),{toggleProductFilter:c}=S.useActions(),a=p.useCallback(()=>{i(!o)},[o]);return{active:o,selectedFiltersCount:t,toggleActive:a,toggleProductFilter:c}}function L(){const{loading:e,facets:r}=S.useNostoAppState(t=>{var n,o;return{loading:t.loading,facets:((o=(n=t.response)==null?void 0:n.products)==null?void 0:o.facets)??[]}});return{loading:e,facets:r}}function O(e,r){const t=r-e;return!isNaN(t)&&t>0?new Array(r-e).fill(void 0).map((n,o)=>o+e):[]}function j(e){const{query:r,products:t}=S.useNostoAppState(n=>({query:n.query,products:n.response.products}));return p.useMemo(()=>{var F,y,R;if(!t)return{totalPages:0,resultsFrom:0,resultsTo:0,pages:[]};const n=((F=r.products)==null?void 0:F.from)??0,o=(e==null?void 0:e.width)??1/0,i=Math.max(Math.floor(o-1)/2,1),c=t.size>0?Math.floor(n/t.size)+1:1,a=t.size>0?Math.ceil(t.total/t.size):0,d=A=>A>=c-i&&A<=c+i,s=n+1,l=Math.min(n+t.total,t.total),u={from:n,page:c,current:!0},g=A=>({from:(A-1)*t.size,page:A,current:A===c}),m=c>1?g(c-1):void 0,f=c<a?g(c+1):void 0,b=c-i-1>1?g(1):void 0,h=c+i+1<a?g(a):void 0,v=O(1,a+1).filter(d).map(g);return!b&&((y=v[0])==null?void 0:y.page)===2&&v.unshift(g(1)),!h&&((R=v[v.length-1])==null?void 0:R.page)===a-1&&v.push(g(a)),{totalPages:a,resultsFrom:s,resultsTo:l,current:u,prev:m,next:f,first:b,last:h,pages:v}},[r,t,e==null?void 0:e.width])}function V(){const[e,r]=p.useState([]),[t,n]=p.useState([]);return p.useEffect(()=>{P.s(async o=>{const{products:i,segments:c}=await o.getSearchSessionParams();r(c??[]),n((i==null?void 0:i.personalizationBoost)??[])})},[]),{segments:e,boost:t}}function B(){const{facets:e}=S.useNostoAppState(s=>{var l;return{facets:((l=s.response.products)==null?void 0:l.facets)??[]}}),{replaceFilter:r,toggleProductFilter:t}=S.useActions(),n=p.useCallback(s=>{const l=e==null?void 0:e.find(u=>u.type==="stats"&&u.field===s);if(l&&"min"in l&&"max"in l)return l},[e]),o=p.useCallback(s=>{var l;return((l=e==null?void 0:e.find(u=>u.field===s))==null?void 0:l.name)??s},[e]),i=p.useCallback(s=>"field"in s&&(s.value instanceof Array||s.range instanceof Array),[]),c=p.useCallback(s=>{var l;return{...s,range:(l=s.range)==null?void 0:l.map(u=>({gt:u.gt?Number(u.gt):u.gt,gte:u.gte?Number(u.gte):u.gte,lt:u.lt?Number(u.lt):u.lt,lte:u.lte?Number(u.lte):u.lte}))}},[]),a=p.useCallback(s=>(s.value??[]).map(u=>({value:u,field:s.field,name:o(s.field),filter:c(s),remove:()=>{t(s.field,u,!1)}})),[c,o,t]),d=p.useCallback(s=>(s.range??[]).map(u=>{var f,b;const g=u.gte??u.gt??((f=n(s.field))==null?void 0:f.min),m=u.lte??u.lt??((b=n(s.field))==null?void 0:b.max);if(g!==void 0&&m!==void 0)return{value:`${g} - ${m}`,field:s.field,name:o(s.field),filter:c(s),remove:()=>{r(s.field,void 0)}}}).filter(Boolean),[c,n,o,r]);return{selectFilters:i,toValueFilter:a,toRangeFilter:d}}function C(){const{filter:e}=S.useNostoAppState(a=>{var d;return{filter:((d=a.query.products)==null?void 0:d.filter)??[]}}),{updateSearch:r}=S.useActions(),{selectFilters:t,toValueFilter:n,toRangeFilter:o}=B(),i=p.useMemo(()=>e?e.filter(t).flatMap(a=>"value"in a?n(a):"range"in a?o(a):[]).filter(Boolean):[],[e,t,o,n]),c=p.useCallback(()=>{r({products:{filter:[]}})},[r]);return{filters:i,removeAll:c}}function k(e){var u,g,m;const{replaceFilter:r}=S.useActions(),{query:t,products:n}=S.useNostoAppState(f=>({query:f.query,products:f.response.products})),o=(u=n==null?void 0:n.facets)==null?void 0:u.find(f=>f.id===e);if(!o)return{min:0,max:0,range:[0,0],updateRange:()=>{}};const i=(m=(g=t.products)==null?void 0:g.filter)==null?void 0:m.find(f=>f.field===o.field),c=i!=null&&i.range?i.range[0]:void 0,a=typeof c=="object"&&("gte"in c||"lte"in c)?[N.parseNumber(c.gte),N.parseNumber(c.lte)]:[void 0,void 0],d="min"in o?Math.floor(o.min):0,s="max"in o?Math.ceil(o.max):0,l=([f,b])=>{const h=f!==void 0?Math.floor(f):void 0,v=b!==void 0?Math.ceil(b):void 0,F=h!==void 0,y=v!==void 0;(d===h||!F)&&(s===v||!y)?r(o.field,void 0):(d===h||!F)&&y?r(o.field,{lte:v.toString()}):(s===v||!y)&&F?r(o.field,{gte:h.toString()}):F&&y&&r(o.field,{gte:h.toString(),lte:v.toString()})};return{min:d,max:s,range:[a[0]??d,a[1]??s],updateRange:l}}function D(e,r){const{min:t,max:n,range:o,updateRange:i}=k(e),{filters:c}=C(),a=p.useMemo(()=>{var b;const u=c.find(h=>{var v;return(v=h==null?void 0:h.filter)==null?void 0:v.range});let g=null;if(u){const h=(b=u.filter.range)==null?void 0:b[0];g=[N.parseNumber(h==null?void 0:h.gte),N.parseNumber(h==null?void 0:h.lte)]}const m=[];let f=Math.floor(t/r)*r;for(;f<n;){const h=f+r,v=g&&g[0]===f&&g[1]===h;m.push({min:f,max:h,selected:v}),f=h}return m},[c,t,n,r]),d=u=>{i([u,o[1]])},s=u=>{i([o[0],u])},l=t!==o[0]||n!==o[1];return{min:t,max:n,range:o,updateRange:i,ranges:a,handleMinChange:d,handleMaxChange:s,isSelected:l}}function U(){const{products:e,keywords:r}=S.useNostoAppState(t=>t.response);return{products:e??{hits:[],total:0},keywords:r??{hits:[],total:0}}}function E(){const e=S.useNostoAppState(t=>{var n;return(n=t.query.products)==null?void 0:n.filter});return p.useMemo(()=>e?e.reduce((t,n)=>t+(Array.isArray(n.value)?n.value.length:1),0):0,[e])}function M(e){return e&&!Number.isNaN(e)?e:0}function _(e,r){const{from:t,size:n,total:o}=S.useNostoAppState(s=>{var l,u,g,m,f;return{from:M(((l=s.query.products)==null?void 0:l.from)??0),size:M(((g=(u=s.response)==null?void 0:u.products)==null?void 0:g.size)??0),total:M(((f=(m=s.response)==null?void 0:m.products)==null?void 0:f.total)??0)}}),{updateSearch:i}=S.useActions(),c=t+n,a=[...e].reverse().filter(s=>s+r<o);return{from:t,to:c,total:o,size:n,sizeOptions:a,handleSizeChange:s=>{i({products:{size:N.parseNumber(s)}})}}}function $(e,r){return e.length!==r.length?!1:e.every(t=>r.find(n=>t.field===n.field&&t.order===n.order))}function I(e){var i,c;const r=S.useNostoAppState(a=>a.query),{updateSearch:t}=S.useActions();return{activeSort:((i=e.find(a=>{var d;return $(a.value.sort,((d=r.products)==null?void 0:d.sort)||[])}))==null?void 0:i.id)??((c=e[0])==null?void 0:c.id),setSort:a=>{const d=e.find(s=>s.id===a);d&&t({products:{sort:d.value.sort}})}}}const w=window.SpeechRecognition||window.webkitSpeechRecognition,x=!!(w&&typeof w=="function");function G(){return{listening:!1,startListening:()=>{},stopListening:()=>{}}}function H({language:e="en-US",interimResults:r=!1}={}){const[t,n]=p.useState(!1),o=p.useRef(null),i=p.useCallback(({onResult:a,onError:d})=>{const s=new w;s.lang=e,s.interimResults=r,s.onstart=()=>n(!0),s.onresult=l=>{var g,m;const{transcript:u}=(m=(g=l.results)==null?void 0:g[0])==null?void 0:m[0];a(u)},d&&(s.onerror=l=>d(l.error)),s.onend=()=>n(!1),o.current=s,s.start()},[e,r]),c=p.useCallback(()=>{var a;(a=o.current)==null||a.stop()},[o]);return{listening:t,startListening:i,stopListening:c}}const J=x?H:G;function K(e,r){if(!e.length||!r.length)return[];const t=r.reduce((n,o)=>(n[o]={},n),{});return e.forEach(n=>{var o;(o=n.customFields)==null||o.forEach(({key:i,value:c})=>{const a=i.toLowerCase();r.includes(a)&&(t[a][c]=t[a][c]||[],t[a][c].push(n))})}),Object.entries(t).filter(n=>Object.keys(n[1]).length).map(([n,o])=>({field:n,options:Object.entries(o).map(([i,c])=>({value:i,skus:c,unavailable:!1,selected:!1}))}))}function Q(e,r){return e.length?e.map(({field:t,options:n})=>({field:t,options:n.map(o=>{var a;const i=!((a=o.skus)!=null&&a.some(d=>Object.entries(r).every(([s,l])=>{var g,m;return s===t?!0:((m=(g=d.customFields)==null?void 0:g.find(f=>f.key.toLowerCase()===s))==null?void 0:m.value)===l}))),c=r[t]===o.value;return{...o,unavailable:i,selected:c}})})):[]}function W(e=[],r=[]){const[t,n]=p.useState({}),o=p.useMemo(()=>K(e,r),[e,r]),i=p.useMemo(()=>Q(o,t),[o,t]),c=p.useCallback((d,s)=>{n(l=>{const u={...l};return u[d]===s?delete u[d]:u[d]=s,u})},[]),a=p.useMemo(()=>Object.keys(t).filter(l=>t[l]).length===0?[]:i.filter(({field:l})=>t[l]).map(({field:l,options:u})=>{const g=t[l],m=u.find(f=>f.value===g);return(m==null?void 0:m.skus)??[]}).reduce((l,u)=>l.filter(g=>u.includes(g))),[i,t]);return{swatches:i,toggleOption:c,matchedSkus:a}}exports.useActions=S.useActions;exports.useLoadMore=S.useLoadMore;exports.useNostoAppState=S.useNostoAppState;exports.speechToTextSupported=x;exports.useDecoratedSearchResults=T;exports.useFacet=q;exports.useFacets=L;exports.usePagination=j;exports.usePersonalization=V;exports.useProductFilters=C;exports.useRange=k;exports.useRangeSelector=D;exports.useResponse=U;exports.useSelectedFiltersCount=E;exports.useSizeOptions=_;exports.useSort=I;exports.useSpeechToText=J;exports.useSwatches=W;
@@ -1,38 +1,38 @@
1
- import { a as y, c as x } from "../useLoadMore-jrN1VGDU.js";
2
- import { b as ue } from "../useLoadMore-jrN1VGDU.js";
1
+ import { a as y, c as x } from "../useLoadMore-CzqfcD1v.js";
2
+ import { b as ue } from "../useLoadMore-CzqfcD1v.js";
3
3
  import { a as T } from "../logger-DVwg4Wor.js";
4
- import { useState as M, useCallback as S, useMemo as b, useEffect as C, useRef as q } from "preact/hooks";
5
- import { s as L } from "../index.es-B8mbAxS4.js";
4
+ import { useState as M, useCallback as S, useMemo as b, useEffect as C, useRef as k } from "preact/hooks";
5
+ import { s as q } from "../index.es-B8mbAxS4.js";
6
6
  import { p as z } from "../parseNumber-QA48nJLp.js";
7
7
  function Q(e) {
8
- const o = y((n) => n.response);
8
+ const o = y((t) => t.response);
9
9
  return e && Array.isArray(e) || e && T(e) ? e : o;
10
10
  }
11
11
  function W(e, o) {
12
12
  var d;
13
- const n = ((d = e.data) == null ? void 0 : d.filter((t) => t.selected).length) ?? 0, { active: s } = {
14
- active: n > 0,
13
+ const t = ((d = e.data) == null ? void 0 : d.filter((n) => n.selected).length) ?? 0, { active: r } = {
14
+ active: t > 0,
15
15
  ...o
16
- }, [r, l] = M(s), { toggleProductFilter: i } = x(), c = S(() => {
17
- l(!r);
18
- }, [r]);
16
+ }, [s, u] = M(r), { toggleProductFilter: i } = x(), a = S(() => {
17
+ u(!s);
18
+ }, [s]);
19
19
  return {
20
20
  /** Active value */
21
- active: r,
21
+ active: s,
22
22
  /** Selected filters count */
23
- selectedFiltersCount: n,
23
+ selectedFiltersCount: t,
24
24
  /** Toggle active function */
25
- toggleActive: c,
25
+ toggleActive: a,
26
26
  /** Toggle product filter function */
27
27
  toggleProductFilter: i
28
28
  };
29
29
  }
30
30
  function X() {
31
- const { loading: e, facets: o } = y((n) => {
32
- var s, r;
31
+ const { loading: e, facets: o } = y((t) => {
32
+ var r, s;
33
33
  return {
34
- loading: n.loading,
35
- facets: ((r = (s = n.response) == null ? void 0 : s.products) == null ? void 0 : r.facets) ?? []
34
+ loading: t.loading,
35
+ facets: ((s = (r = t.response) == null ? void 0 : r.products) == null ? void 0 : s.facets) ?? []
36
36
  };
37
37
  });
38
38
  return {
@@ -42,129 +42,129 @@ function X() {
42
42
  facets: o
43
43
  };
44
44
  }
45
- function k(e, o) {
46
- const n = o - e;
47
- return !isNaN(n) && n > 0 ? new Array(o - e).fill(void 0).map((s, r) => r + e) : [];
45
+ function L(e, o) {
46
+ const t = o - e;
47
+ return !isNaN(t) && t > 0 ? new Array(o - e).fill(void 0).map((r, s) => s + e) : [];
48
48
  }
49
49
  function Y(e) {
50
- const { query: o, products: n } = y((s) => ({
51
- query: s.query,
52
- products: s.response.products
50
+ const { query: o, products: t } = y((r) => ({
51
+ query: r.query,
52
+ products: r.response.products
53
53
  }));
54
54
  return b(() => {
55
55
  var F, w, N;
56
- if (!n)
56
+ if (!t)
57
57
  return {
58
58
  totalPages: 0,
59
59
  resultsFrom: 0,
60
60
  resultsTo: 0,
61
61
  pages: []
62
62
  };
63
- const s = ((F = o.products) == null ? void 0 : F.from) ?? 0, r = (e == null ? void 0 : e.width) ?? 1 / 0, l = Math.max(Math.floor(r - 1) / 2, 1), i = n.size > 0 ? Math.floor(s / n.size) + 1 : 1, c = n.size > 0 ? Math.ceil(n.total / n.size) : 0, d = (R) => R >= i - l && R <= i + l, t = s + 1, u = Math.min(s + n.total, n.total), a = {
64
- from: s,
63
+ const r = ((F = o.products) == null ? void 0 : F.from) ?? 0, s = (e == null ? void 0 : e.width) ?? 1 / 0, u = Math.max(Math.floor(s - 1) / 2, 1), i = t.size > 0 ? Math.floor(r / t.size) + 1 : 1, a = t.size > 0 ? Math.ceil(t.total / t.size) : 0, d = (R) => R >= i - u && R <= i + u, n = r + 1, l = Math.min(r + t.total, t.total), c = {
64
+ from: r,
65
65
  page: i,
66
66
  current: !0
67
67
  }, f = (R) => ({
68
- from: (R - 1) * n.size,
68
+ from: (R - 1) * t.size,
69
69
  page: R,
70
70
  current: R === i
71
- }), p = i > 1 ? f(i - 1) : void 0, g = i < c ? f(i + 1) : void 0, v = i - l - 1 > 1 ? f(1) : void 0, m = i + l + 1 < c ? f(c) : void 0, h = k(1, c + 1).filter(d).map(f);
72
- return !v && ((w = h[0]) == null ? void 0 : w.page) === 2 && h.unshift(f(1)), !m && ((N = h[h.length - 1]) == null ? void 0 : N.page) === c - 1 && h.push(f(c)), {
73
- totalPages: c,
74
- resultsFrom: t,
75
- resultsTo: u,
76
- current: a,
77
- prev: p,
71
+ }), m = i > 1 ? f(i - 1) : void 0, g = i < a ? f(i + 1) : void 0, v = i - u - 1 > 1 ? f(1) : void 0, p = i + u + 1 < a ? f(a) : void 0, h = L(1, a + 1).filter(d).map(f);
72
+ return !v && ((w = h[0]) == null ? void 0 : w.page) === 2 && h.unshift(f(1)), !p && ((N = h[h.length - 1]) == null ? void 0 : N.page) === a - 1 && h.push(f(a)), {
73
+ totalPages: a,
74
+ resultsFrom: n,
75
+ resultsTo: l,
76
+ current: c,
77
+ prev: m,
78
78
  next: g,
79
79
  first: v,
80
- last: m,
80
+ last: p,
81
81
  pages: h
82
82
  };
83
- }, [o, n, e == null ? void 0 : e.width]);
83
+ }, [o, t, e == null ? void 0 : e.width]);
84
84
  }
85
85
  function Z() {
86
- const [e, o] = M([]), [n, s] = M([]);
86
+ const [e, o] = M([]), [t, r] = M([]);
87
87
  return C(() => {
88
- L(async (r) => {
89
- const { products: l, segments: i } = await r.getSearchSessionParams();
90
- o(i ?? []), s((l == null ? void 0 : l.personalizationBoost) ?? []);
88
+ q(async (s) => {
89
+ const { products: u, segments: i } = await s.getSearchSessionParams();
90
+ o(i ?? []), r((u == null ? void 0 : u.personalizationBoost) ?? []);
91
91
  });
92
92
  }, []), {
93
93
  segments: e,
94
- boost: n
94
+ boost: t
95
95
  };
96
96
  }
97
97
  function O() {
98
- const { facets: e } = y((t) => {
99
- var u;
98
+ const { facets: e } = y((n) => {
99
+ var l;
100
100
  return {
101
- facets: ((u = t.response.products) == null ? void 0 : u.facets) ?? []
101
+ facets: ((l = n.response.products) == null ? void 0 : l.facets) ?? []
102
102
  };
103
- }), { replaceFilter: o, toggleProductFilter: n } = x(), s = S(
104
- (t) => {
105
- const u = e == null ? void 0 : e.find((a) => a.type === "stats" && a.field === t);
106
- if (u && "min" in u && "max" in u)
107
- return u;
103
+ }), { replaceFilter: o, toggleProductFilter: t } = x(), r = S(
104
+ (n) => {
105
+ const l = e == null ? void 0 : e.find((c) => c.type === "stats" && c.field === n);
106
+ if (l && "min" in l && "max" in l)
107
+ return l;
108
108
  },
109
109
  [e]
110
- ), r = S(
111
- (t) => {
112
- var u;
113
- return ((u = e == null ? void 0 : e.find((a) => a.field === t)) == null ? void 0 : u.name) ?? t;
110
+ ), s = S(
111
+ (n) => {
112
+ var l;
113
+ return ((l = e == null ? void 0 : e.find((c) => c.field === n)) == null ? void 0 : l.name) ?? n;
114
114
  },
115
115
  [e]
116
- ), l = S((t) => "field" in t && (t.value instanceof Array || t.range instanceof Array), []), i = S((t) => {
117
- var u;
116
+ ), u = S((n) => "field" in n && (n.value instanceof Array || n.range instanceof Array), []), i = S((n) => {
117
+ var l;
118
118
  return {
119
- ...t,
120
- range: (u = t.range) == null ? void 0 : u.map((a) => ({
121
- gt: a.gt ? Number(a.gt) : a.gt,
122
- gte: a.gte ? Number(a.gte) : a.gte,
123
- lt: a.lt ? Number(a.lt) : a.lt,
124
- lte: a.lte ? Number(a.lte) : a.lte
119
+ ...n,
120
+ range: (l = n.range) == null ? void 0 : l.map((c) => ({
121
+ gt: c.gt ? Number(c.gt) : c.gt,
122
+ gte: c.gte ? Number(c.gte) : c.gte,
123
+ lt: c.lt ? Number(c.lt) : c.lt,
124
+ lte: c.lte ? Number(c.lte) : c.lte
125
125
  }))
126
126
  };
127
- }, []), c = S(
128
- (t) => (t.value ?? []).map((a) => ({
129
- value: a,
130
- field: t.field,
131
- name: r(t.field),
132
- filter: i(t),
127
+ }, []), a = S(
128
+ (n) => (n.value ?? []).map((c) => ({
129
+ value: c,
130
+ field: n.field,
131
+ name: s(n.field),
132
+ filter: i(n),
133
133
  remove: () => {
134
- n(t.field, a, !1);
134
+ t(n.field, c, !1);
135
135
  }
136
136
  })),
137
- [i, r, n]
137
+ [i, s, t]
138
138
  ), d = S(
139
- (t) => (t.range ?? []).map((a) => {
139
+ (n) => (n.range ?? []).map((c) => {
140
140
  var g, v;
141
- const f = a.gte ?? a.gt ?? ((g = s(t.field)) == null ? void 0 : g.min), p = a.lte ?? a.lt ?? ((v = s(t.field)) == null ? void 0 : v.max);
142
- if (f !== void 0 && p !== void 0)
141
+ const f = c.gte ?? c.gt ?? ((g = r(n.field)) == null ? void 0 : g.min), m = c.lte ?? c.lt ?? ((v = r(n.field)) == null ? void 0 : v.max);
142
+ if (f !== void 0 && m !== void 0)
143
143
  return {
144
- value: `${f} - ${p}`,
145
- field: t.field,
146
- name: r(t.field),
147
- filter: i(t),
144
+ value: `${f} - ${m}`,
145
+ field: n.field,
146
+ name: s(n.field),
147
+ filter: i(n),
148
148
  remove: () => {
149
- o(t.field, void 0);
149
+ o(n.field, void 0);
150
150
  }
151
151
  };
152
152
  }).filter(Boolean),
153
- [i, s, r, o]
153
+ [i, r, s, o]
154
154
  );
155
155
  return {
156
- selectFilters: l,
157
- toValueFilter: c,
156
+ selectFilters: u,
157
+ toValueFilter: a,
158
158
  toRangeFilter: d
159
159
  };
160
160
  }
161
161
  function j() {
162
- const { filter: e } = y((c) => {
162
+ const { filter: e } = y((a) => {
163
163
  var d;
164
164
  return {
165
- filter: ((d = c.query.products) == null ? void 0 : d.filter) ?? []
165
+ filter: ((d = a.query.products) == null ? void 0 : d.filter) ?? []
166
166
  };
167
- }), { updateSearch: o } = x(), { selectFilters: n, toValueFilter: s, toRangeFilter: r } = O(), l = b(() => e ? e.filter(n).flatMap((c) => "value" in c ? s(c) : "range" in c ? r(c) : []).filter(Boolean) : [], [e, n, r, s]), i = S(() => {
167
+ }), { updateSearch: o } = x(), { selectFilters: t, toValueFilter: r, toRangeFilter: s } = O(), u = b(() => e ? e.filter(t).flatMap((a) => "value" in a ? r(a) : "range" in a ? s(a) : []).filter(Boolean) : [], [e, t, s, r]), i = S(() => {
168
168
  o({
169
169
  products: {
170
170
  filter: []
@@ -173,18 +173,18 @@ function j() {
173
173
  }, [o]);
174
174
  return {
175
175
  /** Selected filters array. */
176
- filters: l,
176
+ filters: u,
177
177
  /** Should remove all selected filters. */
178
178
  removeAll: i
179
179
  };
180
180
  }
181
181
  function V(e) {
182
- var a, f, p;
183
- const { replaceFilter: o } = x(), { query: n, products: s } = y((g) => ({
182
+ var c, f, m;
183
+ const { replaceFilter: o } = x(), { query: t, products: r } = y((g) => ({
184
184
  query: g.query,
185
185
  products: g.response.products
186
- })), r = (a = s == null ? void 0 : s.facets) == null ? void 0 : a.find((g) => g.id === e);
187
- if (!r)
186
+ })), s = (c = r == null ? void 0 : r.facets) == null ? void 0 : c.find((g) => g.id === e);
187
+ if (!s)
188
188
  return {
189
189
  min: 0,
190
190
  max: 0,
@@ -192,14 +192,14 @@ function V(e) {
192
192
  updateRange: () => {
193
193
  }
194
194
  };
195
- const l = (p = (f = n.products) == null ? void 0 : f.filter) == null ? void 0 : p.find((g) => g.field === r.field), i = l != null && l.range ? l.range[0] : void 0, c = typeof i == "object" && ("gte" in i || "lte" in i) ? [z(i.gte), z(i.lte)] : [void 0, void 0], d = "min" in r ? Math.floor(r.min) : 0, t = "max" in r ? Math.ceil(r.max) : 0, u = ([g, v]) => {
196
- const m = g !== void 0 ? Math.floor(g) : void 0, h = v !== void 0 ? Math.ceil(v) : void 0, F = m !== void 0, w = h !== void 0;
197
- (d === m || !F) && (t === h || !w) ? o(r.field, void 0) : (d === m || !F) && w ? o(r.field, {
195
+ const u = (m = (f = t.products) == null ? void 0 : f.filter) == null ? void 0 : m.find((g) => g.field === s.field), i = u != null && u.range ? u.range[0] : void 0, a = typeof i == "object" && ("gte" in i || "lte" in i) ? [z(i.gte), z(i.lte)] : [void 0, void 0], d = "min" in s ? Math.floor(s.min) : 0, n = "max" in s ? Math.ceil(s.max) : 0, l = ([g, v]) => {
196
+ const p = g !== void 0 ? Math.floor(g) : void 0, h = v !== void 0 ? Math.ceil(v) : void 0, F = p !== void 0, w = h !== void 0;
197
+ (d === p || !F) && (n === h || !w) ? o(s.field, void 0) : (d === p || !F) && w ? o(s.field, {
198
198
  lte: h.toString()
199
- }) : (t === h || !w) && F ? o(r.field, {
200
- gte: m.toString()
201
- }) : F && w && o(r.field, {
202
- gte: m.toString(),
199
+ }) : (n === h || !w) && F ? o(s.field, {
200
+ gte: p.toString()
201
+ }) : F && w && o(s.field, {
202
+ gte: p.toString(),
203
203
  lte: h.toString()
204
204
  });
205
205
  };
@@ -207,62 +207,62 @@ function V(e) {
207
207
  /** Min value */
208
208
  min: d,
209
209
  /** Max value */
210
- max: t,
210
+ max: n,
211
211
  /** Range value */
212
- range: [c[0] ?? d, c[1] ?? t],
212
+ range: [a[0] ?? d, a[1] ?? n],
213
213
  /** Update range function */
214
- updateRange: u
214
+ updateRange: l
215
215
  };
216
216
  }
217
217
  function ee(e, o) {
218
- const { min: n, max: s, range: r, updateRange: l } = V(e), { filters: i } = j(), c = b(() => {
218
+ const { min: t, max: r, range: s, updateRange: u } = V(e), { filters: i } = j(), a = b(() => {
219
219
  var v;
220
- const a = i.find((m) => {
220
+ const c = i.find((p) => {
221
221
  var h;
222
- return (h = m == null ? void 0 : m.filter) == null ? void 0 : h.range;
222
+ return (h = p == null ? void 0 : p.filter) == null ? void 0 : h.range;
223
223
  });
224
224
  let f = null;
225
- if (a) {
226
- const m = (v = a.filter.range) == null ? void 0 : v[0];
227
- f = [z(m == null ? void 0 : m.gte), z(m == null ? void 0 : m.lte)];
225
+ if (c) {
226
+ const p = (v = c.filter.range) == null ? void 0 : v[0];
227
+ f = [z(p == null ? void 0 : p.gte), z(p == null ? void 0 : p.lte)];
228
228
  }
229
- const p = [];
230
- let g = Math.floor(n / o) * o;
231
- for (; g < s; ) {
232
- const m = g + o, h = f && f[0] === g && f[1] === m;
233
- p.push({
229
+ const m = [];
230
+ let g = Math.floor(t / o) * o;
231
+ for (; g < r; ) {
232
+ const p = g + o, h = f && f[0] === g && f[1] === p;
233
+ m.push({
234
234
  min: g,
235
- max: m,
235
+ max: p,
236
236
  selected: h
237
- }), g = m;
237
+ }), g = p;
238
238
  }
239
- return p;
240
- }, [i, n, s, o]), d = (a) => {
241
- l([a, r[1]]);
242
- }, t = (a) => {
243
- l([r[0], a]);
244
- }, u = n !== r[0] || s !== r[1];
239
+ return m;
240
+ }, [i, t, r, o]), d = (c) => {
241
+ u([c, s[1]]);
242
+ }, n = (c) => {
243
+ u([s[0], c]);
244
+ }, l = t !== s[0] || r !== s[1];
245
245
  return {
246
246
  /** Minimum value */
247
- min: n,
247
+ min: t,
248
248
  /** Maximum value */
249
- max: s,
249
+ max: r,
250
250
  /** Range value */
251
- range: r,
251
+ range: s,
252
252
  /** Update range function */
253
- updateRange: l,
253
+ updateRange: u,
254
254
  /** Ranges */
255
- ranges: c,
255
+ ranges: a,
256
256
  /** Handle min change */
257
257
  handleMinChange: d,
258
258
  /** Handle max change */
259
- handleMaxChange: t,
259
+ handleMaxChange: n,
260
260
  /** Is selected */
261
- isSelected: u
261
+ isSelected: l
262
262
  };
263
263
  }
264
264
  function te() {
265
- const { products: e, keywords: o } = y((n) => n.response);
265
+ const { products: e, keywords: o } = y((t) => t.response);
266
266
  return {
267
267
  /** Array of products */
268
268
  products: e ?? { hits: [], total: 0 },
@@ -271,61 +271,61 @@ function te() {
271
271
  };
272
272
  }
273
273
  function ne() {
274
- const e = y((n) => {
275
- var s;
276
- return (s = n.query.products) == null ? void 0 : s.filter;
274
+ const e = y((t) => {
275
+ var r;
276
+ return (r = t.query.products) == null ? void 0 : r.filter;
277
277
  });
278
- return b(() => e ? e.reduce((n, s) => n + (Array.isArray(s.value) ? s.value.length : 1), 0) : 0, [e]);
278
+ return b(() => e ? e.reduce((t, r) => t + (Array.isArray(r.value) ? r.value.length : 1), 0) : 0, [e]);
279
279
  }
280
280
  function A(e) {
281
281
  return e && !Number.isNaN(e) ? e : 0;
282
282
  }
283
283
  function re(e, o) {
284
- const { from: n, size: s, total: r } = y((t) => {
285
- var u, a, f, p, g;
284
+ const { from: t, size: r, total: s } = y((n) => {
285
+ var l, c, f, m, g;
286
286
  return {
287
- from: A(((u = t.query.products) == null ? void 0 : u.from) ?? 0),
288
- size: A(((f = (a = t.response) == null ? void 0 : a.products) == null ? void 0 : f.size) ?? 0),
289
- total: A(((g = (p = t.response) == null ? void 0 : p.products) == null ? void 0 : g.total) ?? 0)
287
+ from: A(((l = n.query.products) == null ? void 0 : l.from) ?? 0),
288
+ size: A(((f = (c = n.response) == null ? void 0 : c.products) == null ? void 0 : f.size) ?? 0),
289
+ total: A(((g = (m = n.response) == null ? void 0 : m.products) == null ? void 0 : g.total) ?? 0)
290
290
  };
291
- }), { updateSearch: l } = x(), i = n + s, c = [...e].reverse().filter((t) => t + o < r);
291
+ }), { updateSearch: u } = x(), i = t + r, a = [...e].reverse().filter((n) => n + o < s);
292
292
  return {
293
293
  /** from value */
294
- from: n,
294
+ from: t,
295
295
  /** to value */
296
296
  to: i,
297
297
  /** total value */
298
- total: r,
298
+ total: s,
299
299
  /** size value */
300
- size: s,
300
+ size: r,
301
301
  /** Array of size options */
302
- sizeOptions: c,
302
+ sizeOptions: a,
303
303
  /** Should be called when size is changed */
304
- handleSizeChange: (t) => {
305
- l({
304
+ handleSizeChange: (n) => {
305
+ u({
306
306
  products: {
307
- size: z(t)
307
+ size: z(n)
308
308
  }
309
309
  });
310
310
  }
311
311
  };
312
312
  }
313
313
  function B(e, o) {
314
- return e.length !== o.length ? !1 : e.every((n) => o.find((s) => n.field === s.field && n.order === s.order));
314
+ return e.length !== o.length ? !1 : e.every((t) => o.find((r) => t.field === r.field && t.order === r.order));
315
315
  }
316
316
  function se(e) {
317
- var l, i;
318
- const o = y((c) => c.query), { updateSearch: n } = x();
317
+ var u, i;
318
+ const o = y((a) => a.query), { updateSearch: t } = x();
319
319
  return {
320
320
  /** Active sort */
321
- activeSort: ((l = e.find((c) => {
321
+ activeSort: ((u = e.find((a) => {
322
322
  var d;
323
- return B(c.value.sort, ((d = o.products) == null ? void 0 : d.sort) || []);
324
- })) == null ? void 0 : l.id) ?? ((i = e[0]) == null ? void 0 : i.id),
323
+ return B(a.value.sort, ((d = o.products) == null ? void 0 : d.sort) || []);
324
+ })) == null ? void 0 : u.id) ?? ((i = e[0]) == null ? void 0 : i.id),
325
325
  /** Set sort function */
326
- setSort: (c) => {
327
- const d = e.find((t) => t.id === c);
328
- d && n({
326
+ setSort: (a) => {
327
+ const d = e.find((n) => n.id === a);
328
+ d && t({
329
329
  products: {
330
330
  sort: d.value.sort
331
331
  }
@@ -347,40 +347,40 @@ function E({
347
347
  language: e = "en-US",
348
348
  interimResults: o = !1
349
349
  } = {}) {
350
- const [n, s] = M(!1), r = q(null), l = S(
351
- ({ onResult: c, onError: d }) => {
352
- const t = new P();
353
- t.lang = e, t.interimResults = o, t.onstart = () => s(!0), t.onresult = (u) => {
354
- var f, p;
355
- const { transcript: a } = (p = (f = u.results) == null ? void 0 : f[0]) == null ? void 0 : p[0];
356
- c(a);
357
- }, d && (t.onerror = (u) => d(u.error)), t.onend = () => s(!1), r.current = t, t.start();
350
+ const [t, r] = M(!1), s = k(null), u = S(
351
+ ({ onResult: a, onError: d }) => {
352
+ const n = new P();
353
+ n.lang = e, n.interimResults = o, n.onstart = () => r(!0), n.onresult = (l) => {
354
+ var f, m;
355
+ const { transcript: c } = (m = (f = l.results) == null ? void 0 : f[0]) == null ? void 0 : m[0];
356
+ a(c);
357
+ }, d && (n.onerror = (l) => d(l.error)), n.onend = () => r(!1), s.current = n, n.start();
358
358
  },
359
359
  [e, o]
360
360
  ), i = S(() => {
361
- var c;
362
- (c = r.current) == null || c.stop();
363
- }, [r]);
361
+ var a;
362
+ (a = s.current) == null || a.stop();
363
+ }, [s]);
364
364
  return {
365
- listening: n,
366
- startListening: l,
365
+ listening: t,
366
+ startListening: u,
367
367
  stopListening: i
368
368
  };
369
369
  }
370
370
  const oe = D ? E : U;
371
371
  function $(e, o) {
372
372
  if (!e.length || !o.length) return [];
373
- const n = o.reduce((s, r) => (s[r] = {}, s), {});
374
- return e.forEach((s) => {
375
- var r;
376
- (r = s.customFields) == null || r.forEach(({ key: l, value: i }) => {
377
- const c = l.toLowerCase();
378
- o.includes(c) && (n[c][i] = n[c][i] || [], n[c][i].push(s));
373
+ const t = o.reduce((r, s) => (r[s] = {}, r), {});
374
+ return e.forEach((r) => {
375
+ var s;
376
+ (s = r.customFields) == null || s.forEach(({ key: u, value: i }) => {
377
+ const a = u.toLowerCase();
378
+ o.includes(a) && (t[a][i] = t[a][i] || [], t[a][i].push(r));
379
379
  });
380
- }), Object.entries(n).map(([s, r]) => ({
381
- field: s,
382
- options: Object.entries(r).map(([l, i]) => ({
383
- value: l,
380
+ }), Object.entries(t).filter((r) => Object.keys(r[1]).length).map(([r, s]) => ({
381
+ field: r,
382
+ options: Object.entries(s).map(([u, i]) => ({
383
+ value: u,
384
384
  skus: i,
385
385
  unavailable: !1,
386
386
  selected: !1
@@ -388,33 +388,33 @@ function $(e, o) {
388
388
  }));
389
389
  }
390
390
  function I(e, o) {
391
- return e.length ? e.map(({ field: n, options: s }) => ({
392
- field: n,
393
- options: s.map((r) => {
394
- var c;
395
- const l = !((c = r.skus) != null && c.some((d) => Object.entries(o).every(([t, u]) => {
396
- var f, p;
397
- return t === n ? !0 : ((p = (f = d.customFields) == null ? void 0 : f.find((g) => g.key.toLowerCase() === t)) == null ? void 0 : p.value) === u;
398
- }))), i = o[n] === r.value;
391
+ return e.length ? e.map(({ field: t, options: r }) => ({
392
+ field: t,
393
+ options: r.map((s) => {
394
+ var a;
395
+ const u = !((a = s.skus) != null && a.some((d) => Object.entries(o).every(([n, l]) => {
396
+ var f, m;
397
+ return n === t ? !0 : ((m = (f = d.customFields) == null ? void 0 : f.find((g) => g.key.toLowerCase() === n)) == null ? void 0 : m.value) === l;
398
+ }))), i = o[t] === s.value;
399
399
  return {
400
- ...r,
401
- unavailable: l,
400
+ ...s,
401
+ unavailable: u,
402
402
  selected: i
403
403
  };
404
404
  })
405
405
  })) : [];
406
406
  }
407
407
  function ce(e = [], o = []) {
408
- const [n, s] = M({}), r = b(() => I($(e, o), n), [e, o, n]), l = S((c, d) => {
409
- s((t) => {
410
- const u = { ...t };
411
- return u[c] === d ? delete u[c] : u[c] = d, u;
408
+ const [t, r] = M({}), s = b(() => $(e, o), [e, o]), u = b(() => I(s, t), [s, t]), i = S((d, n) => {
409
+ r((l) => {
410
+ const c = { ...l };
411
+ return c[d] === n ? delete c[d] : c[d] = n, c;
412
412
  });
413
- }, []), i = b(() => Object.keys(n).filter((t) => n[t]).length === 0 ? [] : r.filter(({ field: t }) => n[t]).map(({ field: t, options: u }) => {
414
- const a = n[t], f = u.find((p) => p.value === a);
415
- return (f == null ? void 0 : f.skus) ?? [];
416
- }).reduce((t, u) => t.filter((a) => u.includes(a))), [r, n]);
417
- return { swatches: r, toggleOption: l, matchedSkus: i };
413
+ }, []), a = b(() => Object.keys(t).filter((l) => t[l]).length === 0 ? [] : u.filter(({ field: l }) => t[l]).map(({ field: l, options: c }) => {
414
+ const f = t[l], m = c.find((g) => g.value === f);
415
+ return (m == null ? void 0 : m.skus) ?? [];
416
+ }).reduce((l, c) => l.filter((f) => c.includes(f))), [u, t]);
417
+ return { swatches: u, toggleOption: i, matchedSkus: a };
418
418
  }
419
419
  export {
420
420
  D as speechToTextSupported,
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("../useLoadMore-BFEnHdOH.cjs"),r=require("../InfiniteScrollWithObserver-DfcZhVtT.cjs");exports.getNextPageQuery=e.getNextPageQuery;exports.newSearch=e.newSearch;exports.updateSearch=e.updateSearch;exports.InfiniteScrollWithObserver=r.InfiniteScrollWithObserver;exports.intersectionObserverSupported=r.intersectionObserverSupported;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("../useLoadMore-BJWBvbZ1.cjs"),r=require("../InfiniteScrollWithObserver-Do05gtr2.cjs");exports.getNextPageQuery=e.getNextPageQuery;exports.newSearch=e.newSearch;exports.updateSearch=e.updateSearch;exports.InfiniteScrollWithObserver=r.InfiniteScrollWithObserver;exports.intersectionObserverSupported=r.intersectionObserverSupported;
@@ -1,5 +1,5 @@
1
- import { g as t, n as a, u as s } from "../useLoadMore-jrN1VGDU.js";
2
- import { I as i, i as n } from "../InfiniteScrollWithObserver-D_nTat6t.js";
1
+ import { g as t, n as a, u as s } from "../useLoadMore-CzqfcD1v.js";
2
+ import { I as i, i as n } from "../InfiniteScrollWithObserver-CvnCyquA.js";
3
3
  export {
4
4
  i as InfiniteScrollWithObserver,
5
5
  t as getNextPageQuery,
@@ -0,0 +1,2 @@
1
+ import { SwatchField } from '../types';
2
+ export declare function applySelectionStates(swatches: SwatchField[], selectedOptions: Record<string, string>): SwatchField[];
@@ -0,0 +1 @@
1
+ "use strict";const b=require("./index.es-DlUp67LT.cjs"),a=require("./logger-BcHMZObS.cjs");async function z(t,{hitDecorators:e,...r},c){var o,i;const n=await c(t,r);if(!((i=(o=n.products)==null?void 0:o.hits)!=null&&i.length)||!(e!=null&&e.length))return n;const s=u=>e.reduce((f,l)=>l(f),u);return{...n,products:{...n.products,hits:n.products.hits.map(s)}}}function N(t,e,r){const c=JSON.stringify(e);try{r.setItem(t,c)}catch(n){a.logger.warn(n)}}function k(t,e){try{const r=e.getItem(t);if(r)return JSON.parse(r)}catch(r){a.logger.warn(r)}}function C(t,e){N(t,e,sessionStorage)}function O(t){return k(t,sessionStorage)}const R="nosto:search-js:cache",T=60*1e3;function E(t,e){C(R,{query:t,result:e,created:Date.now()})}function I(t){const e=O(R);if(!e||!J(e))return null;const r=m(e.query);return!a.isEqual(m(t),r)||Date.now()-e.created>T?null:e.result}function m(t){const e={...t,time:void 0,products:{...t.products,size:void 0}};return JSON.parse(JSON.stringify(e))}function J(t){return typeof t=="object"&&t!==null&&"query"in t&&"result"in t&&"created"in t}async function V(t,{usePersistentCache:e,...r},c){if(!e)return c(t,r);const n=await j(t,r,c);return E(t,n),n}async function j(t,e,r){var p,g,h,S,w,y;const{from:c=0,size:n=0}=t.products||{},s=I(t);if(!s)return await r(t,e);const o=((p=s==null?void 0:s.products)==null?void 0:p.size)??0,i=((g=s==null?void 0:s.products)==null?void 0:g.hits)??[];if(n===o)return s;if(n<o)return{...s,products:{...s.products,size:n,hits:i.slice(0,n),total:((h=s.products)==null?void 0:h.total)||0}};const u=n-i.length,f=c>0?c+1:n-u,l={...t,products:{...t.products,from:f,size:u}},d=await r(l,e);return{...s,products:{...s.products,size:n,hits:[...((S=s.products)==null?void 0:S.hits)||[],...((w=d.products)==null?void 0:w.hits)||[]],total:((y=d.products)==null?void 0:y.total)||0}}}function W(t){return new Promise(e=>setTimeout(e,t))}async function _(t,{maxRetries:e=0,retryInterval:r=0,...c},n){let s=0;for(;;)try{return await n(t,c)}catch(o){if(s>=e)throw o;if(!q(o))throw a.logger.info("Skipping retry logic for",o),o;s++,await W(r)}}function q(t){return!t||typeof t!="object"?!1:!("status"in t)||A(t.status)}function A(t){return typeof t=="number"&&(t<400||t>=500)}async function P(t,e={}){const r=await new Promise(b.s);return x(r.search,_,V,z)(t,e)}function x(t,...e){return e.reduce((r,c)=>(n,s)=>c(n,s,r),t)}exports.search=P;
@@ -0,0 +1,138 @@
1
+ import { s as b } from "./index.es-B8mbAxS4.js";
2
+ import { l, i as z } from "./logger-DVwg4Wor.js";
3
+ async function N(t, { hitDecorators: e, ...r }, o) {
4
+ var c, i;
5
+ const n = await o(t, r);
6
+ if (!((i = (c = n.products) == null ? void 0 : c.hits) != null && i.length) || !(e != null && e.length))
7
+ return n;
8
+ const s = (u) => e.reduce((a, f) => f(a), u);
9
+ return {
10
+ ...n,
11
+ products: {
12
+ ...n.products,
13
+ hits: n.products.hits.map(s)
14
+ }
15
+ };
16
+ }
17
+ function k(t, e, r) {
18
+ const o = JSON.stringify(e);
19
+ try {
20
+ r.setItem(t, o);
21
+ } catch (n) {
22
+ l.warn(n);
23
+ }
24
+ }
25
+ function C(t, e) {
26
+ try {
27
+ const r = e.getItem(t);
28
+ if (r)
29
+ return JSON.parse(r);
30
+ } catch (r) {
31
+ l.warn(r);
32
+ }
33
+ }
34
+ function O(t, e) {
35
+ k(t, e, sessionStorage);
36
+ }
37
+ function T(t) {
38
+ return C(t, sessionStorage);
39
+ }
40
+ const R = "nosto:search-js:cache", E = 60 * 1e3;
41
+ function I(t, e) {
42
+ O(R, { query: t, result: e, created: Date.now() });
43
+ }
44
+ function J(t) {
45
+ const e = T(R);
46
+ if (!e || !V(e))
47
+ return null;
48
+ const r = y(e.query);
49
+ return !z(y(t), r) || Date.now() - e.created > E ? null : e.result;
50
+ }
51
+ function y(t) {
52
+ const e = {
53
+ ...t,
54
+ time: void 0,
55
+ products: {
56
+ ...t.products,
57
+ size: void 0
58
+ }
59
+ };
60
+ return JSON.parse(JSON.stringify(e));
61
+ }
62
+ function V(t) {
63
+ return typeof t == "object" && t !== null && "query" in t && "result" in t && "created" in t;
64
+ }
65
+ async function j(t, { usePersistentCache: e, ...r }, o) {
66
+ if (!e)
67
+ return o(t, r);
68
+ const n = await W(t, r, o);
69
+ return I(t, n), n;
70
+ }
71
+ async function W(t, e, r) {
72
+ var d, h, g, S, m, w;
73
+ const { from: o = 0, size: n = 0 } = t.products || {}, s = J(t);
74
+ if (!s)
75
+ return await r(t, e);
76
+ const c = ((d = s == null ? void 0 : s.products) == null ? void 0 : d.size) ?? 0, i = ((h = s == null ? void 0 : s.products) == null ? void 0 : h.hits) ?? [];
77
+ if (n === c)
78
+ return s;
79
+ if (n < c)
80
+ return {
81
+ ...s,
82
+ products: {
83
+ ...s.products,
84
+ size: n,
85
+ hits: i.slice(0, n),
86
+ total: ((g = s.products) == null ? void 0 : g.total) || 0
87
+ }
88
+ };
89
+ const u = n - i.length, a = o > 0 ? o + 1 : n - u, f = {
90
+ ...t,
91
+ products: {
92
+ ...t.products,
93
+ from: a,
94
+ size: u
95
+ }
96
+ }, p = await r(f, e);
97
+ return {
98
+ ...s,
99
+ products: {
100
+ ...s.products,
101
+ size: n,
102
+ hits: [...((S = s.products) == null ? void 0 : S.hits) || [], ...((m = p.products) == null ? void 0 : m.hits) || []],
103
+ total: ((w = p.products) == null ? void 0 : w.total) || 0
104
+ }
105
+ };
106
+ }
107
+ function A(t) {
108
+ return new Promise((e) => setTimeout(e, t));
109
+ }
110
+ async function P(t, { maxRetries: e = 0, retryInterval: r = 0, ...o }, n) {
111
+ let s = 0;
112
+ for (; ; )
113
+ try {
114
+ return await n(t, o);
115
+ } catch (c) {
116
+ if (s >= e)
117
+ throw c;
118
+ if (!_(c))
119
+ throw l.info("Skipping retry logic for", c), c;
120
+ s++, await A(r);
121
+ }
122
+ }
123
+ function _(t) {
124
+ return !t || typeof t != "object" ? !1 : !("status" in t) || x(t.status);
125
+ }
126
+ function x(t) {
127
+ return typeof t == "number" && (t < 400 || t >= 500);
128
+ }
129
+ async function K(t, e = {}) {
130
+ const r = await new Promise(b);
131
+ return F(r.search, P, j, N)(t, e);
132
+ }
133
+ function F(t, ...e) {
134
+ return e.reduce((r, o) => (n, s) => o(n, s, r), t);
135
+ }
136
+ export {
137
+ K as s
138
+ };
@@ -1 +1 @@
1
- "use strict";const y=require("./configContext-BR7w4VlU.cjs"),n=require("preact/hooks"),M=require("./search-DJuT47P8.cjs"),g=require("./unique-Blp-K9zG.cjs"),h=require("./deepMerge-lDscDfII.cjs"),v=require("./logger-BcHMZObS.cjs");function T(e,t){return{...t,products:{facets:e==="autocomplete"?void 0:["*"],fields:A,...t.products},...t.keywords?{keywords:{...t.keywords,fields:g.unique([...I,...t.keywords.fields??[]]),highlight:t.keywords.highlight??{preTag:"<strong>",postTag:"</strong>"}}}:{}}}const A=["productId","url","name","imageUrl","imageHash","thumbUrl","description","brand","variantId","availability","price","priceText","categoryIds","categories","customFields.key","customFields.value","priceCurrencyCode","datePublished","listPrice","unitPricingBaseMeasure","unitPricingUnit","unitPricingMeasure","googleCategory","gtin","ageGroup","gender","condition","alternateImageUrls","ratingValue","reviewCount","inventoryLevel","skus.id","skus.name","skus.price","skus.listPrice","skus.priceText","skus.url","skus.imageUrl","skus.inventoryLevel","skus.customFields.key","skus.customFields.value","skus.availability","pid","onDiscount","extra.key","extra.value","saleable","available","tags1","tags2","tags3"],I=["keyword","_redirect"];async function k(e,t,r){var d,l,S,m,C,w,b;const o=g.measure("newSearch"),c=e.config.pageType,u=c==="search"?"serp":c,a=h.deepMerge(e.store.getInitialState().query,t),s=h.deepMerge(e.config.search,r,{track:u,redirect:c!=="autocomplete",isKeyword:!!(r!=null&&r.isKeyword),usePersistentCache:e.config.pageType!=="autocomplete"&&e.config.persistentSearchCache});(l=(d=e.config).onBeforeSearch)==null||l.call(d,e,s),e.store.updateState({query:a,loading:!0,initialized:!0});const i=e.config.queryModifications({...a,products:{...a.products,filter:g.mergeArrays((m=(S=e.store.getInitialState().query)==null?void 0:S.products)==null?void 0:m.filter,(C=t.products)==null?void 0:C.filter)}},c);try{const p=await M.search(T(e.config.pageType,i),s);e.store.updateState({response:p,loading:!1})}catch(p){v.logger.error("Search action failed",p),(b=(w=e.config).onSearchError)==null||b.call(w,p,i,s,c)}o()}async function f(e,t,r){const o=g.measure("updateSearch"),c=h.deepMerge(e.store.getState().query,{products:{from:0}},t);await k(e,c,r),o()}function P(e=Q){const t=n.useContext(y.StoreContext),[r,o]=n.useState(e(t.getState()));return t.onChange(e,o),n.useEffect(()=>()=>t.clearOnChange(o),[t]),r}const Q=e=>e;async function U(e,t,r){var a;const o=g.measure("replaceFilter"),c=(a=e.store.getState().query.products)==null?void 0:a.filter,u=r!==void 0?[{field:t,[typeof r=="object"?"range":"value"]:[r]}]:[];await f(e,{products:{filter:[...(c==null?void 0:c.filter(s=>s.field!==t))??[],...u]}}),o()}async function L(e,t,r,o){var i,d;const c=g.measure("toggleProductFilter"),u=(i=e.store.getState().query.products)==null?void 0:i.filter,a=u==null?void 0:u.find(l=>l.value instanceof Array&&l.field===t),s=a!=null&&a.value?{...a,value:[...a.value.filter(l=>!v.isEqual(l,r)),...o?[r]:[]]}:o?{field:t,value:[r]}:void 0;await f(e,{products:{filter:[...(u==null?void 0:u.filter(l=>l!==a))??[],...(d=s==null?void 0:s.value)!=null&&d.length?[s]:[]]}}),c()}function F(){const e=y.useConfig(),t=n.useContext(y.StoreContext),r=n.useMemo(()=>({config:e,store:t}),[e,t]),o=n.useCallback((s,i)=>k(r,s,i),[r]),c=n.useCallback((s,i)=>f(r,s,i),[r]),u=n.useCallback((s,i,d)=>L(r,s,i,d),[r]),a=n.useCallback((s,i)=>U(r,s,i),[r]);return{newSearch:o,updateSearch:c,toggleProductFilter:u,replaceFilter:a}}function q({from:e,size:t,pageSize:r}){return g.isBot()?{products:{from:e+r}}:{products:{size:t+r}}}function N(e=24){const{from:t,size:r}=P(u=>{var a,s;return{from:((a=u.query.products)==null?void 0:a.from)??0,size:((s=u.query.products)==null?void 0:s.size)??0}}),{updateSearch:o}=F();return{loadMore:n.useCallback(async()=>{await o(q({from:t,size:r,pageSize:e}))},[t,r,e,o])}}exports.getNextPageQuery=q;exports.newSearch=k;exports.updateSearch=f;exports.useActions=F;exports.useLoadMore=N;exports.useNostoAppState=P;
1
+ "use strict";const y=require("./configContext-BR7w4VlU.cjs"),n=require("preact/hooks"),M=require("./search-DapMuqyR.cjs"),g=require("./unique-Blp-K9zG.cjs"),h=require("./deepMerge-lDscDfII.cjs"),v=require("./logger-BcHMZObS.cjs");function T(e,t){return{...t,products:{facets:e==="autocomplete"?void 0:["*"],fields:A,...t.products},...t.keywords?{keywords:{...t.keywords,fields:g.unique([...I,...t.keywords.fields??[]]),highlight:t.keywords.highlight??{preTag:"<strong>",postTag:"</strong>"}}}:{}}}const A=["productId","url","name","imageUrl","imageHash","thumbUrl","description","brand","variantId","availability","price","priceText","categoryIds","categories","customFields.key","customFields.value","priceCurrencyCode","datePublished","listPrice","unitPricingBaseMeasure","unitPricingUnit","unitPricingMeasure","googleCategory","gtin","ageGroup","gender","condition","alternateImageUrls","ratingValue","reviewCount","inventoryLevel","skus.id","skus.name","skus.price","skus.listPrice","skus.priceText","skus.url","skus.imageUrl","skus.inventoryLevel","skus.customFields.key","skus.customFields.value","skus.availability","pid","onDiscount","extra.key","extra.value","saleable","available","tags1","tags2","tags3"],I=["keyword","_redirect"];async function k(e,t,r){var d,l,S,m,C,w,b;const o=g.measure("newSearch"),c=e.config.pageType,u=c==="search"?"serp":c,a=h.deepMerge(e.store.getInitialState().query,t),s=h.deepMerge(e.config.search,r,{track:u,redirect:c!=="autocomplete",isKeyword:!!(r!=null&&r.isKeyword),usePersistentCache:e.config.pageType!=="autocomplete"&&e.config.persistentSearchCache});(l=(d=e.config).onBeforeSearch)==null||l.call(d,e,s),e.store.updateState({query:a,loading:!0,initialized:!0});const i=e.config.queryModifications({...a,products:{...a.products,filter:g.mergeArrays((m=(S=e.store.getInitialState().query)==null?void 0:S.products)==null?void 0:m.filter,(C=t.products)==null?void 0:C.filter)}},c);try{const p=await M.search(T(e.config.pageType,i),s);e.store.updateState({response:p,loading:!1})}catch(p){v.logger.error("Search action failed",p),(b=(w=e.config).onSearchError)==null||b.call(w,p,i,s,c)}o()}async function f(e,t,r){const o=g.measure("updateSearch"),c=h.deepMerge(e.store.getState().query,{products:{from:0}},t);await k(e,c,r),o()}function P(e=Q){const t=n.useContext(y.StoreContext),[r,o]=n.useState(e(t.getState()));return t.onChange(e,o),n.useEffect(()=>()=>t.clearOnChange(o),[t]),r}const Q=e=>e;async function U(e,t,r){var a;const o=g.measure("replaceFilter"),c=(a=e.store.getState().query.products)==null?void 0:a.filter,u=r!==void 0?[{field:t,[typeof r=="object"?"range":"value"]:[r]}]:[];await f(e,{products:{filter:[...(c==null?void 0:c.filter(s=>s.field!==t))??[],...u]}}),o()}async function L(e,t,r,o){var i,d;const c=g.measure("toggleProductFilter"),u=(i=e.store.getState().query.products)==null?void 0:i.filter,a=u==null?void 0:u.find(l=>l.value instanceof Array&&l.field===t),s=a!=null&&a.value?{...a,value:[...a.value.filter(l=>!v.isEqual(l,r)),...o?[r]:[]]}:o?{field:t,value:[r]}:void 0;await f(e,{products:{filter:[...(u==null?void 0:u.filter(l=>l!==a))??[],...(d=s==null?void 0:s.value)!=null&&d.length?[s]:[]]}}),c()}function F(){const e=y.useConfig(),t=n.useContext(y.StoreContext),r=n.useMemo(()=>({config:e,store:t}),[e,t]),o=n.useCallback((s,i)=>k(r,s,i),[r]),c=n.useCallback((s,i)=>f(r,s,i),[r]),u=n.useCallback((s,i,d)=>L(r,s,i,d),[r]),a=n.useCallback((s,i)=>U(r,s,i),[r]);return{newSearch:o,updateSearch:c,toggleProductFilter:u,replaceFilter:a}}function q({from:e,size:t,pageSize:r}){return g.isBot()?{products:{from:e+r}}:{products:{size:t+r}}}function N(e=24){const{from:t,size:r}=P(u=>{var a,s;return{from:((a=u.query.products)==null?void 0:a.from)??0,size:((s=u.query.products)==null?void 0:s.size)??0}}),{updateSearch:o}=F();return{loadMore:n.useCallback(async()=>{await o(q({from:t,size:r,pageSize:e}))},[t,r,e,o])}}exports.getNextPageQuery=q;exports.newSearch=k;exports.updateSearch=f;exports.useActions=F;exports.useLoadMore=N;exports.useNostoAppState=P;
@@ -1,6 +1,6 @@
1
1
  import { S as C, u as F } from "./configContext-GuZOIX3o.js";
2
2
  import { useContext as v, useState as P, useEffect as M, useMemo as T, useCallback as d } from "preact/hooks";
3
- import { s as I } from "./search-CIiuO9FB.js";
3
+ import { s as I } from "./search-PYPD19kM.js";
4
4
  import { u as q, a as p, m as Q, i as U } from "./unique-m1TIDWdl.js";
5
5
  import { d as f } from "./deepMerge-CZwCJzEe.js";
6
6
  import { l as A, i as z } from "./logger-DVwg4Wor.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nosto/search-js",
3
- "version": "2.12.1",
3
+ "version": "2.13.0",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "files": [
@@ -90,11 +90,11 @@
90
90
  "@testing-library/dom": "^10.4.0",
91
91
  "@types/dom-speech-recognition": "^0.0.6",
92
92
  "@types/eslint-config-prettier": "^6.11.3",
93
- "@types/node": "^22.15.17",
94
- "@vitest/coverage-v8": "^3.1.3",
93
+ "@types/node": "^22.15.19",
94
+ "@vitest/coverage-v8": "^3.1.4",
95
95
  "concurrently": "^9.1.2",
96
96
  "copyfiles": "^2.4.1",
97
- "eslint": "^9.26.0",
97
+ "eslint": "^9.27.0",
98
98
  "eslint-config-prettier": "^10.1.5",
99
99
  "eslint-plugin-barrel-files": "^3.0.1",
100
100
  "eslint-plugin-prettier": "^5.4.0",
@@ -108,9 +108,9 @@
108
108
  "prettier": "^3.5.3",
109
109
  "typedoc": "^0.28.4",
110
110
  "typescript": "^5.8.3",
111
- "typescript-eslint": "^8.32.0",
111
+ "typescript-eslint": "^8.32.1",
112
112
  "vite": "^6.3.5",
113
- "vite-plugin-dts": "^4.5.3",
113
+ "vite-plugin-dts": "^4.5.4",
114
114
  "vitest": "^3.1.3"
115
115
  },
116
116
  "publishConfig": {
@@ -1,2 +0,0 @@
1
- import { SwatchField } from '../types';
2
- export declare function applySwatchSelectionStates(swatches: SwatchField[], selectedOptions: Record<string, string>): SwatchField[];
@@ -1,138 +0,0 @@
1
- import { s as y } from "./index.es-B8mbAxS4.js";
2
- import { l, i as R } from "./logger-DVwg4Wor.js";
3
- async function b(t, { hitDecorators: e, ...n }, o) {
4
- var c, i;
5
- const r = await o(t, n);
6
- if (!((i = (c = r.products) == null ? void 0 : c.hits) != null && i.length) || !(e != null && e.length))
7
- return r;
8
- const s = (a) => e.reduce((f, u) => u(f), a);
9
- return {
10
- ...r,
11
- products: {
12
- ...r.products,
13
- hits: r.products.hits.map(s)
14
- }
15
- };
16
- }
17
- function N(t, e, n) {
18
- const o = JSON.stringify(e);
19
- try {
20
- n.setItem(t, o);
21
- } catch (r) {
22
- l.warn(r);
23
- }
24
- }
25
- function k(t, e) {
26
- try {
27
- const n = e.getItem(t);
28
- if (n)
29
- return JSON.parse(n);
30
- } catch (n) {
31
- l.warn(n);
32
- }
33
- }
34
- function C(t, e) {
35
- N(t, e, sessionStorage);
36
- }
37
- function O(t) {
38
- return k(t, sessionStorage);
39
- }
40
- const w = "nosto:search-js:cache", T = 60 * 1e3;
41
- function z(t, e) {
42
- C(w, { query: t, result: e, created: Date.now() });
43
- }
44
- function E(t) {
45
- const e = O(w);
46
- if (!e || !I(e))
47
- return null;
48
- const n = m(e.query);
49
- return !R(m(t), n) || Date.now() - e.created > T ? null : e.result;
50
- }
51
- function m(t) {
52
- const e = {
53
- ...t,
54
- time: void 0,
55
- products: {
56
- ...t.products,
57
- size: void 0
58
- }
59
- };
60
- return JSON.parse(JSON.stringify(e));
61
- }
62
- function I(t) {
63
- return typeof t == "object" && t !== null && "query" in t && "result" in t && "created" in t;
64
- }
65
- async function J(t, { usePersistentCache: e, ...n }, o) {
66
- if (!e)
67
- return o(t, n);
68
- const r = await V(t, n, o);
69
- return z(t, r), r;
70
- }
71
- async function V(t, e, n) {
72
- var p, d, h, g, S;
73
- const { from: o = 0, size: r = 0 } = t.products || {}, s = E(t);
74
- if (!s)
75
- return await n(t, e);
76
- const c = ((p = s == null ? void 0 : s.products) == null ? void 0 : p.hits) || [];
77
- if (r === c.length)
78
- return s;
79
- if (r < c.length)
80
- return {
81
- ...s,
82
- products: {
83
- ...s.products,
84
- size: r,
85
- hits: c.slice(0, r),
86
- total: ((d = s.products) == null ? void 0 : d.total) || 0
87
- }
88
- };
89
- const i = r - c.length, a = o > 0 ? o + 1 : r - i, f = {
90
- ...t,
91
- products: {
92
- ...t.products,
93
- from: a,
94
- size: i
95
- }
96
- }, u = await n(f, e);
97
- return {
98
- ...s,
99
- products: {
100
- ...s.products,
101
- size: r,
102
- hits: [...((h = s.products) == null ? void 0 : h.hits) || [], ...((g = u.products) == null ? void 0 : g.hits) || []],
103
- total: ((S = u.products) == null ? void 0 : S.total) || 0
104
- }
105
- };
106
- }
107
- function j(t) {
108
- return new Promise((e) => setTimeout(e, t));
109
- }
110
- async function W(t, { maxRetries: e = 0, retryInterval: n = 0, ...o }, r) {
111
- let s = 0;
112
- for (; ; )
113
- try {
114
- return await r(t, o);
115
- } catch (c) {
116
- if (s >= e)
117
- throw c;
118
- if (!A(c))
119
- throw l.info("Skipping retry logic for", c), c;
120
- s++, await j(n);
121
- }
122
- }
123
- function A(t) {
124
- return !t || typeof t != "object" ? !1 : !("status" in t) || P(t.status);
125
- }
126
- function P(t) {
127
- return typeof t == "number" && (t < 400 || t >= 500);
128
- }
129
- async function G(t, e = {}) {
130
- const n = await new Promise(y);
131
- return _(n.search, W, J, b)(t, e);
132
- }
133
- function _(t, ...e) {
134
- return e.reduce((n, o) => (r, s) => o(r, s, n), t);
135
- }
136
- export {
137
- G as s
138
- };
@@ -1 +0,0 @@
1
- "use strict";const m=require("./index.es-DlUp67LT.cjs"),a=require("./logger-BcHMZObS.cjs");async function R(t,{hitDecorators:e,...r},c){var o,i;const n=await c(t,r);if(!((i=(o=n.products)==null?void 0:o.hits)!=null&&i.length)||!(e!=null&&e.length))return n;const s=f=>e.reduce((l,u)=>u(l),f);return{...n,products:{...n.products,hits:n.products.hits.map(s)}}}function b(t,e,r){const c=JSON.stringify(e);try{r.setItem(t,c)}catch(n){a.logger.warn(n)}}function N(t,e){try{const r=e.getItem(t);if(r)return JSON.parse(r)}catch(r){a.logger.warn(r)}}function k(t,e){b(t,e,sessionStorage)}function C(t){return N(t,sessionStorage)}const y="nosto:search-js:cache",O=60*1e3;function T(t,e){k(y,{query:t,result:e,created:Date.now()})}function z(t){const e=C(y);if(!e||!E(e))return null;const r=w(e.query);return!a.isEqual(w(t),r)||Date.now()-e.created>O?null:e.result}function w(t){const e={...t,time:void 0,products:{...t.products,size:void 0}};return JSON.parse(JSON.stringify(e))}function E(t){return typeof t=="object"&&t!==null&&"query"in t&&"result"in t&&"created"in t}async function I(t,{usePersistentCache:e,...r},c){if(!e)return c(t,r);const n=await J(t,r,c);return T(t,n),n}async function J(t,e,r){var d,p,g,h,S;const{from:c=0,size:n=0}=t.products||{},s=z(t);if(!s)return await r(t,e);const o=((d=s==null?void 0:s.products)==null?void 0:d.hits)||[];if(n===o.length)return s;if(n<o.length)return{...s,products:{...s.products,size:n,hits:o.slice(0,n),total:((p=s.products)==null?void 0:p.total)||0}};const i=n-o.length,f=c>0?c+1:n-i,l={...t,products:{...t.products,from:f,size:i}},u=await r(l,e);return{...s,products:{...s.products,size:n,hits:[...((g=s.products)==null?void 0:g.hits)||[],...((h=u.products)==null?void 0:h.hits)||[]],total:((S=u.products)==null?void 0:S.total)||0}}}function V(t){return new Promise(e=>setTimeout(e,t))}async function j(t,{maxRetries:e=0,retryInterval:r=0,...c},n){let s=0;for(;;)try{return await n(t,c)}catch(o){if(s>=e)throw o;if(!W(o))throw a.logger.info("Skipping retry logic for",o),o;s++,await V(r)}}function W(t){return!t||typeof t!="object"?!1:!("status"in t)||_(t.status)}function _(t){return typeof t=="number"&&(t<400||t>=500)}async function q(t,e={}){const r=await new Promise(m.s);return A(r.search,j,I,R)(t,e)}function A(t,...e){return e.reduce((r,c)=>(n,s)=>c(n,s,r),t)}exports.search=q;