"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 Agent = require("../features/aquarelle/AquarelleAgent");
const sharp = require("sharp");
const database_1 = require("../database");
const storyboard_1 = require("storyboard");
const trickle = require('timetrickle');
function default_1(app) {
    const limit = trickle(10, 250);
    app.post('/api/aquarelle', (req, res) => {
        Agent.processAlbums().catch((err) => {
            storyboard_1.mainStory.error('aquarelle', err.message, { attach: err });
        });
        Agent.processArtists().catch((err) => {
            storyboard_1.mainStory.error('aquarelle', err.message, { attach: err });
        });
        res.status(201).send({ ok: true });
    });
    app.get('/api/aquarelle/:artist', (req, res) => {
        limit(() => __awaiter(this, void 0, void 0, function* () {
            const { artist } = req.params;
            const size = /^\d+$/.test(req.query.s) ? +req.query.s : 300;
            const arts = new database_1.default('artworks');
            const file = yield arts.getAttachment('artworks/library/' + artist, size > 64 ? 'large' : 'small');
            res.contentType('image/png');
            res.setHeader('Cache-Control', 'max-age=900');
            if (size !== 300) {
                return sharp(file).resize(size).toBuffer().then((buffer) => {
                    res.send(buffer);
                });
            }
            res.writeHead(200, {});
            res.send(file);
        }));
    });
    app.get('/api/aquarelle/:artist/:album', (req, res) => {
        limit(() => __awaiter(this, void 0, void 0, function* () {
            const { artist, album } = req.params;
            const size = /^\d+$/.test(req.query.s) ? +req.query.s : 300;
            const arts = new database_1.default('artworks');
            const file = yield arts.getAttachment('artworks/library/' + artist + '/' + album, size > 64 ? 'large' : 'small');
            res.contentType('image/png');
            res.setHeader('Cache-Control', 'max-age=900');
            if (size !== 300) {
                return sharp(file).resize(size).toBuffer().then((buffer) => {
                    res.send(buffer);
                });
            }
            res.writeHead(200, {});
            res.send(file);
        }));
    });
}
exports.default = default_1;
//# sourceMappingURL=aquarelle.js.map