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