const errList = require('../includes/errorManager');

function send(req, res) {
    if ('db' in res && typeof res.db == 'object' && 'close' in res.db && typeof res.db.close == 'function') res.db.close();

    let lngs = req.headers['accept-language'].split(',');
    let lng = null;
    if (lngs.length > 0) {
        lng = lngs[0].substring(0, 2);
    }

    let json = {};

    if (!('api_res' in res)) {
        json = {
            errorCode: 'ERR9999',
            message: 'api not found',
            status: 0
        };
    } else if (typeof res.api_res != 'object') {
        json = {
            errorCode: res.api_res,
            message: errList(res.api_res, lng),
            status: 0
        };
    } else {
        json = {
            data: res.api_res,
            status: 1
        };
    }

    res.send(json);
}

module.exports = { send: send };