"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const storyboard_1 = require("storyboard");
function getIDs(pouch, db) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = new pouch(db);
        const res = yield data.allDocs({ include_docs: false });
        const ids = res.rows.map((doc) => ({ id: doc.id, rev: doc.value.rev }));
        return [data, ids];
    });
}
function clean(pouch, dryRun = false) {
    return __awaiter(this, void 0, void 0, function* () {
        let didSomething = false;
        const [files, filesId] = yield getIDs(pouch, 'files');
        const [tracks, tracksId] = yield getIDs(pouch, 'tracks');
        const widowTracks = tracksId.filter((track) => {
            return !filesId.find((file) => file.id.startsWith(`${track.id}/`));
        });
        storyboard_1.mainStory.debug('vacuum', `Removing ${widowTracks.length} tracks`, {
            attach: widowTracks, attachLevel: 'debug'
        });
        if (widowTracks.length)
            didSomething = true;
        if (!dryRun) {
            yield Promise.all(widowTracks.map((doc) => {
                return tracks.remove(doc.id, doc.rev);
            }));
        }
        const [albums, albumsId] = yield getIDs(pouch, 'albums');
        const widowAlbums = albumsId.filter((album) => {
            return !tracksId.find((track) => track.id.startsWith(`${album.id}/`));
            // || widowTracks.find((track) => track.id.startsWith(`${album.id}/`));
        });
        if (widowAlbums.length)
            didSomething = true;
        storyboard_1.mainStory.debug('vacuum', `Removing ${widowAlbums.length} albums`, {
            attach: widowAlbums, attachLevel: 'debug'
        });
        if (!dryRun) {
            yield Promise.all(widowAlbums.map((doc) => {
                return albums.remove(doc.id, doc.rev);
            }));
        }
        const [artists, artistsId] = yield getIDs(pouch, 'artists');
        const widowArtists = artistsId.filter((artist) => {
            return !albumsId.find((album) => album.id.startsWith(`${artist.id}/`));
            // || widowAlbums.find((album) => album.id.startsWith(`${artist.id}/`));
        });
        if (widowArtists.length)
            didSomething = true;
        storyboard_1.mainStory.debug('vacuum', `Removing ${widowArtists.length} artists`, {
            attach: widowArtists, attachLevel: 'debug'
        });
        if (!dryRun) {
            yield Promise.all(widowArtists.map((doc) => {
                return artists.remove(doc.id, doc.rev);
            }));
        }
        if (didSomething) {
            yield clean(pouch, dryRun);
        }
    });
}
exports.default = clean;
//# sourceMappingURL=library-cleaner.js.map