!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["react-horizontal-scrolling-menu"]={},e.React)}(this,(function(e,t){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=n(t);const r="react-horizontal-scrolling-menu",s=`${r}--item`,i=`${r}--scroll-container`,l=`${r}--wrapper`,a=`${r}--inner-wrapper`,u=`${r}--header`,d=`${r}--arrow-left`,c=`${r}--arrow-right`,f=`${r}--footer`,h="itemId",v="data-key",m="data-index",p={first:"first",last:"last",onInit:"onInit",onUpdate:"onUpdate"},b="",g={current:null};var y=Object.freeze({__proto__:null,rootClassName:r,itemClassName:s,scrollContainerClassName:i,wrapperClassName:l,innerWrapperClassName:a,headerClassName:u,arrowLeftClassName:d,arrowRightClassName:c,footerClassName:f,id:h,dataKeyAttribute:v,dataIndexAttribute:m,events:p,emptyStr:b,emptyRef:g});class w{constructor(){this.subscribe=(e,t)=>{this.observers.set(e,(this.observers.get(e)||[]).concat(t))},this.unsubscribe=(e,t)=>{const n=(this.observers.get(e)||[]).filter((e=>e!==t));n.length?this.observers.set(e,n):this.observers.delete(e)},this.emitUpdates=(e,t)=>{const n=this.observers.get(e)||[];null==n||n.forEach((e=>e(t)))},this.updateBatch=(e,t=!0)=>{e.length&&(e.forEach((([e,t])=>this.emitUpdates(e,t))),t&&this.emitUpdates(p.onUpdate))},this.update=(e,t)=>{this.emitUpdates(e,t),this.emitUpdates(p.onUpdate)},this.observers=new Map}}class M extends Map{constructor(){super(),this.subscribe=(e,t)=>this.observer.subscribe(e,t),this.unsubscribe=(e,t)=>this.observer.unsubscribe(e,t),this.isEdgeItem=({key:e,value:t,first:n=this.first(),last:o=this.last()})=>{const r=[];return e===(null==n?void 0:n.key)?r.push([p.first,t]):e===(null==o?void 0:o.key)&&r.push([p.last,t]),r},this.edgeItemsCheck=e=>{const t=this.first(),n=this.last(),o=e.find((([e])=>e===(null==t?void 0:t.key))),r=[];o&&r.push([p.first,o[1]]);const s=e.find((([e])=>e===(null==n?void 0:n.key)));return s&&r.push([p.last,s[1]]),r},this.toArr=()=>this.sort([...this]),this.toItems=()=>this.toArr().map((([e])=>e)),this.sort=e=>e.sort((([,e],[,t])=>+e.index-+t.index)),this.set=(e,t)=>{const n=String(e),o=[[n,t]];return super.set(n,t),o.push(...this.isEdgeItem({key:n,value:t,first:this.first(),last:this.last()})),this.observer.updateBatch(o),this},this.setBatch=e=>{this.firstRun&&this.observer.update(p.onInit);const t=[...e];return this.sort(t).forEach((([e,t])=>{super.set(String(e),t)})),t.push(...this.edgeItemsCheck(t)),this.observer.updateBatch(t,!this.firstRun),this.firstRun=!1,this},this.first=()=>{var e;return null===(e=this.toArr()[0])||void 0===e?void 0:e[1]},this.last=()=>{var e,t;return null===(t=null===(e=this.toArr().slice(-1))||void 0===e?void 0:e[0])||void 0===t?void 0:t[1]},this.filter=e=>this.toArr().filter(e),this.find=e=>this.toArr().find(e),this.findIndex=e=>this.toArr().findIndex(e),this.getCurrentPos=e=>{const t=this.toArr(),n=t.findIndex((([t,n])=>t===e||n===e));return[t,n]},this.prev=e=>{var t;const[n,o]=this.getCurrentPos(e);return-1!==o?null===(t=n[o-1])||void 0===t?void 0:t[1]:void 0},this.next=e=>{var t;const[n,o]=this.getCurrentPos(e);return-1!==o?null===(t=n[o+1])||void 0===t?void 0:t[1]:void 0},this.getVisible=()=>this.filter((e=>e[1].visible)),this.observer=new w,this.firstRun=!0}}const E=e=>"object"==typeof e&&null!=e&&1===e.nodeType,C=(e,t)=>(!t||"hidden"!==e)&&"visible"!==e&&"clip"!==e,I=(e,t)=>{if(e.clientHeight{const t=(e=>{if(!e.ownerDocument||!e.ownerDocument.defaultView)return null;try{return e.ownerDocument.defaultView.frameElement}catch(e){return null}})(e);return!!t&&(t.clientHeightst||s>e&&i=t&&l>=n?s-e-o:i>t&&ln?i-t+r:0,k=e=>{const t=e.parentElement;return null==t?e.getRootNode().host||null:t},N=(e,t)=>{var n,o,r,s;if("undefined"==typeof document)return[];const{scrollMode:i,block:l,inline:a,boundary:u,skipOverflowHiddenElements:d}=t,c="function"==typeof u?u:e=>e!==u;if(!E(e))throw new TypeError("Invalid target");const f=document.scrollingElement||document.documentElement,h=[];let v=e;for(;E(v)&&c(v);){if(v=k(v),v===f){h.push(v);break}null!=v&&v===document.body&&I(v)&&!I(document.documentElement)||null!=v&&I(v,d)&&h.push(v)}const m=null!=(o=null==(n=window.visualViewport)?void 0:n.width)?o:innerWidth,p=null!=(s=null==(r=window.visualViewport)?void 0:r.height)?s:innerHeight,{scrollX:b,scrollY:g}=window,{height:y,width:w,top:M,right:C,bottom:N,left:S}=e.getBoundingClientRect(),{top:R,right:j,bottom:O,left:T}=(e=>{const t=window.getComputedStyle(e);return{top:parseFloat(t.scrollMarginTop)||0,right:parseFloat(t.scrollMarginRight)||0,bottom:parseFloat(t.scrollMarginBottom)||0,left:parseFloat(t.scrollMarginLeft)||0}})(e);let $="start"===l||"nearest"===l?M-R:"end"===l?N+O:M+y/2-R+O,W="center"===a?S+w/2-T+j:"end"===a?C+j:S-T;const B=[];for(let e=0;e=0&&S>=0&&N<=p&&C<=m&&M>=r&&N<=u&&S>=d&&C<=s)return B;const c=getComputedStyle(t),v=parseInt(c.borderLeftWidth,10),E=parseInt(c.borderTopWidth,10),I=parseInt(c.borderRightWidth,10),k=parseInt(c.borderBottomWidth,10);let R=0,j=0;const O="offsetWidth"in t?t.offsetWidth-t.clientWidth-v-I:0,T="offsetHeight"in t?t.offsetHeight-t.clientHeight-E-k:0,V="offsetWidth"in t?0===t.offsetWidth?0:o/t.offsetWidth:0,A="offsetHeight"in t?0===t.offsetHeight?0:n/t.offsetHeight:0;if(f===t)R="start"===l?$:"end"===l?$-p:"nearest"===l?x(g,g+p,p,E,k,g+$,g+$+y,y):$-p/2,j="start"===a?W:"center"===a?W-m/2:"end"===a?W-m:x(b,b+m,m,v,I,b+W,b+W+w,w),R=Math.max(0,R+g),j=Math.max(0,j+b);else{R="start"===l?$-r-E:"end"===l?$-u+k+T:"nearest"===l?x(r,u,n,E,k+T,$,$+y,y):$-(r+n/2)+T/2,j="start"===a?W-d-v:"center"===a?W-(d+o/2)+O/2:"end"===a?W-s+I+O:x(d,s,o,v,I+O,W,W+w,w);const{scrollLeft:e,scrollTop:i}=t;R=0===A?0:Math.max(0,Math.min(i+R/A,t.scrollHeight-n/A+T)),j=0===V?0:Math.max(0,Math.min(e+j/V,t.scrollWidth-o/V+O)),$+=i-R,W+=e-j}B.push({el:t,top:R,left:j})}return B};function S(e,t){if(!e.isConnected||!(e=>{let t=e;for(;t&&t.parentNode;){if(t.parentNode===document)return!0;t=t.parentNode instanceof ShadowRoot?t.parentNode.host:t.parentNode}return!1})(e))return;const n=(e=>{const t=window.getComputedStyle(e);return{top:parseFloat(t.scrollMarginTop)||0,right:parseFloat(t.scrollMarginRight)||0,bottom:parseFloat(t.scrollMarginBottom)||0,left:parseFloat(t.scrollMarginLeft)||0}})(e);if((e=>"object"==typeof e&&"function"==typeof e.behavior)(t))return t.behavior(N(e,t));const o="boolean"==typeof t||null==t?void 0:t.behavior;for(const{el:r,top:s,left:i}of N(e,(e=>!1===e?{block:"end",inline:"nearest"}:(e=>e===Object(e)&&0!==Object.keys(e).length)(e)?e:{block:"start",inline:"nearest"})(t))){const e=s-n.top+n.bottom,t=i-n.left+n.right;r.scroll({top:e,left:t,behavior:o})}}let R;const j=()=>(R||(R="performance"in window?performance.now.bind(performance):Date.now),R());function O(e){const t=j(),n=Math.min((t-e.startTime)/e.duration,1),o=e.ease(n),r=e.startX+(e.x-e.startX)*o,s=e.startY+(e.y-e.startY)*o;e.method(r,s,n,o),r!==e.x||s!==e.y?requestAnimationFrame((()=>O(e))):e.cb()}function T(e,t,n){let o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:600,r=arguments.length>4&&void 0!==arguments[4]?arguments[4]:e=>1+--e*e*e*e*e,s=arguments.length>5?arguments[5]:void 0,i=arguments.length>6?arguments[6]:void 0;const l=e,a=e.scrollLeft,u=e.scrollTop;O({scrollable:l,method:(t,n,o,r)=>{const s=Math.ceil(t),l=Math.ceil(n);e.scrollLeft=s,e.scrollTop=l,null==i||i({target:e,elapsed:o,value:r,left:s,top:l})},startTime:j(),startX:a,startY:u,x:t,y:n,duration:o,ease:r,cb:s})}const $=function(e,t){const n=t||{};return(e=>e&&!e.behavior||"smooth"===e.behavior)(n)?S(e,{block:n.block,inline:n.inline,scrollMode:n.scrollMode,boundary:n.boundary,skipOverflowHiddenElements:n.skipOverflowHiddenElements,behavior:e=>Promise.all(e.reduce(((e,t)=>{let{el:o,left:r,top:s}=t;const i=o.scrollLeft,l=o.scrollTop;return i===r&&l===s?e:[...e,new Promise((e=>T(o,r,s,n.duration,n.ease,(()=>e({el:o,left:[i,r],top:[l,s]})),n.onScrollChange)))]}),[]))}):Promise.resolve(S(e,t))},W=e=>Object.values(e).map((e=>e.current)).filter(Boolean);function B(e,t,n,o,r,s){var i;const l=(null===(i=null==e?void 0:e.entry)||void 0===i?void 0:i.target)||e;if(!l)return;const a={behavior:t||"smooth",inline:n||"end",block:o||"nearest"};return s?l.scrollIntoView(a):$(l,Object.assign(Object.assign({},r),a))}const V=e=>document.querySelector(`[${v}='${e}']`),A=e=>document.querySelector(`[${m}='${e}']`);function L(e){return o.default.isValidElement(e)&&e||"function"==typeof e&&o.default.createElement(e,null)||!!e&&"object"==typeof e&&o.default.createElement(e,null)||null}const H=e=>{var t;return String((null===(t=null==e?void 0:e.props)||void 0===t?void 0:t[h])||((null==e?void 0:e.key)||b).replace(/^\.\$/,b))};function U(e){return!!e&&Object.prototype.hasOwnProperty.call(e,"current")}var P=o.default.memo((function({children:e,className:t,id:n,index:r,refs:s}){const i=o.default.useRef(null);return s[String(r)]=i,o.default.createElement("div",{className:t,[v]:n,[m]:r,ref:i},e)}));function F({children:e,itemClassName:t=b,refs:n}){const r=o.default.Children.toArray(e).filter(Boolean),i=o.default.useMemo((()=>`${s} ${t}`),[t]);return r.map(((e,t)=>{const r=H(e);return o.default.createElement(P,{className:i,id:r,key:r,refs:n,index:t},e)}))}function D({className:e=b,children:t,onScroll:n=(()=>{}),scrollRef:r,containerRef:s}){const l=o.default.useMemo((()=>`${i} ${e}`),[e]),a=o.default.useCallback((e=>{U(r)?r.current=e:r(e),U(s)?s.current=e:s(e)}),[r,s]);return o.default.createElement("div",{className:l,onScroll:n,ref:a},t)}const _=o.default.createContext({});const X="undefined"!=typeof window?o.default.useLayoutEffect:o.default.useEffect;function Y({items:e,itemsChanged:t,refs:n,options:r}){const s=o.default.useRef(),i=o.default.useCallback((t=>{e.setBatch(function(e,t){return[...e].map((e=>{var n,o,r,s;const i=e.target,l=String(null!==(o=null===(n=null==i?void 0:i.dataset)||void 0===n?void 0:n.key)&&void 0!==o?o:b);return[l,{index:String(null!==(s=null===(r=null==i?void 0:i.dataset)||void 0===r?void 0:r.index)&&void 0!==s?s:b),key:l,entry:e,visible:e.intersectionRatio>=t.ratio}]}))}(t,r))}),[e,r]);X((()=>{const e=W(n),t=s.current||new IntersectionObserver(i,r);return s.current=t,e.forEach((e=>t.observe(e))),()=>{t.disconnect(),s.current=void 0}}),[i,t,r,n])}const q=e=>o.default.Children.toArray(e).map(H).filter(Boolean);const z={ratio:.9,rootMargin:"5px",threshold:[.05,.5,.75,.95]};const K={current:{}},G=()=>{};e.ScrollMenu=function({LeftArrow:e,RightArrow:t,children:n,Header:r,Footer:s,transitionDuration:i=500,transitionBehavior:h,onInit:v=G,onUpdate:m=G,onMouseDown:y,onMouseLeave:w,onMouseUp:E,onMouseMove:C,onScroll:I=G,onTouchMove:x,onTouchStart:k,onTouchEnd:N,onWheel:S=G,options:R=z,scrollContainerClassName:j=b,containerRef:O=g,itemClassName:T=b,wrapperClassName:$=b,apiRef:W=K,RTL:H,noPolyfill:P=!0}){const J=L(e),Q=L(t),Z=L(r),ee=L(s),te=o.default.useRef(null),[ne]=o.default.useState({}),oe=o.default.useMemo((()=>Object.assign(Object.assign(Object.assign({},z),R),{root:te.current})),[R]),re=o.default.useRef(new M).current,se=function(e,t){const[n,r]=o.default.useState(b),s=o.default.useMemo((()=>q(e)),[e]);return o.default.useEffect((()=>{const e=s.filter(Boolean).join(b);t.toItems().filter((e=>!s.includes(e))).forEach((e=>{t.delete(e)})),r(e)}),[s,t]),n}(n,re),ie=((e,t)=>{const n=o.default.useRef(!0),r=o.default.useMemo((()=>[t-.05,t-.01,t,t+.01,t+.05]),[t]),s=o.default.useCallback((e=>{var o;n.current=(null===(o=null==e?void 0:e[0])||void 0===o?void 0:o.intersectionRatio)>=t}),[t]);return X((()=>{const t=new IntersectionObserver(s,{threshold:r,rootMargin:"0px"});return e.current&&t.observe(e.current),()=>t.disconnect()}),[n,e,s,r]),n})(te,oe.ratio+.05<1?oe.ratio+.05:.95);Y(o.default.useMemo((()=>({items:re,itemsChanged:se,options:oe,refs:ne})),[re,se,ne,oe]));const le=o.default.useMemo((()=>function(e,t,n,r){var s,i,l;const a=(t,n=!1)=>{const[r,s]=o.default.useState(n),i=o.default.useCallback((e=>{s(!!(null==e?void 0:e.visible))}),[]);return o.default.useEffect((()=>(e.subscribe(t,i),()=>{e.unsubscribe(t,i)})),[t,i]),r},u=!!(null===(s=e.first())||void 0===s?void 0:s.visible),d=!!(null===(i=e.last())||void 0===i?void 0:i.visible),c=t=>{var n;return null===(n=e.find((e=>e[1].key===String(t))))||void 0===n?void 0:n[1]},f=()=>{var t,n;const o=null===(n=null===(t=e.getVisible())||void 0===t?void 0:t[0])||void 0===n?void 0:n[1];return o?e.prev(o):void 0},h=()=>{var t;const n=null===(t=e.getVisible().findLast((()=>!0)))||void 0===t?void 0:t[1];return n?e.next(n):void 0},v=null===(l=null==n?void 0:n.boundary)||void 0===l?void 0:l.current;return{getItemById:c,getItemElementById:V,getItemByIndex:t=>{var n;return null===(n=e.find((e=>String(e[1].index)===String(t))))||void 0===n?void 0:n[1]},getItemElementByIndex:A,getNextElement:h,getPrevElement:f,isFirstItemVisible:u,isItemVisible:t=>e.getVisible().map((e=>e[0])).includes(String(t)),isLastItem:t=>e.last()===c(t),isLastItemVisible:d,scrollNext:(e,t,o,{duration:s,boundary:i=v}={})=>{const l=null!=e?e:null==n?void 0:n.behavior;return B(h(),l,t||"start",o||"nearest",{boundary:i,duration:null!=s?s:null==n?void 0:n.duration},r)},scrollPrev:(e,t,o,{duration:s,boundary:i=v}={})=>{const l=null!=e?e:null==n?void 0:n.behavior;return B(f(),l,t||"end",o||"nearest",{boundary:i,duration:null!=s?s:null==n?void 0:n.duration},r)},useIsVisible:a,useLeftArrowVisible:()=>{const e=a("first",!0),[n,r]=o.default.useState(e);return o.default.useEffect((()=>{t.current&&r(e)}),[e]),n},useRightArrowVisible:()=>{const e=a("last",!1),[n,r]=o.default.useState(e);return o.default.useEffect((()=>{t.current&&r(e)}),[e]),n},scrollToItem:(e,t,o,s,i)=>{var l;return B(e,null!=t?t:null==n?void 0:n.behavior,o,s,Object.assign(Object.assign({boundary:v},i),{duration:null!==(l=null==i?void 0:i.duration)&&void 0!==l?l:null==n?void 0:n.duration}),r)}}}(re,ie,{duration:i,behavior:h,boundary:te},P)),[re,i,h,P,ie]),ae=o.default.useCallback((()=>Object.assign(Object.assign({},le),{items:re,scrollContainer:te,menuVisible:ie})),[le,re,te,ie]),[ue,de]=o.default.useState((()=>ae()));(({context:e,onInit:t,onUpdate:n})=>{const r=o.default.useCallback((()=>t(e)),[t,e]),s=o.default.useCallback((()=>n(e)),[n,e]),{items:i}=e;o.default.useEffect((()=>(i.subscribe(p.onInit,r),i.subscribe(p.onUpdate,s),()=>{i.unsubscribe(p.onInit,r),i.unsubscribe(p.onUpdate,s)})),[i,r,s])})({context:ue,onInit:v,onUpdate:m}),o.default.useEffect((()=>de(ae())),[ae]),o.default.useEffect((()=>{U(W)?W.current=ue:W(ue)}),[ue,W]);const ce=o.default.useCallback((e=>I(ue,e)),[I,ue]),fe=o.default.useCallback((e=>S(ue,e)),[S,ue]),he=o.default.useMemo((()=>`${l} ${$}`),[$]),ve=o.default.useMemo((()=>`${j}${H?" rtl":b}`),[H,j]);return o.default.createElement("div",{className:he,onWheel:fe,onMouseDown:null==y?void 0:y(ue),onMouseLeave:null==w?void 0:w(ue),onMouseUp:null==E?void 0:E(ue),onMouseMove:null==C?void 0:C(ue),onTouchStart:null==k?void 0:k(ue),onTouchMove:null==x?void 0:x(ue),onTouchEnd:null==N?void 0:N(ue)},o.default.createElement(_.Provider,{value:ue},o.default.createElement("div",{className:u},Z),o.default.createElement("div",{className:a},o.default.createElement("div",{className:d},J),o.default.createElement(D,{className:ve,onScroll:ce,scrollRef:te,containerRef:O},o.default.createElement(F,{refs:ne,itemClassName:T},n)),o.default.createElement("div",{className:c},Q)),o.default.createElement("div",{className:f},ee)))},e.VisibilityContext=_,e.constants=y,e.getItemsPos=e=>{var t;const n=e[Math.floor(e.length/2)];return{first:null==e?void 0:e[0],center:n,last:null===(t=e.slice(-1))||void 0===t?void 0:t[0]}},e.slidingWindow=function(e,t){return{prev:()=>function(e,t){const n=e.findIndex((e=>e===(null==t?void 0:t[0]))),o=t.length,r=n-o,s=r<0,i=s?0:r,l=e.slice(i,s?o:n);return l.length===o?l:e.slice(n,o)}(e,t),next:()=>function(e,t){const n=e.findIndex((e=>{var n;return e===(null===(n=t.slice(-1))||void 0===n?void 0:n[0])})),o=t.length,r=n+o+1,s=r>e.length-1,i=s?e.length-1:r,l=e.slice(s?i-o+1:n+1,i);return l.length===o?l:e.slice(e.length-o,e.length+o)}(e,t)}},Object.defineProperty(e,"__esModule",{value:!0})}));