"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeWebTorrentFile = exports.removeAllWebTorrentFiles = exports.removeHLSFile = exports.removeHLSPlaylist = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../helpers/logger");
const shared_1 = require("./object-storage/shared");
function removeHLSPlaylist(video) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const hls = video.getHLSPlaylist();
        if (!hls)
            return;
        yield video.removeStreamingPlaylistFiles(hls);
        yield hls.destroy();
        video.VideoStreamingPlaylists = video.VideoStreamingPlaylists.filter(p => p.id !== hls.id);
    });
}
exports.removeHLSPlaylist = removeHLSPlaylist;
function removeHLSFile(video, fileToDeleteId) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Deleting HLS file %d of %s.', fileToDeleteId, video.url, (0, shared_1.lTags)(video.uuid));
        const hls = video.getHLSPlaylist();
        const files = hls.VideoFiles;
        if (files.length === 1) {
            yield removeHLSPlaylist(video);
            return undefined;
        }
        const toDelete = files.find(f => f.id === fileToDeleteId);
        yield video.removeStreamingPlaylistVideoFile(video.getHLSPlaylist(), toDelete);
        yield toDelete.destroy();
        hls.VideoFiles = hls.VideoFiles.filter(f => f.id !== toDelete.id);
        return hls;
    });
}
exports.removeHLSFile = removeHLSFile;
function removeAllWebTorrentFiles(video) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        for (const file of video.VideoFiles) {
            yield video.removeWebTorrentFile(file);
            yield file.destroy();
        }
        video.VideoFiles = [];
        return video;
    });
}
exports.removeAllWebTorrentFiles = removeAllWebTorrentFiles;
function removeWebTorrentFile(video, fileToDeleteId) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const files = video.VideoFiles;
        if (files.length === 1) {
            return removeAllWebTorrentFiles(video);
        }
        const toDelete = files.find(f => f.id === fileToDeleteId);
        yield video.removeWebTorrentFile(toDelete);
        yield toDelete.destroy();
        video.VideoFiles = files.filter(f => f.id !== toDelete.id);
        return video;
    });
}
exports.removeWebTorrentFile = removeWebTorrentFile;
//# sourceMappingURL=video-file.js.map