"use strict";
/*
 * Copyright 2018, collaboration Factory AG. All rights reserved.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractTSConfigGenerator = void 0;
const path = require("path");
const fs = require("fs");
const CplacePlugin_1 = require("./CplacePlugin");
const utils_1 = require("../utils");
class AbstractTSConfigGenerator {
    constructor(plugin, dependencies, localOnly, isProduction, srcFolderName) {
        this.plugin = plugin;
        this.dependencies = dependencies;
        this.localOnly = localOnly;
        this.isProduction = isProduction;
        this.srcFolderName = srcFolderName;
        this.platformPlugin = 'cf.cplace.platform';
        this.relRepoRootPrefix = '../../..';
        this.tsConfigJson = 'tsconfig.json';
        this.mainFolderName = '';
        this.pathToMain = this.getRelativePathToMain(this.localOnly, this.plugin.repo, this.relRepoRootPrefix);
        this.relPathToPlatform = path.join(this.relRepoRootPrefix, CplacePlugin_1.default.getPluginPathRelativeToRepo(this.plugin.repo, this.platformPlugin, this.mainFolderName, localOnly));
        this.relPathToPlatformTs = path.join(this.relPathToPlatform, 'assets', this.srcFolderName);
    }
    getRelativePathToMain(localOnly, repo, relRepoRootPrefix) {
        let workingDir = process.cwd();
        workingDir = path.resolve(workingDir);
        if (path.basename(workingDir) === 'main' ||
            path.basename(workingDir) === 'cplace') {
            this.mainFolderName = path.basename(workingDir);
        }
        const expectedMain = path.resolve(workingDir, '..', 'main');
        const expectedCplace = path.resolve(workingDir, '..', 'cplace');
        if (fs.existsSync(expectedMain)) {
            this.mainFolderName = path.basename(expectedMain);
        }
        else if (fs.existsSync(expectedCplace)) {
            this.mainFolderName = path.basename(expectedCplace);
        }
        utils_1.debug(`main/cplace Repository folder was found in ${this.mainFolderName}`);
        return path.join(relRepoRootPrefix, !localOnly && repo !== this.mainFolderName
            ? path.join('..', this.mainFolderName)
            : '');
    }
    getTSConfigPath() {
        return path.join(this.plugin.assetsDir, this.srcFolderName, this.tsConfigJson);
    }
    getPathsAndRefs() {
        let defaultPaths = Object.assign(Object.assign({}, AbstractTSConfigGenerator.getPathDependency(this.platformPlugin, this.relPathToPlatformTs)), { '*': ['*'] });
        const defaultPathsAndRefs = {
            paths: defaultPaths,
            refs: [
                {
                    path: this.relPathToPlatformTs,
                },
            ],
        };
        return this.dependencies.reduce((acc, dependency) => {
            // we do not add platform paths and references here as some modules might not have direct dependency on platform
            if (dependency.pluginName === this.platformPlugin) {
                return acc;
            }
            const relPathToDependency = path.join(this.relRepoRootPrefix, dependency.getPluginPathRelativeFromRepo(this.plugin.repo, this.localOnly));
            const relPathToDependencyTs = path.join(relPathToDependency, 'assets', this.srcFolderName);
            const newPath = AbstractTSConfigGenerator.getPathDependency(dependency.pluginName, relPathToDependencyTs);
            const newRef = { path: relPathToDependencyTs };
            return {
                paths: Object.assign(Object.assign({}, acc.paths), newPath),
                refs: [...acc.refs, newRef],
            };
        }, defaultPathsAndRefs);
    }
    saveConfig() {
        const content = JSON.stringify(this.tsConfig, null, 4);
        fs.writeFileSync(this.getTSConfigPath(), content, { encoding: 'utf8' });
        utils_1.debug(`(TsConfigGenerator) [${this.plugin.pluginName}] TS Config content:`);
        utils_1.debug(content);
    }
    static getPathDependency(dependency, path) {
        const dependencyObject = {};
        dependencyObject[`@${dependency}/*`] = [path + '/*'];
        return dependencyObject;
    }
}
exports.AbstractTSConfigGenerator = AbstractTSConfigGenerator;
//# sourceMappingURL=AbstractTSConfigGenerator.js.map