"use strict";var l=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var r=(s,e)=>l(s,"name",{value:e,configurable:true});var S=(s,e)=>{for(var n in e)l(s,n,{get:e[n],enumerable:true})};var L=(s,e,n,o)=>{if(e&&typeof e==="object"||typeof e==="function"){for(let t of h(e))if(!I.call(s,t)&&t!==n)l(s,t,{get:()=>e[t],enumerable:!(o=D(e,t))||o.enumerable})}return s};var k=s=>L(l({},"__esModule",{value:true}),s);var m={};S(m,{default:()=>w,downloadController:()=>f});module.exports=k(m);var g=require("node:path");var c=require("electron");var v=require("uuid");const d=require("../preload-safe-debug")("Ferdium:DownloadController");class w{activeDownloads=new Map;registeredSessions=new WeakSet;sessionDownloadListeners=new WeakMap;sessionServiceIds=new WeakMap;webContentsServiceIds=new Map;sessionRegistrationCount=0;isListeningForIpc=false;ipc;createDownloadId;getAppActions;constructor({ipc:e=c.ipcRenderer,createDownloadId:n=v.v4,getAppActions:o=r(()=>window["ferdium"].actions.app,"getAppActions")}={}){this.ipc=e;this.createDownloadId=n;this.getAppActions=o}get activeDownloadCount(){return this.activeDownloads.size}get registeredSessionCount(){return this.sessionRegistrationCount}registerWebContents({serviceId:e,webContents:n}){this.registerSession(n.session);this.registerServiceIdForSession(n.session,e);const o=this.getWebContentsId(n);if(o===null){return}this.webContentsServiceIds.set(o,e);n.once?.("destroyed",()=>{this.unregisterWebContents(o)})}unregisterWebContents(e){this.webContentsServiceIds.delete(e)}trackDownload({item:e,serviceId:n}){this.ensureIpcListeners();const o=this.createDownloadId();const t=this.getAppActions();this.activeDownloads.set(o,e);t.addDownload({id:o,serviceId:n,filename:e.getFilename(),url:e.getURL(),savePath:e.getSavePath()});const a=r((p,i)=>{if(i==="interrupted"){d("Download is interrupted but can be resumed")}else if(i==="progressing"){if(e.isPaused()){d("Download is paused")}else{d(`Received bytes: ${e.getReceivedBytes()}`)}}t.updateDownload({id:o,serviceId:n,filename:(0,g.basename)(e.getSavePath()),url:e.getURL(),savePath:e.getSavePath(),receivedBytes:e.getReceivedBytes(),totalBytes:e.getTotalBytes(),state:i});d("download updated",i)},"handleUpdated");const u=r((p,i)=>{d("download done",i);e.removeListener("updated",a);this.activeDownloads.delete(o);if(i==="completed"){d("Download successfully")}else{if(i==="cancelled"&&e.getSavePath()===""){t.removeDownload(o);d("Download is cancelled")}d(`Download failed: ${i}`)}t.endedDownload({id:o,serviceId:n,receivedBytes:e.getReceivedBytes(),totalBytes:e.getTotalBytes(),state:i})},"handleDone");e.addListener("updated",a);e.once("done",u);return o}dispose(){if(this.isListeningForIpc){this.ipc.removeListener("toggle-pause-download",this.handleTogglePauseDownload);this.ipc.removeListener("stop-download",this.handleStopDownload);this.isListeningForIpc=false}this.activeDownloads.clear();this.webContentsServiceIds.clear();this.registeredSessions=new WeakSet;this.sessionDownloadListeners=new WeakMap;this.sessionServiceIds=new WeakMap;this.sessionRegistrationCount=0}registerSession(e){if(this.registeredSessions.has(e)){return}const n=r((o,t,a)=>{this.handleWillDownload(e,o,t,a)},"listener");e.on("will-download",n);this.registeredSessions.add(e);this.sessionDownloadListeners.set(e,n);this.sessionRegistrationCount+=1}registerServiceIdForSession(e,n){const o=this.sessionServiceIds.get(e)??new Set;o.add(n);this.sessionServiceIds.set(e,o)}getWebContentsId(e){if(!e){return null}if(typeof e.id==="number"){return e.id}if(typeof e.getId==="function"){return e.getId()}return null}getServiceIdForDownload(e,n){const o=this.getWebContentsId(n);if(o!==null){const a=this.webContentsServiceIds.get(o);if(a){return a}}const t=this.sessionServiceIds.get(e);if(t?.size===1){return[...t][0]}return""}handleWillDownload=(e,n,o,t)=>{n.preventDefault();this.trackDownload({item:o,serviceId:this.getServiceIdForDownload(e,t)})};ensureIpcListeners(){if(this.isListeningForIpc){return}this.ipc.on("toggle-pause-download",this.handleTogglePauseDownload);this.ipc.on("stop-download",this.handleStopDownload);this.isListeningForIpc=true}getTargetDownloads(e){if(!e){return[...this.activeDownloads.values()]}const n=this.activeDownloads.get(e);return n?[n]:[]}handleTogglePauseDownload=(e,n)=>{const o=this.getTargetDownloads(n?.downloadId);for(const t of o){d("toggle-pause-download",t.isPaused(),t.getState());if(t.isPaused()){t.resume()}else{t.pause()}d("toggle-pause-download",t.isPaused(),t.getState());this.getAppActions().updateDownload({id:this.getDownloadId(t),paused:t.isPaused()})}};handleStopDownload=(e,n)=>{const o=this.getTargetDownloads(n?.downloadId);for(const t of o){t.cancel()}};getDownloadId(e){for(const[n,o]of this.activeDownloads){if(o===e){return n}}return""}}r(w,"DownloadController");const f=new w;
