"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DistTagEvaluator = void 0;
exports.isMinUI5VersionVariable = isMinUI5VersionVariable;
const ux_specification_types_1 = require("@sap/ux-specification-types");
const extensionLogger_1 = require("../../extensionLogger");
const i18next_1 = __importDefault(require("i18next"));
const distTagsJSON = __importStar(require("./dist_tag.json"));
const package_json_1 = require("../../../package.json");
/**
 * Checks if the provided minUI5Version is a variable (starts with '$').
 *
 * @param minUI5Version - The minimum UI5 version to check.
 * @returns {boolean} - true if minUI5Version is a variable, false otherwise.
 */
function isMinUI5VersionVariable(minUI5Version) {
    return minUI5Version.startsWith('$');
}
class DistTagEvaluator {
    static { this.distTagsOfSpec = []; }
    constructor() {
        if (DistTagEvaluator.distTagsOfSpec.length === 0) {
            DistTagEvaluator.distTagsOfSpec = this.getDistTagsOfVersion();
        }
    }
    /**
     * Resets the buffered list of dist tags (used for unit tests).
     */
    static resetDistTagsOfSpec() {
        DistTagEvaluator.distTagsOfSpec = [];
    }
    /**
     * Retrieves the actual spec version from package.json.
     *
     * @returns the version, if found in package.json
     */
    getSpecVersion() {
        return package_json_1.version;
    }
    /**
     * Determines the relevant list of dist tags for the current spec version.
     * The dist tags are read from the dist_tag.json (copy from https://github.wdf.sap.corp/NPMJS/FIORI_ELEMENT_SPECIFICATION-1.0.0/blob/master/dist_tag.json).
     * The spec vrsion is read from package.json.
     *
     * @returns an array of the dist tags
     */
    getDistTagsOfVersion() {
        let result = [];
        const distTags = distTagsJSON.distTags;
        let specVersion = this.getSpecVersion();
        if (specVersion) {
            if (specVersion.startsWith('9.9.9')) {
                //master ==> take the newest version
                result = distTags[0].distTag;
            }
            else {
                specVersion = specVersion.substring(0, specVersion.lastIndexOf('.'));
                const tag = distTags.find((tag) => tag.version.startsWith(specVersion));
                if (tag) {
                    result = tag.distTag;
                }
            }
        }
        return result;
    }
    /**
     * Checks if the given minUI5Version and spec version fit together, based on the distTags stord in ./dist_tag.json.
     *
     * @param {string} minUI5Version - minUI5Version from manifest
     * @param {ExtensionLogger} logger - logger as passed from the API
     */
    doesUi5VersionFitDistTags(minUI5Version, logger) {
        //env variable is used by tests to disable the check, as test projects may not correspond to the given spec version
        if (process.env.SPEC_NO_VERSION_CHECK === 'true') {
            return;
        }
        if (!minUI5Version || minUI5Version.startsWith('$')) {
            const message = i18next_1.default.t('UNKNOWNMINUI5VERSION', { minUI5Version });
            (0, extensionLogger_1.log)(logger, {
                severity: "info" /* LogSeverity.Info */,
                message,
                location: {
                    path: ux_specification_types_1.MANIFESTPATH
                }
            });
            return;
        }
        const actualVersion = minUI5Version === 'latest' ? 'latest' : minUI5Version.substring(0, minUI5Version.lastIndexOf('.'));
        if (DistTagEvaluator.distTagsOfSpec.findIndex((distTagVersion) => distTagVersion.endsWith(actualVersion)) === -1) {
            const message = i18next_1.default.t('WRONGMINUI5VERSION', { minUI5Version });
            (0, extensionLogger_1.log)(logger, {
                severity: "warning" /* LogSeverity.Warning */,
                message,
                location: {
                    path: ux_specification_types_1.MANIFESTPATH
                }
            });
        }
    }
}
exports.DistTagEvaluator = DistTagEvaluator;
//# sourceMappingURL=distTagEvaluator.js.map