/*!
 * fireplayer-plugin-chapter.js v1.1.0
 * Github: https://fireplayer.net
 * Fire Player - https://fireplayer.net
 * Released under the MIT License.
 */
!function(t,e){"object"===typeof exports&&"undefined"!==typeof module?module.exports=e():"function"==typeof define&&define.amd?(t.fireplayerPluginChapter=e(),define(function(){return t.fireplayerPluginChapter})):(t="undefined"!==typeof globalThis?globalThis:t||self).fireplayerPluginChapter=e()}(this,function(){"use strict";if("undefined"!==typeof document){const t="fireplayer-plugin-chapter";let e=document.getElementById(t);e||(e=document.createElement("style"),e.id=t,"loading"===document.readyState?document.addEventListener("DOMContentLoaded",()=>{document.head.appendChild(e)}):(document.head||document.documentElement).appendChild(e)),e.textContent=".fireplayer-plugin-chapter .fire-control-progress-inner{height:100%!important;background-color:transparent!important}.fireplayer-plugin-chapter .fire-control-progress-inner>.fire-progress-hover,.fireplayer-plugin-chapter .fire-control-progress-inner>.fire-progress-loaded,.fireplayer-plugin-chapter .fire-control-progress-inner>.fire-progress-played{display:none!important}.fireplayer-plugin-chapter .fire-control-thumbnails{bottom:calc(var(--fire-bottom-gap) + 64px)!important}.fireplayer-plugin-chapter .fire-chapters{position:absolute;z-index:0;inset:0;display:flex;align-items:center;gap:4px;height:100%;transform:scaleY(1.25)}.fireplayer-plugin-chapter .fire-chapters .fire-chapter{display:flex;align-items:center;height:100%}.fireplayer-plugin-chapter .fire-chapters .fire-chapter .fire-chapter-inner{position:relative;cursor:pointer;width:100%;height:50%;border-radius:10px;overflow:hidden;transition:height var(--fire-transition-duration) ease;background-color:var(--fire-progress-color)}.fireplayer-plugin-chapter .fire-chapters .fire-chapter:hover .fire-chapter-inner{height:100%}.fireplayer-plugin-chapter .fire-chapter-title{transform-origin:bottom center;transform:scale(.5);opacity:0;position:absolute;z-index:70;top:-50px;left:0;padding:3px 5px;line-height:1;font-size:14px;border-radius:var(--fire-border-radius);white-space:nowrap;background-color:var(--fire-tip-background);transition:transform var(--fire-transition-duration) ease,opacity var(--fire-transition-duration) ease}.fireplayer-plugin-chapter.fire-progress-hover .fire-chapter-title{transform:scale(1);opacity:1}"}return function(t={}){return e=>{const{$player:r}=e.template,{setStyle:a,append:n,clamp:o,query:i,addClass:p,removeClass:s}=e.constructor.utils;let d=[];const l=e.query(".fire-control-progress-inner"),c=n(l,'<div class="fire-chapters"></div>'),h=n(l,'<div class="fire-chapter-title"></div>');function u({$chapter:t,width:e}){const r=t.dataset.title.trim();if(r){h.textContent=r;const t=h.clientWidth;e<=t/2?a(h,"left",0):e>l.clientWidth-t/2?a(h,"left",l.clientWidth-t+"px"):a(h,"left",e-t/2+"px")}}function g(t=[]){if(d=[],c.textContent="",s(r,"fireplayer-plugin-chapter"),Array.isArray(t)&&t.length&&e.duration){t=t.sort((t,e)=>t.start-e.start);for(let r=0;r<t.length;r++){const a=t[r],n=t[r+1];if(a.end===1/0&&(a.end=e.duration),"number"!==typeof a.start||"number"!==typeof a.end||"string"!==typeof a.title)throw new TypeError("Illegal chapter data type");if(a.start<0||a.end>e.duration||a.start>=a.end)throw new Error("Illegal chapter time point");if(n&&a.end>n.start)throw new Error("Illegal chapter time point")}t[0].start>0&&t.unshift({start:0,end:t[0].start,title:""}),t[t.length-1].end<e.duration&&t.push({start:t[t.length-1].end,end:e.duration,title:""});for(let e=0;e<t.length-1;e++)t[e].end!==t[e+1].start&&t.splice(e+1,0,{start:t[e].end,end:t[e+1].start,title:""});d=t.map(t=>{const r=n(c,'\n                <div class="fire-chapter">\n                    <div class="fire-chapter-inner">\n                        <div class="fire-progress-hover"></div>\n                        <div class="fire-progress-loaded"></div>\n                        <div class="fire-progress-played"></div>\n                    </div>\n                </div>\n        '),a=o(t.start,0,e.duration),p=o(t.end,0,e.duration),s=p-a,d=s/e.duration;return r.dataset.start=a,r.dataset.end=p,r.dataset.duration=s,r.dataset.title=t.title.trim(),r.style.width=100*d+"%",{$chapter:r,$hover:i(".fire-progress-hover",r),$loaded:i(".fire-progress-loaded",r),$played:i(".fire-progress-played",r)}}),p(r,"fireplayer-plugin-chapter"),e.emit("setBar","loaded",e.loaded||0)}}return e.on("setBar",(t,r)=>{if(d.length)for(let n=0;n<d.length;n++){const{$chapter:o,$loaded:i,$played:p,$hover:s}=d[n],l={hover:s,loaded:i,played:p}[t];if(!l)return;const h=c.clientWidth*r,g=e.duration*r,y=Number.parseFloat(o.dataset.duration),f=Number.parseFloat(o.dataset.start),m=Number.parseFloat(o.dataset.end);if(g<f&&a(l,"width",0),g>m&&a(l,"width","100%"),g>=f&&g<=m){a(l,"width",100*((g-f)/y)+"%"),"hover"===t&&h>0&&u({$chapter:o,width:h})}}}),e.once("video:loadedmetadata",()=>g(t.chapters)),{name:"fireplayerPluginChapter",update:({chapters:t})=>g(t)}}}});
