/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
import{inherits as t,createSuper as e,classCallCheck as i,defineProperty as a,assertThisInitialized as n,createClass as r}from"../../_virtual/_rollupPluginBabelHelpers.js";import s from"../../utils/util.js";import o from"../../utils/sniffer.js";import{TIME_UPDATE as h,COMPLETE as c,EMPTIED as l,PLAY as d,PAUSE as u,LOADED_DATA as f,LOAD_START as v}from"../../events.js";import p from"../../utils/debug.js";import m from"../../plugin/plugin.js";var g="realtime",y="firstframe",I="poster";var k=null,C=/* @__PURE__ */function(){t(w,m);var C=e(w);function w(){var t;i(this,w);for(var e=arguments.length,r=new Array(e),o=0;o<e;o++)r[o]=arguments[o];return t=C.call.apply(C,[this].concat(r)),a(n(t),"onLoadedData",function(e){t.player&&(t._frameCount=t.config.startFrameCount,t.stop(),t.renderOnTimeupdate(e),t.off(h,t.renderOnTimeupdate),t.on(h,t.renderOnTimeupdate))}),a(n(t),"onVisibilitychange",function(e){"visible"===document.visibilityState?t._checkIfCanStart()&&t.start():"hidden"===document.visibilityState&&t.stop()}),a(n(t),"renderOnTimeupdate",function(e){if(t._frameCount>0)t.renderOnce(),t._frameCount--;else{t._isLoaded=!0,t.off(h,t.renderOnTimeupdate);var i=t.config.startInterval;!t.player.paused&&t._checkIfCanStart()&&t.start(0,i)}}),a(n(t),"start",function(e,i){var a=t.player.video,r=function(){try{return parseInt(window.performance.now(),10)}catch(t){/* @__PURE__ */
return(new Date).getTime()}}(),o=t.checkVideoIsSupport(a);o&&t.canvasCtx&&(i||(i=t.interval),t.stop(),a.videoWidth&&a.videoHeight&&(t.videoPI=a.videoHeight>0?parseInt(a.videoWidth/a.videoHeight*100,10):0,(t.config.mode===g||r-t.preTime>=i)&&(a&&a.videoWidth&&t.update(o,t.videoPI),t.preTime=r)),t.frameId="timer"===t._loopType?s.setTimeout(n(t),t.start,i):s.requestAnimationFrame(t.start))}),a(n(t),"stop",function(){t.frameId&&("timer"===t._loopType?s.clearTimeout(n(t),t.frameId):s.cancelAnimationFrame(t.frameId),t.frameId=null)}),t}return r(w,[{key:"afterCreate",value:function(){var t=this;!0===this.playerConfig.dynamicBg&&(this.config.disable=!1),w.isSupport||(this.config.disable=!0);var e=this.config,i=e.disable,a=e.mode,n=e.frameRate;i||(this._pos={width:0,height:0,rwidth:0,rheight:0,x:0,y:0,pi:0},this.isStart=!1,this._isLoaded=!1,this.videoPI=0,this.preTime=0,this.interval=parseInt(1e3/n,10),this.canvas=null,this.canvasCtx=null,this._frameCount=0,this._loopType=this.config.mode!==g&&this.interval>=1e3?"timer":"animation",this.once(c,function(){t.player&&(t.init(),t.renderByPoster(),t.player.paused||t.start())}),a!==I&&(a!==y&&(this.on(l,function(){t.stop()}),this.on(d,function(){var e=t.config.startInterval;t._checkIfCanStart()&&t.start(0,e)}),this.on(u,function(){t.stop()})),this.on(f,this.onLoadedData),this.on(v,function(){t._isLoaded=!1,t.stop()}),document.addEventListener("visibilitychange",this.onVisibilitychange)))}},{key:"setConfig",value:function(t){var e=this;Object.keys(t).forEach(function(i){"root"===i&&t[i]!==e.config[i]?e.reRender(t[i]):"frameRate"===i?e.interval=parseInt(1e3/t[i],10):"disable"===i&&t[i]&&e.stop(),e.config[i]=t[i]})}},{key:"init",value:function(t){var e=this.player,i=this.config;this.canvasFilter=w.supportCanvasFilter();try{var a=t||i.root;a||(a=i.isInnerRender&&e.innerContainer||e.root),a.insertAdjacentHTML("afterbegin",'<div class="xgplayer-dynamic-bg" data-index="'.concat(i.index,'"><canvas>\n        </canvas><xgmask></xgmask></div>')),this.root=a.children[0],this.canvas=this.find("canvas"),this.canvasFilter||(this.canvas.style.filter=i.filter,this.canvas.style.webkitFilter=i.filter),this.mask=this.find("xgmask"),i.addMask&&(this.mask.style.background=i.maskBg),this.canvasCtx=this.canvas.getContext("2d")}catch(n){p.logError("plugin:DynamicBg",n)}}},{key:"reRender",value:function(t){if(this.config.disable||this.root){this.stop();var e=this.root?this.root.parentElement:null;if(e!==t&&e.removeChild(this.root),t){this.init(t),this.renderOnce();var i=this.config.startInterval;this._checkIfCanStart()&&this.start(0,i)}else this.root=null}}},{key:"checkVideoIsSupport",value:function(t){if(!t)return null;var e=t&&t instanceof window.HTMLVideoElement?t:t.canvas?t.canvas:t.flyVideo?t.flyVideo:null;if(e&&("safari"!==o.browser||!s.isMSE(e)))return e;var i=e?e.tagName.toLowerCase():"";return"canvas"===i||"img"===i?e:null}},{key:"renderByPoster",value:function(){var t=this.playerConfig.poster;if(t){var e="String"===s.typeOf(t)?t:"String"===s.typeOf(t.poster)?t.poster:null;this.updateImg(e)}}},{key:"_checkIfCanStart",value:function(){var t=this.config.mode;return this._isLoaded&&!this.player.paused&&t!==y&&t!==I}},{key:"renderOnce",value:function(){var t=this.player.video;if(t.videoWidth&&t.videoHeight){this.videoPI=parseInt(t.videoWidth/t.videoHeight*100,10);var e=this.checkVideoIsSupport(t);e&&this.update(e,this.videoPI)}}},{key:"updateImg",value:function(t){var e=this;if(t){var i=this.canvas.getBoundingClientRect(),a=i.width,n=i.height,r=new window.Image;r.onload=function(){if(e.canvas&&!e.frameId&&!e.isStart){e.canvas.height=n,e.canvas.width=a;var t=parseInt(a/n*100,10);e.update(r,t),r=null}},r.src=t}}},{key:"update",value:function(t,e){if(this.canvas&&this.canvasCtx&&e)try{var i=this._pos,a=this.config,n=this.canvas.getBoundingClientRect(),r=n.width,s=n.height;if(r!==i.width||s!==i.height||i.pi!==e){var o=parseInt(r/s*100,10);i.pi=e,i.width!==r&&(i.width=this.canvas.width=r),i.height!==s&&(i.height=this.canvas.height=s);var h=s,c=r;o<e?c=parseInt(s*e/100,10):o>e&&(h=parseInt(100*r/e,10)),i.rwidth=c*a.multiple,i.rheight=h*a.multiple,i.x=(r-i.rwidth)/2,i.y=(s-i.rheight)/2}this.canvasFilter&&(this.canvasCtx.filter=a.filter),this.canvasCtx.drawImage(t,i.x,i.y,i.rwidth,i.rheight)}catch(l){p.logError("plugin:DynamicBg",l)}}},{key:"destroy",value:function(){this.stop(),document.removeEventListener("visibilitychange",this.onVisibilitychange),this.canvasCtx=null,this.canvas=null}},{key:"render",value:function(){return""}}],[{key:"pluginName",get:function(){return"dynamicBg"}},{key:"defaultConfig",get:function(){return{isInnerRender:!1,disable:!0,index:-1,mode:"framerate",frameRate:10,filter:"blur(50px)",startFrameCount:2,startInterval:0,addMask:!0,multiple:1.2,maskBg:"rgba(0,0,0,0.7)"}}},{key:"isSupport",get:function(){return"boolean"==typeof k?k:k=function(){try{return!!document.createElement("canvas").getContext}catch(t){return!1}}()}},{key:"supportCanvasFilter",value:function(){return!("safari"===o.browser||"firefox"===o.browser)}}]),w}();export{C as default};