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