import e from"@babel/runtime/helpers/slicedToArray";import t from"@babel/runtime/helpers/defineProperty";import{useRef as r,useEffect as n,useState as o,useCallback as i,useMemo as a}from"react";import c from"prop-types";import{throttle as l,noop as s}from"lodash";import p from"classnames";import{makeStyles as u,Typography as m,Link as d}from"@material-ui/core";import{jsx as f,jsxs as h,Fragment as g}from"react/jsx-runtime";function b(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function y(e){for(var r=1;r<arguments.length;r++){var n=null!=arguments[r]?arguments[r]:{};r%2?b(Object(n),!0).forEach((function(r){t(e,r,n[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):b(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}var v=u((function(e){return{root:t({top:70,width:175,flexShrink:0,position:"sticky",height:"calc(100vh - 70px)",overflowY:"auto",padding:e.spacing(2,0),display:"none"},e.breakpoints.up("sm"),{display:"block"}),title:{color:e.palette.primary.text8,margin:"0 0 8px 16px",lineHeight:"24px",fontSize:10,fontWeight:500,letterSpacing:1},contents:{marginTop:e.spacing(2),paddingLeft:e.spacing(1)},ul:{padding:0,margin:0,listStyle:"none",borderLeft:"1px solid ".concat(e.palette.background.b9)},li:{display:"flex","&:not(:first-of-type)":{marginTop:8}},item:{color:e.palette.primary.text6,display:"inline-block",margin:"7px 16px",fontSize:12,fontWeight:400,letter:"0.4px",lineHeight:"16px"},active:{color:e.palette.text.primary,fontWeight:500},activeItemBorder:{height:30,width:"2px",backgroundColor:"#03BCD4",borderRadius:"0 2px 2px 0",position:"absolute"}}}));function x(t){var c=t.title,u=t.items,b=v(),x=r([]);n((function(){x.current=u.map((function(e){return y(y({},e),{},{node:document.getElementById(e.hash)})}))}),[u]);var O,w,k,j=o(null),P=e(j,2),T=P[0],E=P[1],S=r(!1),N=r(null),I=i((function(){if(!S.current){for(var e,t=x.current.length-1;t>=0;t-=1){if(document.documentElement.scrollTop<100){e={hash:null};break}var r=x.current[t];if(r.node&&r.node.offsetTop<document.documentElement.scrollTop+document.documentElement.clientHeight/8){e=r;break}}e&&T!==e.hash&&E(e.hash)}}),[T]);O=u.length>0?I:null,k=a((function(){return O?l(O,w):s}),[O,w=166]),n((function(){if(k!==s)return window.addEventListener("scroll",k),function(){window.removeEventListener("scroll",k),k.cancel()}}),[k]);return n((function(){return function(){clearTimeout(N.current)}}),[]),n((function(){var e=window.location.hash;if(e){var t=document.getElementById(e.substr(1));t&&setTimeout((function(){t.scrollIntoView({block:"start",inline:"nearest",behavior:"smooth"})}),50)}}),[]),f("nav",{className:b.root,"data-testid":"anchors-list",children:u.length>0&&h(g,{children:[f("div",{className:b.title,children:c}),f(m,{component:"ul",className:b.ul,children:u.map((function(e){var t,r=T===e.hash;return h("li",{className:b.li,children:[r&&f("div",{className:b.activeItemBorder}),f(d,{display:"block",href:"#".concat(e.hash),underline:"none",onClick:(t=e.hash,function(e){e.defaultPrevented||0!==e.button||e.metaKey||e.ctrlKey||e.altKey||e.shiftKey||(S.current=!0,N.current=setTimeout((function(){S.current=!1}),1e3),T!==t&&E(t))}),className:p(b.item,r&&b.active),"data-testid":"anchor-item-".concat(e.hash),children:f("span",{children:e.text})})]},e.text)}))})]})})}x.defaultProps={title:"ON THIS PAGE",items:[]},x.propTypes={title:c.string,items:c.arrayOf(c.shape({hash:c.string,text:c.string}))};export{x as default};
