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