"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_1 = require("@gitbeaker/node");
const winston = require("winston");
const semver = require("semver");
const TemplateTypesList_1 = require("../modules/TemplateTypesList");
const I18n_1 = require("../modules/I18n");
const WizError_1 = require("../modules/WizError");
const settingsJson = require('../../settings.json');
const gitlabApi = new node_1.Gitlab({ host: settingsJson.gitlab.url });
function checkTemplateVersion(content, version) {
    return __awaiter(this, void 0, void 0, function* () {
        const repositoryId = yield getRepositoryId(settingsJson.gitlab.scaffolding_group, content);
        const repositoryTags = yield getRepositoryTags(repositoryId);
        checkAvailableVersions(repositoryTags, version, content);
    });
}
exports.default = checkTemplateVersion;
function checkAvailableVersions(tags, version, content) {
    const availableVersions = tags.some((name) => name === version);
    const template = TemplateTypesList_1.getTemplateData(content);
    const versionByDefault = template.version;
    if (version && !tags.length) {
        throw new WizError_1.WizError('NOT_AVAILABLE_TEMPLATE_VERSION');
    }
    if (version && !availableVersions) {
        winston.info(I18n_1.default.info('AVAILABLE_TEMPLATE_VERSIONS'));
        tags.forEach((tag) => {
            const defaultMark = (tag === versionByDefault) ? '(default)' : '';
            console.log(tag, defaultMark);
        });
        throw new WizError_1.WizError('NOT_AVAILABLE_TEMPLATE_VERSION');
    }
    if (!version && versionByDefault) {
        winston.info(I18n_1.default.info('DEFAULT_TEMPLATE_VERSION', versionByDefault));
    }
}
function getRepositoryId(group, content) {
    return __awaiter(this, void 0, void 0, function* () {
        const { url } = yield TemplateTypesList_1.getTemplateData(content);
        const repositories = yield gitlabApi.Projects.all(group);
        return repositories.filter(({ web_url }) => web_url === url)[0].id;
    });
}
function getRepositoryTags(id) {
    return __awaiter(this, void 0, void 0, function* () {
        return gitlabApi.Tags.all(id)
            .then((tags) => tags
            .map(({ name: version }) => version)
            .sort(semver.rcompare));
    });
}
//# sourceMappingURL=checkTemplateVersion.js.map