"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const semver = require("semver");
const util = require("util");
const constants_1 = require("../constants");
class BundleValidatorHelper {
    constructor($projectData, $errors, $options) {
        this.$projectData = $projectData;
        this.$errors = $errors;
        this.$options = $options;
        this.bundlersMap = {
            webpack: "nativescript-dev-webpack"
        };
        this.$projectData.initializeProjectData();
    }
    validate(minSupportedVersion) {
        if (this.$options.bundle) {
            const bundlePluginName = this.bundlersMap[this.$options.bundle];
            const bundlerVersionInDependencies = this.$projectData.dependencies && this.$projectData.dependencies[bundlePluginName];
            const bundlerVersionInDevDependencies = this.$projectData.devDependencies && this.$projectData.devDependencies[bundlePluginName];
            if (!bundlePluginName || (!bundlerVersionInDependencies && !bundlerVersionInDevDependencies)) {
                this.$errors.fail(constants_1.BundleValidatorMessages.MissingBundlePlugin);
            }
            if (minSupportedVersion) {
                const currentVersion = bundlerVersionInDependencies || bundlerVersionInDevDependencies;
                const isBundleSupported = semver.gte(semver.coerce(currentVersion), semver.coerce(minSupportedVersion));
                if (!isBundleSupported) {
                    this.$errors.failWithoutHelp(util.format(constants_1.BundleValidatorMessages.NotSupportedVersion, minSupportedVersion));
                }
            }
        }
    }
}
exports.BundleValidatorHelper = BundleValidatorHelper;
$injector.register("bundleValidatorHelper", BundleValidatorHelper);
