/*!
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 n,defineProperty as r,assertThisInitialized as a,createClass as i}from"../../_virtual/_rollupPluginBabelHelpers.js";import s from"../../utils/util.js";import{DURATION_CHANGE as u,LOADED_DATA as o,TIME_UPDATE as f,SEEKING as l,PLAY as c}from"../../events.js";import m from"../../plugin/basePlugin.js";import"../../../../../_virtual/delegate.js";var g=/* @__PURE__ */function(){e(h,m);var g=t(h);function h(){var e;n(this,h);for(var t=arguments.length,i=new Array(t),u=0;u<t;u++)i[u]=arguments[u];return e=g.call.apply(g,[this].concat(i)),r(a(e),"_onDurationChange",function(){e.updateSegments();var t=e.player,n=t.currentTime,r=t.timeSegments;if(e._checkIfEnabled(r)){var a=s.getIndexByTime(n,r),i=s.getOffsetCurrentTime(n,r,a);e.player.offsetCurrentTime=i,e.changeIndex(a,r)}}),r(a(e),"_onLoadedData",function(){var t=e.player.timeSegments;if(e._checkIfEnabled(t)){var n=s.getOffsetCurrentTime(0,t);e.player.offsetCurrentTime=n,e.changeIndex(0,t),e.curPos.start>0&&(e.player.currentTime=e.curPos.start)}}),r(a(e),"_onTimeupdate",function(){var t=e.player,n=t.currentTime,r=t.timeSegments;if(e._checkIfEnabled(r)){var a=r.length;e.lastCurrentTime=n;var i=s.getIndexByTime(n,r);i!==e.curIndex&&e.changeIndex(i,r);var u=s.getOffsetCurrentTime(n,r,i);if(e.player.offsetCurrentTime=u,e.curPos){var o=e.curPos,f=o.start,l=o.end;n<f?e.player.currentTime=f:n>l&&i>=a-1&&e.player.pause()}}}),r(a(e),"_onSeeking",function(){var t=e.player,n=t.currentTime,r=t.timeSegments;if(e._checkIfEnabled(r))if(n<r[0].start)e.player.currentTime=r[0].start;else if(n>r[r.length-1].end)e.player.currentTime=r[r.length-1].end;else{var a=s.getIndexByTime(n,r);if(a>=0){var i=e.getSeekTime(n,e.lastCurrentTime,a,r);i>=0&&(e.player.currentTime=i)}}}),r(a(e),"_onPlay",function(){var t=e.player,n=t.currentTime,r=t.timeSegments;e._checkIfEnabled(r)&&n>=r[r.length-1].end&&(e.player.currentTime=r[0].start)}),e}return i(h,[{key:"afterCreate",value:function(){this.curIndex=-1,this.curPos=null,this.lastCurrentTime=0,this.updateSegments(),this.on(u,this._onDurationChange),this.on(o,this._onLoadedData),this.on(f,this._onTimeupdate),this.on(l,this._onSeeking),this.on(c,this._onPlay)}},{key:"setConfig",value:function(e){var t=this;if(e){var n=Object.keys(e);n.length<1||(n.forEach(function(n){t.config[n]=e[n]}),this.updateSegments())}}},{key:"updateSegments",value:function(){var e=this.config,t=e.disable,n=e.segments,r=this.player;if(t||!n||0===n.length)r.timeSegments=[],r.offsetDuration=0,r.offsetCurrentTime=-1;else{var a=this.formatTimeSegments(n,r.duration);r.timeSegments=a,r.offsetDuration=a.length>0?a[a.length-1].duration:0}}},{key:"formatTimeSegments",value:function(e,t){var n=[];return e?(e.sort(function(e,t){return e.start-t.start}),e.forEach(function(e,r){var a={};if(a.start=e.start<0?0:e.start,a.end=t>0&&e.end>t?t:e.end,!(t>0&&a.start>t)){n.push(a);var i=a.end-a.start;if(0===r)a.offset=e.start,a.cTime=0,a.segDuration=i,a.duration=i;else{var s=n[r-1];a.offset=s.offset+(a.start-s.end),a.cTime=s.duration+s.cTime,a.segDuration=i,a.duration=s.duration+i}}}),n):[]}},{key:"getSeekTime",value:function(e,t,n,r){var a=-1,i=r[n],s=i.start,u=i.end;if(e>=s&&e<=u)return a;var o=e-t;if(o<0&&e<s){var f=t>s?t-s:0;return a=n-1>=0?r[n-1].end+o+f:0}return-1}},{key:"_checkIfEnabled",value:function(e){return!(!e||e.length<1)}},{key:"changeIndex",value:function(e,t){this.curIndex=e,e>=0&&t.length>0?this.curPos=t[e]:this.curPos=null}}],[{key:"pluginName",get:function(){return"TimeSegmentsControls"}},{key:"defaultConfig",get:function(){return{disable:!0,segments:[]}}}]),h}();export{g as default};