"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 });
exports.validateFrameworkVersion = void 0;
const content_checker_1 = require("@ewizardjs/content-checker");
const ramda_1 = require("ramda");
const semver = require("semver");
const Config_1 = require("../../modules/Config");
const I18n_1 = require("../../modules/I18n");
const Logger_1 = require("../../modules/Logger");
const getTerminalLink_1 = require("../../utils/getTerminalLink");
const urljoin = require("url-join");
const DOCS_OPTIONS = {
    EWIZARD_URL: 'https://viseven.ewizard.io',
    INSTALL_TITLE: 'wiz install ewizardjs',
    INSTALL_ROUTE: '/docs/ewizardjs/documentation/guide/cli/development.html#wiz-install-options',
    NEW_VERSION_TITLE: 'eWizard.js version 5',
    NEW_VERSION_ROUTE: '/docs/ewizardjs/documentation/guide/version-5.html',
};
function validateFrameworkVersion(contentPath) {
    return __awaiter(this, void 0, void 0, function* () {
        const { name, version } = yield getFrameworkInfo(contentPath);
        if (!(name && version && typeof version === 'string')) {
            return;
        }
        const { deprecatedEwizardjsVersions, ewizard: { url: ewizardUrl }, } = yield Config_1.Config.get();
        switch (name) {
            case content_checker_1.FrameworkList.Ewizardjs:
                return validateEwizardjsVersion(version, deprecatedEwizardjsVersions, ewizardUrl);
            case content_checker_1.FrameworkList.VueEngine:
                return validateVueEngineVersion(ewizardUrl);
            default:
                return;
        }
    });
}
exports.validateFrameworkVersion = validateFrameworkVersion;
function getFrameworkInfo(contentPath) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const { name, version } = yield (0, content_checker_1.getFrameworkInfo)(contentPath);
            return { name, version };
        }
        catch (_a) {
            return { name: '', version: '' };
        }
    });
}
function validateEwizardjsVersion(currentVersion, deprecatedVersionsRange, ewizardUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        if (isVersionDeprecated(currentVersion, deprecatedVersionsRange)) {
            const docsOptions = {
                EWIZARD_URL: ewizardUrl,
                INSTALL_TITLE: `${DOCS_OPTIONS.INSTALL_TITLE}@latest`,
            };
            showWarning('DEPRECATED_EWIZARDJS', docsOptions);
        }
    });
}
function validateVueEngineVersion(ewizardUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        const docsOptions = { EWIZARD_URL: ewizardUrl };
        showWarning('DEPRECATED_VUE_ENGINE', docsOptions);
    });
}
function isVersionDeprecated(currentVersion, deprecatedVersionsRange) {
    return semver.satisfies(currentVersion, deprecatedVersionsRange);
}
function showWarning(messageKey, options) {
    const { install, newVersion } = getLinks(options);
    Logger_1.default.warn(I18n_1.default.info(messageKey, install, newVersion));
}
function getLinks(options = {}) {
    const { EWIZARD_URL, INSTALL_TITLE, INSTALL_ROUTE, NEW_VERSION_TITLE, NEW_VERSION_ROUTE, } = (0, ramda_1.mergeRight)(DOCS_OPTIONS, options);
    return {
        install: getLinkMessage(EWIZARD_URL, INSTALL_TITLE, INSTALL_ROUTE),
        newVersion: getLinkMessage(EWIZARD_URL, NEW_VERSION_TITLE, NEW_VERSION_ROUTE),
    };
}
function getLinkMessage(ewizardUrl, title, route) {
    const docsLink = urljoin(ewizardUrl, route);
    return (0, getTerminalLink_1.getTerminalLink)(title, docsLink);
}
//# sourceMappingURL=validateFrameworkVersion.js.map