"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const datasource_1 = require("../features/datasource");
const storyboard_1 = require("storyboard");
const config_1 = require("../config");
function default_1(app) {
    const source = new datasource_1.MediaSource(config_1.default.get('datasourceKey'));
    app.get('/api/datasource/search', (req, res) => {
        const { query, type } = req.query;
        source.search(query, type).then((results) => {
            const reduced = results.reduce((acc, val) => {
                return Object.assign({}, acc, {
                    [val.type]: (acc[val.type] || []).concat(val)
                });
            }, {});
            res.status(200).send(reduced);
        }).catch((err) => {
            storyboard_1.mainStory.error('datasource', 'An error happened while trying to fetch results', { attach: err });
            res.status(500).send({ error: 'An internal error happened. Check logs for more details.' });
        });
    });
    app.get('/api/datasource/artists/:id', (req, res) => {
        const { id } = req.params;
        source.getArtistById(id).then((artist) => {
            res.status(200).send(artist);
        }).catch((err) => {
            storyboard_1.mainStory.error('datasource', 'An error happened while trying to fetch artist', { attach: err });
            res.status(500).send({ error: 'An internal error happened. Check logs for more details.' });
        });
    });
    app.get('/api/datasource/albums/:id*', (req, res) => {
        const { id } = req.params;
        source.getAlbumById(id + req.params['0']).then((album) => {
            res.status(200).send(album);
        }).catch((err) => {
            storyboard_1.mainStory.error('datasource', 'An error happened while trying to fetch album', { attach: err });
            res.status(500).send({ error: 'An internal error happened. Check logs for more details.' });
        });
    });
    app.get('/api/datasource/autocomplete', (req, res) => {
        const { query, type } = req.query;
        source.autocomplete(query, type).then((results) => {
            const reduced = results.reduce((acc, val) => {
                return Object.assign({}, acc, { [val.type]: (acc[val.type] || []).concat(val) });
            }, {});
            res.status(200).send(reduced);
        }).catch((err) => {
            storyboard_1.mainStory.error('datasource', 'An error happened while trying to fetch results', { attach: err });
            res.status(500).send({ error: 'An internal error happened. Check logs for more details.' });
        });
    });
}
exports.default = default_1;
//# sourceMappingURL=datasource.js.map