"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mapping_1 = __importDefault(require("../../../controllers/public/mapping"));
var Resources;
(function (Resources) {
    Resources["APPLICATION"] = "applications";
    Resources["TABLES"] = "tables";
    Resources["ROWS"] = "rows";
    Resources["USERS"] = "users";
    Resources["QUERIES"] = "queries";
    Resources["SEARCH"] = "search";
})(Resources || (Resources = {}));
function isArrayResponse(ctx) {
    return ctx.url.endsWith(Resources.SEARCH) || Array.isArray(ctx.body);
}
function processApplications(ctx) {
    if (isArrayResponse(ctx)) {
        return mapping_1.default.mapApplications(ctx);
    }
    else {
        return mapping_1.default.mapApplication(ctx);
    }
}
function processTables(ctx) {
    if (isArrayResponse(ctx)) {
        return mapping_1.default.mapTables(ctx);
    }
    else {
        return mapping_1.default.mapTable(ctx);
    }
}
function processRows(ctx) {
    if (isArrayResponse(ctx)) {
        return mapping_1.default.mapRowSearch(ctx);
    }
    else {
        return mapping_1.default.mapRow(ctx);
    }
}
function processUsers(ctx) {
    if (isArrayResponse(ctx)) {
        return mapping_1.default.mapUsers(ctx);
    }
    else {
        return mapping_1.default.mapUser(ctx);
    }
}
function processQueries(ctx) {
    if (isArrayResponse(ctx)) {
        return mapping_1.default.mapQueries(ctx);
    }
    else {
        return mapping_1.default.mapQueryExecution(ctx);
    }
}
exports.default = (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
    let urlParts = ctx.url.split("/");
    urlParts = urlParts.slice(4, urlParts.length);
    let body = {};
    switch (urlParts[0]) {
        case Resources.APPLICATION:
            body = processApplications(ctx);
            break;
        case Resources.TABLES:
            if (urlParts[2] === Resources.ROWS) {
                body = processRows(ctx);
            }
            else {
                body = processTables(ctx);
            }
            break;
        case Resources.USERS:
            body = processUsers(ctx);
            break;
        case Resources.QUERIES:
            body = processQueries(ctx);
            break;
    }
    // update the body based on what has occurred in the mapper
    ctx.body = body;
    yield next();
});
