import * as React from "react";
import { FEAGlobals } from "../../../common/Globals";
import { ButtonIcon } from "../common/ButtonIcon";
import "../../assets/css/download.css";
const renderPauseButton = (id) => (React.createElement(ButtonIcon, { displayText: true, className: "finsemble-download-pause", text: "Pause", icon: "pause", onClick: () => {
        FEAGlobals.fin.desktop.System.pauseDownload(id);
    } }));
const renderCancelButton = (id) => (React.createElement(ButtonIcon, { displayText: true, className: "finsemble-download-cancel", text: "Cancel", icon: "cancel", onClick: () => {
        FEAGlobals.fin.desktop.System.cancelDownload(id);
    } }));
const renderResumeButton = (id) => (React.createElement(ButtonIcon, { displayText: true, className: "finsemble-download-resume", text: "Resume", icon: "resume", onClick: () => {
        FEAGlobals.fin.desktop.System.resumeDownload(id);
    } }));
const renderShowInFolderButton = (id) => (React.createElement(ButtonIcon, { className: "finsemble-download-folder", displayText: true, text: "Show in folder", icon: "search", onClick: () => {
        FEAGlobals.fin.desktop.System.showDownloadedFileInFolder(id);
    } }));
/**
 * Determines which buttons to render based on the current state.
 * The available actions are:
 * Progressing(download in progress): pause, cancel
 * Paused: resume, cancel
 * Completed: show in folder
 * Canceled, interrupted(download failed): none - it's currently not possible to resume/retry a canceled download
 * All downloads in a state of "completion" (completed, cancelled, interrupted, file-deleted) can be removed from the list
 */
const renderButtons = (id, state) => {
    switch (state) {
        case "progressing":
            return (React.createElement("span", null,
                renderPauseButton(id),
                renderCancelButton(id)));
        case "paused":
            return (React.createElement("span", null,
                renderResumeButton(id),
                renderCancelButton(id)));
        case "completed":
            return renderShowInFolderButton(id);
        case "cancelled":
        case "interrupted":
        case "file-deleted":
        default:
            break;
    }
};
const bytesToMB = (bytes) => Math.round((bytes / (1024 * 1024)) * 1e2) / 1e2;
/**
 * Entry for each individual download with information about the download and it's state.
 * Downloads can be paused/canceled/resumed based on completion status.
 * A removal function is passed down from the parent Download Manager to allow removing downloads based on completion state
 */
export const DownloadItem = ({ downloadItem, remove }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    // Completed, canceled, interrupted downloads can be removed
    const removableState = ((_a = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _a === void 0 ? void 0 : _a.state) === "completed" ||
        ((_b = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _b === void 0 ? void 0 : _b.state) === "cancelled" ||
        ((_c = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _c === void 0 ? void 0 : _c.state) === "file-deleted" ||
        ((_d = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _d === void 0 ? void 0 : _d.state) === "interrupted";
    const fileDeleted = ((_e = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _e === void 0 ? void 0 : _e.state) === "file-deleted";
    const receivedMB = bytesToMB((_f = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _f === void 0 ? void 0 : _f.receivedBytes);
    const totalMB = bytesToMB((_g = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _g === void 0 ? void 0 : _g.totalBytes);
    const removeDownloadItem = async () => {
        await FEAGlobals.fin.desktop.System.removeDownloadItem(downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.id);
        remove(downloadItem);
    };
    return (React.createElement("div", { className: "download-row" },
        React.createElement("div", { className: "download-remove" }, removableState && (React.createElement(ButtonIcon, { text: "Cancel", className: "finsemble-download-remove", icon: "times", onClick: removeDownloadItem }))),
        React.createElement("div", { className: `download-name ${fileDeleted ? "file-deleted" : ""}` }, (_h = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _h === void 0 ? void 0 : _h.filename),
        React.createElement("div", { title: (_j = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _j === void 0 ? void 0 : _j.url, className: "download-url" }, (_k = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _k === void 0 ? void 0 : _k.url),
        React.createElement("div", { className: "download-progress-button-row" },
            React.createElement("span", { className: "download-progress" },
                receivedMB,
                "MB/",
                totalMB,
                "MB"),
            React.createElement("span", { className: "download-buttons" }, renderButtons(downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.id, (_l = downloadItem === null || downloadItem === void 0 ? void 0 : downloadItem.displayInfo) === null || _l === void 0 ? void 0 : _l.state)))));
};
//# sourceMappingURL=DownloadItem.js.map