
const Path = require('path');
const CONFIG = require('./config.js');
const Library = require('./library.js').Library;
const BuilderClass = require('./autotagBuilder');

function LibraryManager() {
    this.libraries = [];
    this.updating = false;

    for (let i of CONFIG.photoLibraries)
        this.push(i);
}

LibraryManager.prototype.push = function(path) {
    this.libraries.push(new Library(path));
}

LibraryManager.prototype.updateLibraries = async function(app) {
    if (this.updating)
        return;
    this.updating = true;
    await Promise.allSettled(this.libraries.map(i => i.updateLibrary(app.databaseHelper)));
    await BuilderClass.rebuildPathTags(app);
    this.updating = false;
}

LibraryManager.prototype.isUpdating = function()
{ return this.updating; }

LibraryManager.prototype.removeMedia = async function(path) {
    path = Path.normalize(path);
    for (let i of this.libraries) {
        if (await i.removeMedia(path))
            return true;
    }
    return false;
}

LibraryManager.prototype.findMedia = async function(path) {
    path = Path.normalize(path);
    for (let i of this.libraries) {
        let media = await i.findMedia(path);
        if (media)
            return media;
    }
    return null;
}

module.exports.LibraryManager = new LibraryManager();

