"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 database_1 = require("../database");
const Scanner_1 = require("../features/scanner/Scanner");
const storyboard_1 = require("storyboard");
const http_event_1 = require("../http-event");
const Agent = require("../features/aquarelle/AquarelleAgent");
const utils = require("../features/utils/library-utils");
function downloadCovers() {
    return __awaiter(this, void 0, void 0, function* () {
        yield Agent.processAlbums().catch((err) => {
            storyboard_1.mainStory.error('aquarelle', err.message);
        });
        yield Agent.processArtists().catch((err) => {
            storyboard_1.mainStory.error('aquarelle', err.message);
        });
    });
}
function default_1(app) {
    app.post('/api/scans', (req, res) => {
        const id = req.body.libraryId;
        const full = req.body.full;
        const libraries = new database_1.default('libraries');
        libraries.get(id).then(() => {
            if (full) {
                return utils.resetLibrary(id);
            }
            return Promise.resolve();
        }).then(() => {
            const scanner = new Scanner_1.Scanner(id);
            const ts = Date.now();
            const scanFinish = `finish_${ts}`;
            const scanError = `error_${ts}`;
            const openFolder = `open_folder_${ts}`;
            const scanFinishToken = http_event_1.default.createEventThread(scanFinish);
            const scanErrorToken = http_event_1.default.createEventThread(scanError);
            const openFolderToken = http_event_1.default.createEventThread(openFolder);
            scanner.on('open_folder', (folder) => {
                http_event_1.default.emit(openFolder, { folder });
            });
            scanner.scan(database_1.default).then(downloadCovers).then(() => {
                http_event_1.default.emit(scanFinish, { ok: true });
            }).catch((err) => {
                storyboard_1.mainStory.error('scanner', err.message, { attach: err });
                http_event_1.default.emit(scanError, { error: err, ok: false });
            });
            res.status(201).send({
                status: 201,
                response: 'Scan started',
                events: {
                    finish: scanFinishToken,
                    error: scanErrorToken,
                    open_folder: openFolderToken
                }
            });
        }).catch((err) => {
            storyboard_1.mainStory.error('aquarelle', err.message, { attach: err });
            res.status(400).send({
                status: 400,
                response: 'Couldnt start scan'
            });
        });
    });
}
exports.default = default_1;
//# sourceMappingURL=scanner.js.map