"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const PackageVersion_1 = require("./PackageVersion");
const path = require("path");
const ImlParser_1 = require("./ImlParser");
const PluginDescriptorParser_1 = require("./PluginDescriptorParser");
const utils_1 = require("../utils");
function getDependencyParser() {
    if (PackageVersion_1.PackageVersion.get().major < 3) {
        return new ImlDependencyParser();
    }
    else {
        return new PluginDescriptorDependencyParser();
    }
}
exports.getDependencyParser = getDependencyParser;
class ImlDependencyParser {
    getPluginDependencies(pluginDir, pluginName, excludeTestDependencies) {
        const pathToIml = path.join(pluginDir, `${pluginName}.iml`);
        const imlParser = new ImlParser_1.ImlParser(pathToIml);
        return imlParser.getReferencedModules()
            .filter(module => {
            const includeDependency = !excludeTestDependencies || !module.isTestScoped;
            if (!includeDependency) {
                utils_1.debug(`(DependencyParser) [${pluginName}] excluding test dependency: ${module.moduleName}`);
            }
            return includeDependency;
        })
            .map(module => module.moduleName);
    }
}
class PluginDescriptorDependencyParser {
    getPluginDependencies(pluginDir, pluginName, excludeTestDependencies) {
        const descriptorParser = new PluginDescriptorParser_1.PluginDescriptorParser(pluginDir);
        const pluginDescriptor = descriptorParser.getPluginDescriptor();
        if (pluginName !== pluginDescriptor.name) {
            throw new Error(`[DependencyParser] Expected plugin name ${pluginName} does not match descriptor plugin name: ${pluginDescriptor.name}`);
        }
        const dependencies = pluginDescriptor.dependencies || [];
        if (excludeTestDependencies) {
            return [...dependencies];
        }
        else {
            const testDependencies = pluginDescriptor.testDependencies || [];
            return [...dependencies, ...testDependencies];
        }
    }
}
//# sourceMappingURL=DependencyParser.js.map