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