"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 LocalDevServerConfiguration_1 = __importDefault(require("../user/LocalDevServerConfiguration"));
class Project {
    constructor(directory) {
        this._isSFDX = false;
        const rootDirectory = this.resolveProjectDirectory(directory);
        if (rootDirectory === null) {
            throw new Error(`Directory specified '${directory}' does not resolve to a project. The specified directory must have package.json or sfdx-project.json in it.`);
        }
        this.rootDirectory = rootDirectory;
        const configurationPath = path_1.default.join(this.rootDirectory, 'localdevserver.config.json');
        this._configuration = new LocalDevServerConfiguration_1.default(configurationPath);
        this._isSFDX = fs_extra_1.default.existsSync(path_1.default.join(this.rootDirectory, 'sfdx-project.json'));
        if (this._isSFDX) {
            this.initWithSfdxConfiguration();
        }
    }
    get configuration() {
        return this._configuration;
    }
    get isSfdx() {
        return this._isSFDX;
    }
    get modulesSourceDirectory() {
        if (path_1.default.isAbsolute(this.configuration.modulesSourceDirectory)) {
            return this.configuration.modulesSourceDirectory;
        }
        return path_1.default.join(this.rootDirectory, this.configuration.modulesSourceDirectory || 'src');
    }
    get staticResourcesDirectory() {
        if (path_1.default.isAbsolute(this.configuration.staticResourcesDirectory)) {
            return this.configuration.staticResourcesDirectory;
        }
        if (this.configuration.staticResourcesDirectory !== '') {
            return path_1.default.join(this.rootDirectory, this.configuration.staticResourcesDirectory);
        }
        return null;
    }
    get customLabelsPath() {
        if (path_1.default.isAbsolute(this.configuration.customLabelsFile)) {
            return this.configuration.customLabelsFile;
        }
        if (this.configuration.customLabelsFile !== '') {
            return path_1.default.join(this.rootDirectory, this.configuration.customLabelsFile);
        }
    }
    get directory() {
        return this.rootDirectory;
    }
    resolveProjectDirectory(directory, previousDirectory) {
        let currentDirectory = directory;
        if (currentDirectory === previousDirectory) {
            return null;
        }
        if (currentDirectory === '.' || currentDirectory === '') {
            currentDirectory = process.cwd();
        }
        if (!fs_extra_1.default.existsSync(currentDirectory)) {
            return null;
        }
        if (!fs_extra_1.default.existsSync(path_1.default.join(currentDirectory, 'package.json')) &&
            !fs_extra_1.default.existsSync(path_1.default.join(currentDirectory, 'sfdx-project.json'))) {
            return this.resolveProjectDirectory(path_1.default.join(currentDirectory, '..'), currentDirectory);
        }
        return currentDirectory;
    }
    initWithSfdxConfiguration() {
        const _path = this.directory;
        let jsonFileContents = null;
        const sfdxProjectPath = path_1.default.join(_path, 'sfdx-project.json');
        const packageDirectories = [];
        if (fs_extra_1.default.existsSync(sfdxProjectPath)) {
            try {
                jsonFileContents = fs_extra_1.default.readFileSync(sfdxProjectPath, 'utf-8');
            }
            catch (e) {
                console.error(`Loading ${sfdxProjectPath} failed JSON parsing with error ${e.message}`);
            }
            try {
                if (jsonFileContents !== null &&
                    jsonFileContents.trim() !== '') {
                    const configMap = JSON.parse(jsonFileContents);
                    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);
                        }
                    }
                }
            }
            catch (e) {
                console.error(`Loading configuration from ${sfdxProjectPath} failed with the error ${e.message}`);
            }
        }
        if (packageDirectories.length > 0) {
            if (!this.configuration.modulesSourceDirectory) {
                this.configuration.modulesSourceDirectory =
                    packageDirectories[0];
            }
            if (!this.configuration.staticResourcesDirectory) {
                const resourcePath = path_1.default.join(packageDirectories[0], 'main/default/staticresources');
                this.configuration.staticResourcesDirectory = resourcePath;
            }
            if (!this.configuration.customLabelsFile) {
                const labelsPath = path_1.default.join(packageDirectories[0], 'main/default/labels/CustomLabels.labels-meta.xml');
                if (fs_extra_1.default.existsSync(path_1.default.join(this.rootDirectory, labelsPath))) {
                    this.configuration.customLabelsFile = labelsPath;
                }
            }
        }
    }
}
exports.default = Project;
//# sourceMappingURL=Project.js.map