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