import{isEmpty,waitFor,humanSize,humanDuration}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";import{Controller}from"../base.mjs";import{View}from"../../view/base.mjs";const E=new ElementBuilder({downloadStatus:"enfugue-download-status",downloadCount:"enfugue-download-count",download:"enfugue-download",downloadName:"enfugue-download-name",downloadProgress:"enfugue-download-progress",downloadSize:"enfugue-download-size",downloadTime:"enfugue-download-time"});class DownloadsView extends View{static tagName="enfugue-downloads";constructor(o,t){super(o),this.downloads=t}update(o){if(void 0!==this.node)for(let t of o){let o=this.node.find(`#DL${t.filename.replace(".","_")}`);isEmpty(o)?this.node.append(this.createDownloadNode(t)):(o.find(E.getCustomTag("downloadSize")).content(this.getDownloadSizeText(t)),o.find(E.getCustomTag("downloadProgress")).css(this.getDownloadProgressCSS(t)),o.find(E.getCustomTag("downloadTime")).content(this.getDownloadTimeText(t)))}else this.downloads=o}getDownloadProgressCSS(o){if(isEmpty(o.total))return{};let t=o.downloaded/o.total;return 1===t?{"background-color":"var(--theme-color-primary)","background-image":"none"}:{"background-color":"var(--darker-color)","background-image":`linear-gradient(to right, var(--theme-color-primary) 0%, var(--theme-color-primary) ${(100*t).toFixed(1)}%, transparent calc(${(100*t).toFixed(1)}% + 1px))`}}getDownloadSizeText(o){return isEmpty(o.total)?"Size Unknown":o.downloaded===o.total?`Downloaded ${humanSize(o.total)}`:`Downloaded ${humanSize(o.downloaded)}/${humanSize(o.total)}`}getDownloadTimeText(o){if(isEmpty(o.total))return"Download Pending";if(o.downloaded===o.total){let t=o.total/o.elapsed;return`Complete in ${humanDuration(o.elapsed)} (${humanSize(t)}/sec)`}{let t=o.downloaded/o.elapsed,e=(o.total-o.downloaded)/t;return`${humanDuration(o.elapsed)} elapsed, ${humanDuration(e)} remaining (${humanSize(t)}/sec)`}}createDownloadNode(o){return E.download().id(`DL${o.filename.replace(".","_")}`).content(E.downloadName().content(o.filename),E.downloadSize().content(this.getDownloadSizeText(o)),E.downloadProgress().css(this.getDownloadProgressCSS(o)),E.downloadTime().content(this.getDownloadTimeText(o)))}async build(){let o=await super.build();for(let t of this.downloads)o.append(this.createDownloadNode(t));return o}}class DownloadsController extends Controller{static downloadsWindowWidth=600;static downloadsWindowHeight=500;get interval(){return this.config.model.downloads.interval||5e3}startTimer(){this.timer=setTimeout((()=>this.checkDownloads()),this.interval)}getDownloadStatusCSS(o){return{"background-image":`conic-gradient(from 180deg, var(--theme-color-primary) ${(100*o).toFixed(1)}%, transparent ${(100*o).toFixed(1)}%)`}}async download(o,t,e){let n={type:o,url:t,filename:e};try{await this.model.post("download",null,null,n);this.checkDownloads()}catch(o){if(isEmpty(o)||409!=o.status){let t=isEmpty(o)?"Couldn't communicate with server.":isEmpty(o.detail)?`${o}`:o.detail;this.notify("error","Couldn't Download",t)}else await this.confirm(`File '${e}' exists. Overwrite?`)&&(n.overwrite=!0,response=await this.model.post("download",null,null,n))}}async showDownloads(){if(isEmpty(this.downloadWindow)){let o=new DownloadsView(this.config,this.currentDownloads);this.downloadWindow=await this.spawnWindow("Downloads",o,this.constructor.downloadsWindowWidth,this.constructor.downloadsWindowHeight),this.downloadWindow.onClose((()=>{delete this.downloadWindow}))}else this.downloadWindow.focus()}async checkDownloads(){clearTimeout(this.timer),this.currentDownloads=await this.model.get("download");let o=0,t=0;for(let e of this.currentDownloads)if("downloading"===e.status){let n=e.downloaded/e.total;n>t&&(t=n),o++}o>0?(this.downloadStatusIndicator.removeClass("inactive").css(this.getDownloadStatusCSS(t)).data("tooltip",`${(100*t).toFixed(2)}%`).find(E.getCustomTag("downloadCount")).content(`${o}`).show(),this.startTimer()):this.downloadStatusIndicator.data("tooltip","No Active Downloads").addClass("inactive").find(E.getCustomTag("downloadCount")).content("").hide(),isEmpty(this.downloadWindow)||this.downloadWindow.content.update(this.currentDownloads)}async initialize(){this.downloadStatusIndicator=E.downloadStatus().content(E.i().class("fa-solid fa-download"),E.downloadCount().hide()).on("click",(()=>this.showDownloads())),await this.checkDownloads(),document.querySelector("header").appendChild(await this.downloadStatusIndicator.render())}}export{DownloadsController};
