"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const fs = require('fs');
const parseString = require('xml2js').parseString;
class ImlParser {
    constructor(pathToIml) {
        this.pathToIml = pathToIml;
        if (!fs.existsSync(this.pathToIml)) {
            throw Error(`IML ${this.pathToIml} does not exist`);
        }
        this.parseFile();
    }
    static doesImlExist(directory, pluginName) {
        return fs.existsSync(path.join(directory, `${pluginName}.iml`));
    }
    getReferencedModules() {
        const components = this._module.component;
        let result = [];
        if (components) {
            components.forEach(component => {
                if (component.$.name === 'NewModuleRootManager') {
                    result = ImlParser.getReferencedModulesFromManager(component);
                }
            });
        }
        return result;
    }
    parseFile() {
        if (this._module) {
            return;
        }
        const imlContent = fs.readFileSync(this.pathToIml, 'utf8');
        parseString(imlContent, (err, result) => {
            this._module = result.module;
        });
    }
    static getReferencedModulesFromManager(component) {
        const entries = component.orderEntry;
        if (!entries) {
            return [];
        }
        return entries
            .filter(entry => entry.$.type && entry.$.type === 'module')
            .map(entry => {
            return {
                moduleName: entry.$['module-name'],
                isTestScoped: entry.$['scope'] === 'TEST'
            };
        })
            .filter(dep => {
            return !!dep.moduleName;
        });
    }
}
exports.ImlParser = ImlParser;
//# sourceMappingURL=ImlParser.js.map