import _extends from"@babel/runtime/helpers/extends";import{isUndef}from"../../utils/common";var systemInfo,H5_PAGE="h5Page",MEETING_DETAIL="meetingDetail",DOC_PREVIEW="docPreview",targetPageArr=[H5_PAGE,MEETING_DETAIL,DOC_PREVIEW],isMiniProgram=!isUndef(typeof my)&&null!==my&&!isUndef(typeof my.alert);isMiniProgram&&(systemInfo=my.getSystemInfoSync());var userAgent=isMiniProgram?systemInfo.platform:navigator.userAgent,screenWidth=isMiniProgram?systemInfo.screenWidth:window.screen.width,pixelRatio=(isMiniProgram?systemInfo.pixelRatio:window.devicePixelRatio)||2,emptyWatermark=isMiniProgram?Promise.resolve(""):"",WaterMark=function(){function t(t){void 0===t&&(t={}),this.options=_extends({texts:[""],width:50,height:50,textRotate:-10,textColor:"#000000",textFont:"PingFangSC-Regular,system-ui,sans-serif",fontStyle:"normal",opacity:90,canvas:[],fontSize:14},t),this.options.width*=this.options.fontSize/12,this.options.height*=this.options.fontSize/12,this.options.deg=this.options.textRotate*Math.PI/180,this.options.cosDeg=Math.cos(this.options.deg),this.options.absSinDeg=Math.abs(Math.sin(this.options.deg))}var e=t.prototype;return e.init=function(){var t=this,e=null,i=null;isMiniProgram?i=my.createCanvasContext("canvasBg"):(e=this.createCanvas(),i=e.getContext("2d")),this.calcTextSize();var n=this.options,o=n.allItemsWidth,a=n.drawItems,r=n.height,s=n.containerComp,h=Math.ceil(screenWidth/o),l=new Array(h).fill(a).reduce((function(t,e){return t.concat(e)}),[]),f=function(){t.setCanvasStyle(i),t.drawText(i,l),i.translate(0,r),t.drawText(i,l.reverse(),!0)};if(isMiniProgram)return new Promise((function(t){s.setState({width:o*h,height:2*r},(function(){setTimeout((function(){f(),i.draw(),t(i.toDataURL("image/png"))}),0)}))}));e.width=o*h,e.height=2*r,e.style.display="none",f();var m=e.toDataURL("image/png");return this.destroy(),m},e.calcTextSize=function(){var t=0,e=0,i=this.options;i.drawItems=[].map.call(i.texts,(function(n){var o,a,r,s;if(isMiniProgram){for(var h=0,l=0;l<n.length;l+=1)h+=/[\uff00-\uffff]/.test(n[l])?1:.5;o=1.1*i.fontSize*h,a=1.2*i.fontSize}else{var f=(r='<span style="font:'+i.fontSize+"px "+i.textFont+';visibility:hidden;">'+n+"</span>",(s=document.createElement("div")).innerHTML=r.trim(),s.firstChild);document.body.appendChild(f),o=f.offsetWidth,a=f.offsetHeight,document.body.removeChild(f)}return t=Math.max(t,o),i.fontHeight||(i.fontHeight=a),e+=Math.ceil(i.cosDeg*(i.width<o?o:i.width)),{txt:n,width:o,height:a}})),t>i.width&&(i.width=t);var n=t*i.absSinDeg+i.fontHeight*i.cosDeg;n>i.height&&(i.height=n),i.maxItemWidth=t,i.allItemsWidth=e},e.setCanvasStyle=function(t){var e=this.options,i=e.deg,n=e.absSinDeg,o=e.height,a=e.fontHeight,r=e.fontStyle,s=e.fontSize,h=e.textFont,l=e.textColor,f=e.opacity;t.rotate(i);var m=n*(o-a);t.translate(-m,0),t.font=r+" "+s+"px "+h,t.fillStyle=l,t.textAlign="left",t.textBaseline="bottom",t.globalAlpha=f},e.drawText=function(t,e,i){void 0===i&&(i=!1);var n=this.options,o=n.maxItemWidth,a=n.width,r=n.height,s=n.deg,h=n.cosDeg,l=n.absSinDeg;e.forEach((function(e,n){var f=h*(o-e.width)/2,m=a*h*n,c=Math.abs(m*Math.tan(s))+r;t.fillText(e.txt,m+(i?h*(a-e.width)/2:f),c+(i?l*(a-e.width)/2:0))}))},e.createCanvas=function(){var t=document.createElement("canvas");return this.options.canvas.push(t),t},e.destroy=function(){this.options.canvas.forEach((function(t){t.remove(),t=null}))},t}();function drawWatermark(t,e){var i=JSON.parse(t),n=i.watermark||i;if(!n||"0"===String(n.watermarkStatus))return emptyWatermark;if(!Array.isArray(n.targetPages)||!n.targetPages.some((function(t){return t.name===e&&"1"===String(t.value)})))return emptyWatermark;var o=[];if(Array.isArray(n.contentType)){var a="";n.contentType.includes(1)&&(a+=n.userName+" "),n.contentType.includes(2)&&(a+=(n.account||"").slice(-4)),a&&o.push(a),n.contentType.includes(0)&&n.contentCustom&&o.push(n.contentCustom)}if(!o.length)return emptyWatermark;var r,s,h=/Android|Adr|SymbianOS|Windows\s*Phone|Mobile/.test(userAgent),l=/iPhone|iPad|iPod|Mac\s*OS.*Mobile|iOS/.test(userAgent),f="0"===String(n.watermarkShowDensity);l?f?(r=114,s=66):(r=86,s=45):h?f?(r=47*pixelRatio,s=40*pixelRatio):(r=25*pixelRatio,s=25*pixelRatio):f?(r=300,s=126):(r=194,s=106);return new WaterMark({containerComp:this,texts:o,width:r,height:s,textRotate:-10,textColor:{0:"#FF0000",1:"#000000",2:"#0000FF"}[n.fontColor]||"#000000",textFont:"PingFangSC-Regular,system-ui,sans-serif",fontStyle:"0"===String(n.fontStyle)?"normal":"bold",opacity:(120-parseInt(n.fontDiaphaneity,10))/100,fontSize:{0:12,1:16,2:28}[n.fontSize]||16}).init()}export function generateWatermark(t,e){if(void 0===t&&(t={}),void 0===e&&(e=H5_PAGE),!targetPageArr.includes(e))throw new Error("第二个可选参数，仅能为“h5Page”或“meetingDetail”");try{return drawWatermark.call(this,JSON.stringify(t),e)}catch(t){throw t}}