const e=require("../_util/type.js"),t=require("../_util/classNames.js"),n=require("../config-provider/hooks/useConfigInject.js"),r=require("../vc-util/Dom/addEventListener.js"),i=require("../affix/index.js"),a=require("../node_modules/.pnpm/scroll-into-view-if-needed@3.1.0/node_modules/scroll-into-view-if-needed/dist/index.js"),o=require("../_util/getScroll.js"),s=require("../_util/scrollTo.js"),c=require("../_util/vue-types/index.js"),l=require("./context.js"),u=require("./AnchorLink.js"),d=require("./style/index.js");let f=require("vue");function p(){return window}function m(e,t){if(!e.getClientRects().length)return 0;let n=e.getBoundingClientRect();return n.width||n.height?t===window?(t=e.ownerDocument.documentElement,n.top-t.clientTop):n.top-t.getBoundingClientRect().top:n.top}var h=/#([\S ]+)$/,g=()=>({prefixCls:String,offsetTop:Number,bounds:Number,affix:{type:Boolean,default:!0},showInkInFixed:{type:Boolean,default:!1},getContainer:Function,wrapperClass:String,wrapperStyle:{type:Object,default:void 0},getCurrentAnchor:Function,targetOffset:Number,items:e.arrayType(),direction:c.default.oneOf([`vertical`,`horizontal`]).def(`vertical`),onChange:Function,onClick:Function}),_=(0,f.defineComponent)({compatConfig:{MODE:3},name:`AAnchor`,inheritAttrs:!1,props:g(),setup(e,{emit:c,attrs:g,slots:_,expose:v}){let{prefixCls:y,getTargetContainer:b,direction:x}=n.default(`anchor`,e),S=(0,f.computed)(()=>e.direction??`vertical`),C=(0,f.ref)(null),w=(0,f.ref)(),T=(0,f.reactive)({links:[],scrollContainer:null,scrollEvent:null,animating:!1}),E=(0,f.ref)(null),D=(0,f.computed)(()=>{let{getContainer:t}=e;return t||b?.value||p}),O=(e=0,t=5)=>{let n=[],r=D.value();return T.links.forEach(i=>{let a=h.exec(i.toString());if(!a)return;let o=document.getElementById(a[1]);if(o){let a=m(o,r);a<e+t&&n.push({link:i,top:a})}}),n.length?n.reduce((e,t)=>t.top>e.top?t:e).link:``},k=t=>{let{getCurrentAnchor:n}=e;E.value!==t&&(E.value=typeof n==`function`?n(t):t,c(`change`,t))},A=t=>{let{offsetTop:n,targetOffset:r}=e;k(t);let i=h.exec(t);if(!i)return;let a=document.getElementById(i[1]);if(!a)return;let c=D.value(),l=o.default(c,!0)+m(a,c);l-=r===void 0?n||0:r,T.animating=!0,s.default(l,{callback:()=>{T.animating=!1},getContainer:D.value})};v({scrollTo:A});let j=()=>{if(T.animating)return;let{offsetTop:t,bounds:n,targetOffset:r}=e,i=O(r===void 0?t||0:r,n);k(i)},M=()=>{let e=w.value.querySelector(`.${y.value}-link-title-active`);if(e&&C.value){let t=S.value===`horizontal`;C.value.style.top=t?``:`${e.offsetTop+e.clientHeight/2}px`,C.value.style.height=t?``:`${e.clientHeight}px`,C.value.style.left=t?`${e.offsetLeft}px`:``,C.value.style.width=t?`${e.clientWidth}px`:``,t&&a.default(e,{scrollMode:`if-needed`,block:`nearest`})}};l.default({registerLink:e=>{T.links.includes(e)||T.links.push(e)},unregisterLink:e=>{let t=T.links.indexOf(e);t!==-1&&T.links.splice(t,1)},activeLink:E,scrollTo:A,handleClick:(e,t)=>{c(`click`,e,t)},direction:S}),(0,f.onMounted)(()=>{(0,f.nextTick)(()=>{let e=D.value();T.scrollContainer=e,T.scrollEvent=r.default(T.scrollContainer,`scroll`,j),j()})}),(0,f.onBeforeUnmount)(()=>{T.scrollEvent&&T.scrollEvent.remove()}),(0,f.onUpdated)(()=>{if(T.scrollEvent){let e=D.value();T.scrollContainer!==e&&(T.scrollContainer=e,T.scrollEvent.remove(),T.scrollEvent=r.default(T.scrollContainer,`scroll`,j),j())}M()});let N=e=>Array.isArray(e)?e.map(e=>{let{children:t,key:n,href:r,target:i,class:a,style:o,title:s}=e;return(0,f.createVNode)(u.default,{key:n,href:r,target:i,class:a,style:o,title:s,customTitleProps:e},{default:()=>[S.value===`vertical`?N(t):null],customTitle:_.customTitle})}):null,[P,F]=d.default(y);return()=>{let{offsetTop:n,affix:r,showInkInFixed:a}=e,o=y.value,s=t.default(`${o}-ink`,{[`${o}-ink-visible`]:E.value}),c=t.default(F.value,e.wrapperClass,`${o}-wrapper`,{[`${o}-wrapper-horizontal`]:S.value===`horizontal`,[`${o}-rtl`]:x.value===`rtl`}),l=t.default(o,{[`${o}-fixed`]:!r&&!a}),u=(0,f.createVNode)(`div`,{class:c,style:{maxHeight:n?`calc(100vh - ${n}px)`:`100vh`,...e.wrapperStyle},ref:w},[(0,f.createVNode)(`div`,{class:l},[(0,f.createVNode)(`span`,{class:s,ref:C},null),Array.isArray(e.items)?N(e.items):_.default?.()])]);return P(r?(0,f.createVNode)(i.default,{...g,offsetTop:n,target:D.value},{default:()=>[u]}):u)}}});exports.anchorProps=g,exports.default=_;