"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const fileUtils_1 = require("./fileUtils");
const Constants_1 = require("./Constants");
class SfdxProject {
    constructor(config, rootDir) {
        this._configuration = config;
        this._rootDirectory = rootDir;
        this._sfdxProjectPath = path_1.default.join(rootDir, Constants_1.SFDX_PROJECT_JSON);
        this._isSfdxProject = SfdxProject.isSfdxProjectJsonPresent(rootDir);
    }
    static isSfdxProjectJsonPresent(rootDirectory) {
        return fs_extra_1.default.existsSync(path_1.default.join(rootDirectory, Constants_1.SFDX_PROJECT_JSON));
    }
    get configuration() {
        return this._configuration;
    }
    initWithSfdxConfiguration() {
        if (this._isSfdxProject) {
            const packageDirectories = this.getPackageDirectories();
            if (packageDirectories.length > 0) {
                const defaultPackageDirectory = path_1.default.join(this._rootDirectory, packageDirectories[0]);
                this.setModulesSourceDirectory(defaultPackageDirectory);
                this.setStaticResourcesDirectories(packageDirectories);
                this.setCustomLabelsFile(defaultPackageDirectory);
                this.setContentAssetsPath(defaultPackageDirectory);
            }
        }
    }
    getPackageDirectories() {
        const packageDirectories = [];
        const jsonContents = fileUtils_1.getFileContents(this._sfdxProjectPath);
        if (jsonContents && !!jsonContents.trim()) {
            try {
                this.getPackagesFromMap(JSON.parse(jsonContents), packageDirectories);
            }
            catch (e) {
                console.error(`Loading configuration from ${this._sfdxProjectPath} failed with the error ${e.message}`);
            }
        }
        return packageDirectories;
    }
    getPackagesFromMap(configMap, packageDirectories) {
        if (configMap.packageDirectories instanceof Array) {
            let defaultPackage = '';
            configMap.packageDirectories.forEach((element) => {
                if (element.default) {
                    defaultPackage = element.path;
                }
                else {
                    packageDirectories.push(element.path);
                }
            });
            if (defaultPackage) {
                packageDirectories.unshift(defaultPackage);
            }
        }
    }
    setModulesSourceDirectory(defaultPackageDirectory) {
        if (!this.configuration.modulesSourceDirectory) {
            this.configuration.modulesSourceDirectory = defaultPackageDirectory;
        }
    }
    setStaticResourcesDirectories(packageDirectories) {
        if (this.configuration.staticResourcesDirectories &&
            this.configuration.staticResourcesDirectories.length === 0) {
            this.configuration.staticResourcesDirectories = fileUtils_1.findAllFolderPaths(this._rootDirectory, packageDirectories, Constants_1.STATIC_RESOURCES, SfdxProject.FOLDERS_TO_IGNORE);
        }
    }
    setCustomLabelsFile(defaultPackageDirectory) {
        if (!this.configuration.customLabelsFile) {
            this.configuration.customLabelsFile = fileUtils_1.findFileWithDefaultPath(defaultPackageDirectory, Constants_1.DEFAULT_SFDX_PATH, SfdxProject.CUSTOM_LABELS_FOLDER, SfdxProject.CUSTOM_LABELS_FILE, SfdxProject.FOLDERS_TO_IGNORE);
        }
    }
    setContentAssetsPath(defaultPackageDirectory) {
        if (!this.configuration.contentAssetsDirectory) {
            this.configuration.contentAssetsDirectory = fileUtils_1.findFolderWithDefaultPath(defaultPackageDirectory, Constants_1.DEFAULT_SFDX_PATH, Constants_1.CONTENT_ASSETS, SfdxProject.FOLDERS_TO_IGNORE);
        }
    }
}
exports.default = SfdxProject;
SfdxProject.FOLDERS_TO_IGNORE = new Set([
    'aura',
    'lwc',
    'classes',
    'triggers',
    'layouts',
    'objects'
]);
SfdxProject.CUSTOM_LABELS_FOLDER = 'labels';
SfdxProject.CUSTOM_LABELS_FILE = 'CustomLabels.labels-meta.xml';
//# sourceMappingURL=SfdxProject.js.map