"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());
    });
};
const { generateDatasourceID, getDatasourceParams, getQueryParams, DocumentTypes, BudibaseInternalDB, getTableParams, } = require("../../db/utils");
const { BuildSchemaErrors, InvalidColumns } = require("../../constants");
const { integrations } = require("../../integrations");
const { getDatasourceAndQuery } = require("./row/utils");
const { invalidateDynamicVariables } = require("../../threads/utils");
const { getAppDB } = require("@devlego/backend-core/context");
const { events } = require("@devlego/backend-core");
exports.fetch = function (ctx) {
    return __awaiter(this, void 0, void 0, function* () {
        // Get internal tables
        const db = getAppDB();
        const internalTables = yield db.allDocs(getTableParams(null, {
            include_docs: true,
        }));
        const internal = internalTables.rows.map(row => row.doc);
        const bbInternalDb = Object.assign(Object.assign({}, BudibaseInternalDB), { entities: internal });
        // Get external datasources
        const datasources = (yield db.allDocs(getDatasourceParams(null, {
            include_docs: true,
        }))).rows.map(row => row.doc);
        for (let datasource of datasources) {
            if (datasource.config && datasource.config.auth) {
                // strip secrets from response so they don't show in the network request
                delete datasource.config.auth;
            }
        }
        ctx.body = [bbInternalDb, ...datasources];
    });
};
exports.buildSchemaFromDb = function (ctx) {
    return __awaiter(this, void 0, void 0, function* () {
        const db = getAppDB();
        const datasource = yield db.get(ctx.params.datasourceId);
        const { tables, error } = yield buildSchemaHelper(datasource);
        datasource.entities = tables;
        const dbResp = yield db.put(datasource);
        datasource._rev = dbResp.rev;
        const response = { datasource };
        if (error) {
            response.error = error;
        }
        ctx.body = response;
    });
};
/**
 * Check for variables that have been updated or removed and invalidate them.
 */
const invalidateVariables = (existingDatasource, updatedDatasource) => __awaiter(void 0, void 0, void 0, function* () {
    const existingVariables = existingDatasource.config.dynamicVariables;
    const updatedVariables = updatedDatasource.config.dynamicVariables;
    const toInvalidate = [];
    if (!existingVariables) {
        return;
    }
    if (!updatedVariables) {
        // invalidate all
        toInvalidate.push(...existingVariables);
    }
    else {
        // invaldate changed / removed
        existingVariables.forEach(existing => {
            const unchanged = updatedVariables.find(updated => existing.name === updated.name &&
                existing.queryId === updated.queryId &&
                existing.value === updated.value);
            if (!unchanged) {
                toInvalidate.push(existing);
            }
        });
    }
    yield invalidateDynamicVariables(toInvalidate);
});
exports.update = function (ctx) {
    return __awaiter(this, void 0, void 0, function* () {
        const db = getAppDB();
        const datasourceId = ctx.params.datasourceId;
        let datasource = yield db.get(datasourceId);
        const auth = datasource.config.auth;
        yield invalidateVariables(datasource, ctx.request.body);
        datasource = Object.assign(Object.assign({}, datasource), ctx.request.body);
        if (auth && !ctx.request.body.auth) {
            // don't strip auth config from DB
            datasource.config.auth = auth;
        }
        const response = yield db.put(datasource);
        yield events.datasource.updated(datasource);
        datasource._rev = response.rev;
        // Drain connection pools when configuration is changed
        if (datasource.source) {
            const source = integrations[datasource.source];
            if (source && source.pool) {
                yield source.pool.end();
            }
        }
        ctx.status = 200;
        ctx.message = "Datasource saved successfully.";
        ctx.body = { datasource };
    });
};
exports.save = function (ctx) {
    return __awaiter(this, void 0, void 0, function* () {
        const db = getAppDB();
        const plus = ctx.request.body.datasource.plus;
        const fetchSchema = ctx.request.body.fetchSchema;
        const datasource = Object.assign({ _id: generateDatasourceID({ plus }), type: plus ? DocumentTypes.DATASOURCE_PLUS : DocumentTypes.DATASOURCE }, ctx.request.body.datasource);
        let schemaError = null;
        if (fetchSchema) {
            const { tables, error } = yield buildSchemaHelper(datasource);
            schemaError = error;
            datasource.entities = tables;
        }
        const dbResp = yield db.put(datasource);
        yield events.datasource.created(datasource);
        datasource._rev = dbResp.rev;
        // Drain connection pools when configuration is changed
        if (datasource.source) {
            const source = integrations[datasource.source];
            if (source && source.pool) {
                yield source.pool.end();
            }
        }
        const response = { datasource };
        if (schemaError) {
            response.error = schemaError;
        }
        ctx.body = response;
    });
};
exports.destroy = function (ctx) {
    return __awaiter(this, void 0, void 0, function* () {
        const db = getAppDB();
        const datasourceId = ctx.params.datasourceId;
        const datasource = yield db.get(datasourceId);
        // Delete all queries for the datasource
        const queries = yield db.allDocs(getQueryParams(datasourceId, null));
        yield db.bulkDocs(queries.rows.map(row => ({
            _id: row.id,
            _rev: row.value.rev,
            _deleted: true,
        })));
        // delete the datasource
        yield db.remove(datasourceId, ctx.params.revId);
        yield events.datasource.deleted(datasource);
        ctx.message = `Datasource deleted.`;
        ctx.status = 200;
    });
};
exports.find = function (ctx) {
    return __awaiter(this, void 0, void 0, function* () {
        const database = getAppDB();
        ctx.body = yield database.get(ctx.params.datasourceId);
    });
};
// dynamic query functionality
exports.query = function (ctx) {
    return __awaiter(this, void 0, void 0, function* () {
        const queryJson = ctx.request.body;
        try {
            ctx.body = yield getDatasourceAndQuery(queryJson);
        }
        catch (err) {
            ctx.throw(400, err);
        }
    });
};
function getErrorTables(errors, errorType) {
    return Object.entries(errors)
        .filter(entry => entry[1] === errorType)
        .map(([name]) => name);
}
function updateError(error, newError, tables) {
    if (!error) {
        error = "";
    }
    if (error.length > 0) {
        error += "\n";
    }
    error += `${newError} ${tables.join(", ")}`;
    return error;
}
const buildSchemaHelper = (datasource) => __awaiter(void 0, void 0, void 0, function* () {
    const Connector = integrations[datasource.source];
    // Connect to the DB and build the schema
    const connector = new Connector(datasource.config);
    yield connector.buildSchema(datasource._id, datasource.entities);
    datasource.entities = connector.tables;
    // make sure they all have a display name selected
    for (let entity of Object.values(datasource.entities)) {
        if (entity.primaryDisplay) {
            continue;
        }
        const notAutoColumn = Object.values(entity.schema).find(schema => !schema.autocolumn);
        if (notAutoColumn) {
            entity.primaryDisplay = notAutoColumn.name;
        }
    }
    const errors = connector.schemaErrors;
    let error = null;
    if (errors && Object.keys(errors).length > 0) {
        const noKey = getErrorTables(errors, BuildSchemaErrors.NO_KEY);
        const invalidCol = getErrorTables(errors, BuildSchemaErrors.INVALID_COLUMN);
        if (noKey.length) {
            error = updateError(error, "No primary key constraint found for the following:", noKey);
        }
        if (invalidCol.length) {
            const invalidCols = Object.values(InvalidColumns).join(", ");
            error = updateError(error, `Cannot use columns ${invalidCols} found in following:`, invalidCol);
        }
    }
    return { tables: connector.tables, error };
});
