"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VcsFactory = void 0;
const GithubApi_1 = require("./GithubApi");
const GitlabApi_1 = require("./GitlabApi");
const utils_1 = require("../../utils");
const ServerFactory_1 = __importDefault(require("../../ServerFactory"));
const vcs_1 = require("../../../shared/constants/vcs");
class VcsFactory {
    static createVcs(vcs, token) {
        const apiConfig = {
            token,
            baseUrl: vcs.baseUrl,
            vcsId: vcs.id,
            maxFileSize: vcs.maxFileSize || vcs_1.VCS_DEFAULT_MAX_FILE_SIZE,
        };
        switch (vcs.type) {
            case 'github':
                return new GithubApi_1.GithubApi(apiConfig);
            case 'gitlab':
                return new GitlabApi_1.GitlabApi(apiConfig);
            default: {
                const vcsApi = ServerFactory_1.default.createCustomVcsApi(vcs.type, vcs, token);
                if (!vcsApi)
                    throw new utils_1.ErrorWithCode(400, `Unsupported VCS type`);
                return vcsApi;
            }
        }
    }
    static getVcsSettings(req) {
        const { uiSettings } = req.ctx.config;
        if (!('vcsSettings' in uiSettings))
            throw new utils_1.ErrorWithCode(404, 'vcsSettings is not found in UISettings');
        return uiSettings.vcsSettings;
    }
    static getVcsSettingById(req, vcsId) {
        if (!vcsId)
            throw new utils_1.ErrorWithCode(400, 'vcsId is required');
        const result = this.getVcsSettings(req).find((vcs) => vcs.id === vcsId);
        if (!result)
            throw new utils_1.ErrorWithCode(500, 'The current vcsId is missing from the vcsSettings setting in UISettings');
        return result;
    }
}
exports.VcsFactory = VcsFactory;
