"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util = require("util");
const constants_1 = require("../constants");
const version_validator_helper_1 = require("./version-validator-helper");
class BundleValidatorHelper extends version_validator_helper_1.VersionValidatorHelper {
    constructor($errors, $options) {
        super();
        this.$errors = $errors;
        this.$options = $options;
        this.bundlersMap = {
            webpack: "nativescript-dev-webpack"
        };
    }
    validate(projectData, minSupportedVersion) {
        if (this.$options.bundle) {
            const currentVersion = this.getBundlerDependencyVersion(projectData);
            if (!currentVersion) {
                this.$errors.failWithoutHelp(constants_1.BundleValidatorMessages.MissingBundlePlugin);
            }
            const shouldThrowError = minSupportedVersion && this.isValidVersion(currentVersion) && this.isVersionLowerThan(currentVersion, minSupportedVersion);
            if (shouldThrowError) {
                this.$errors.failWithoutHelp(util.format(constants_1.BundleValidatorMessages.NotSupportedVersion, minSupportedVersion));
            }
        }
    }
    getBundlerDependencyVersion(projectData, bundlerName) {
        let dependencyVersion = null;
        const bundlePluginName = bundlerName || this.bundlersMap[this.$options.bundle];
        const bundlerVersionInDependencies = projectData.dependencies && projectData.dependencies[bundlePluginName];
        const bundlerVersionInDevDependencies = projectData.devDependencies && projectData.devDependencies[bundlePluginName];
        dependencyVersion = bundlerVersionInDependencies || bundlerVersionInDevDependencies;
        return dependencyVersion;
    }
}
exports.BundleValidatorHelper = BundleValidatorHelper;
$injector.register("bundleValidatorHelper", BundleValidatorHelper);
