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