/*!
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 e,createSuper as t,classCallCheck as i,defineProperty as s,assertThisInitialized as n,createClass as a,toConsumableArray as r}from"../../_virtual/_rollupPluginBabelHelpers.js";import p from"../../utils/util.js";import{DOWNLOAD_SPEED_CHANGE as d,LOADED_DATA as o,REPLAY as h}from"../../events.js";import l from"../../plugin/plugin.js";var c="cdn",u=["cdn"],f=/* @__PURE__ */function(){e(m,l);var f=t(m);function m(){var e;i(this,m);for(var t=arguments.length,a=new Array(t),r=0;r<t;r++)a[r]=arguments[r];return e=f.call.apply(f,[this].concat(a)),s(n(e),"getSpeed",function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:c;if(!e.speedListCache||!e.speedListCache[t])return 0;if(e.speedListCache[t].length<=0)return 0;var i=0;return e.speedListCache[t].map(function(e){i+=e}),Math.floor(i/e.speedListCache[t].length)}),s(n(e),"startTimer",function(){p.isMSE(e.player.video)||(e.initSpeedList(),e.cnt=0,e.timer=setTimeout(e.testSpeed,e.config.testTimeStep))}),s(n(e),"initSpeedList",function(){e.speedListCache={},u.forEach(function(t){e.speedListCache[t]=[]})}),s(n(e),"_onRealSpeedChange",function(t){t.speed&&e.appendList(t.speed,t.type||c)}),s(n(e),"testSpeed",function(){if(clearTimeout(e.timer),e.timer=null,e.player&&e.config.openSpeed){var t=e.config,i=t.url,s=t.loadSize,n=t.testCnt,a=t.testTimeStep,r=i+(i.indexOf("?")<0?"?testst=":"&testst=")+Date.now();if(!(e.cnt>=n)){e.cnt++;try{var p=/* @__PURE__ */(new Date).getTime(),d=null,o=new XMLHttpRequest;e.xhr=o,o.open("GET",r);var h={},l=Math.floor(10*Math.random());h.Range="bytes="+l+"-"+(s+l),h&&Object.keys(h).forEach(function(e){o.setRequestHeader(e,h[e])}),o.onreadystatechange=function(){if(4===o.readyState){e.xhr=null,d=/* @__PURE__ */(new Date).getTime();var t=o.getResponseHeader("Content-Length")/1024*8,i=Math.round(1e3*t/(d-p));e.appendList(i),e.timer=setTimeout(e.testSpeed,a)}},o.send()}catch(c){console.error(c)}}}}),s(n(e),"appendList",function(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:c;if(e.speedListCache&&e.speedListCache[i]){var s=e.config.saveSpeedMax;e.speedListCache[i].length>=s&&e.speedListCache[i].shift(),e.speedListCache[i].push(t);var a=n(e).player;a&&(i===c?a.realTimeSpeed=t:a[e.getSpeedName("realTime",i)]=t),e.updateSpeed(i)}}),s(n(e),"updateSpeed",function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:c,i=e.getSpeed(t),s=n(e).player;if(s)if(t===c)s.avgSpeed&&i===s.avgSpeed||(s.avgSpeed=i,s.emit(d,{speed:i,realTimeSpeed:s.realTimeSpeed}));else{var a=e.getSpeedName("avg",t);s[a]&&i===s[a]||(s[a]=i,s.emit(d,{speed:i,realTimeSpeed:s.realTimeSpeed}))}}),e}return a(m,[{key:"afterCreate",value:function(){var e=this.config,t=e.openSpeed,i=e.addSpeedTypeList;(null==i?void 0:i.length)>0&&u.push.apply(u,r(i)),this.initSpeedList(),this.on("real_time_speed",this._onRealSpeedChange),this.timer=null,this.cnt=0,this.xhr=null,t&&this.on([o,h],this.startTimer)}},{key:"getSpeedName",value:function(e,t){return e+"Speed"+t.toUpperCase()}},{key:"openSpeed",get:function(){return this.config.openSpeed},set:function(e){if(this.config.openSpeed=e,!e&&this.timer)return clearTimeout(this.timer),void(this.timer=null);if(this.config.openSpeed){if(this.timer)return;this.timer=setTimeout(this.testSpeed,this.config.testTimeStep)}}},{key:"destroy",value:function(){var e=this;this.off("real_time_speed",this._onRealSpeedChange),this.off([o,h],this.startTimer),u.forEach(function(t){e.speedListCache&&e.speedListCache[t]&&(e.speedListCache[t]=[])}),this.speedListCache&&(this.speedListCache={}),clearTimeout(this.timer),this.timer=null,this.xhr&&4!==this.xhr.readyState&&(this.xhr.cancel&&this.xhr.cancel(),this.xhr=null)}}],[{key:"pluginName",get:function(){return"testspeed"}},{key:"defaultConfig",get:function(){return{openSpeed:!1,testCnt:3,loadSize:204800,testTimeStep:3e3,url:"",saveSpeedMax:5,addSpeedTypeList:[]}}}]),m}();export{f as default};