import{arrayType as e,objectType as t,someType as n,withInstall as r}from"../_util/type.mjs";import i from"../_util/props-util/initDefaultProps.mjs";import{useToken as a}from"../theme/internal.mjs";import{useMutationObserver as o}from"../_util/hooks/_vueuse/useMutationObserver.mjs";import{getPixelRatio as s,getStyleStr as c,reRendering as l,rotateWatermark as u}from"./utils.mjs";import{computed as d,createVNode as f,defineComponent as p,onBeforeUnmount as m,onMounted as h,shallowRef as g,watch as _}from"vue";var v=2,y=3,b=()=>({zIndex:Number,rotate:Number,width:Number,height:Number,image:String,content:n([String,Array]),font:t(),rootClassName:String,gap:e(),offset:e()}),x=r(p({name:`AWatermark`,inheritAttrs:!1,props:i(b(),{zIndex:9,rotate:-22,font:{},gap:[100,100]}),setup(e,{slots:t,attrs:n}){let[,r]=a(),i=g(),p=g(),b=g(!1),x=d(()=>e.gap?.[0]??100),S=d(()=>e.gap?.[1]??100),C=d(()=>x.value/2),w=d(()=>S.value/2),T=d(()=>e.offset?.[0]??C.value),E=d(()=>e.offset?.[1]??w.value),D=d(()=>e.font?.fontSize??r.value.fontSizeLG),O=d(()=>e.font?.fontWeight??`normal`),k=d(()=>e.font?.fontStyle??`normal`),A=d(()=>e.font?.fontFamily??`sans-serif`),j=d(()=>e.font?.color??r.value.colorFill),M=d(()=>{let t={zIndex:e.zIndex??9,position:`absolute`,left:0,top:0,width:`100%`,height:`100%`,pointerEvents:`none`,backgroundRepeat:`repeat`},n=T.value-C.value,r=E.value-w.value;return n>0&&(t.left=`${n}px`,t.width=`calc(100% - ${n}px)`,n=0),r>0&&(t.top=`${r}px`,t.height=`calc(100% - ${r}px)`,r=0),t.backgroundPosition=`${n}px ${r}px`,t}),N=()=>{p.value&&(p.value.remove(),p.value=void 0)},P=(e,t)=>{i.value&&p.value&&(b.value=!0,p.value.setAttribute(`style`,c({...M.value,backgroundImage:`url('${e}')`,backgroundSize:`${(x.value+t)*v}px`})),i.value?.append(p.value),setTimeout(()=>{b.value=!1}))},F=t=>{let n=120,r=64,i=e.content,a=e.image,o=e.width,s=e.height;if(!a&&t.measureText){t.font=`${Number(D.value)}px ${A.value}`;let e=Array.isArray(i)?i:[i],a=e.map(e=>t.measureText(e).width);n=Math.ceil(Math.max(...a)),r=Number(D.value)*e.length+(e.length-1)*y}return[o??n,s??r]},I=(t,n,r,i,a)=>{let o=s(),c=e.content,l=Number(D.value)*o;t.font=`${k.value} normal ${O.value} ${l}px/${a}px ${A.value}`,t.fillStyle=j.value,t.textAlign=`center`,t.textBaseline=`top`,t.translate(i/2,0),(Array.isArray(c)?c:[c])?.forEach((e,i)=>{t.fillText(e??``,n,r+i*(l+y*o))})},L=()=>{let t=document.createElement(`canvas`),n=t.getContext(`2d`),r=e.image,i=e.rotate??-22;if(n){p.value||(p.value=document.createElement(`div`));let e=s(),[a,o]=F(n),c=(x.value+a)*e,l=(S.value+o)*e;t.setAttribute(`width`,`${c*v}px`),t.setAttribute(`height`,`${l*v}px`);let d=x.value*e/2,f=S.value*e/2,m=a*e,h=o*e,g=(m+x.value*e)/2,_=(h+S.value*e)/2,y=d+c,b=f+l,C=g+c,w=_+l;if(n.save(),u(n,g,_,i),r){let e=new Image;e.onload=()=>{n.drawImage(e,d,f,m,h),n.restore(),u(n,C,w,i),n.drawImage(e,y,b,m,h),P(t.toDataURL(),a)},e.crossOrigin=`anonymous`,e.referrerPolicy=`no-referrer`,e.src=r}else I(n,d,f,m,h),n.restore(),u(n,C,w,i),I(n,y,b,m,h),P(t.toDataURL(),a)}};return h(()=>{L()}),_(()=>[e,r.value.colorFill,r.value.fontSizeLG],()=>{L()},{deep:!0,flush:`post`}),m(()=>{N()}),o(i,e=>{b.value||e.forEach(e=>{l(e,p.value)&&(N(),L())})},{attributes:!0,subtree:!0,childList:!0,attributeFilter:[`style`,`class`]}),()=>f(`div`,{...n,ref:i,class:[n.class,e.rootClassName],style:[{position:`relative`},n.style]},[t.default?.()])}}));export{x as default,b as watermarkProps};