"use strict";
/*
 * Copyright 2018, collaboration Factory AG. All rights reserved.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const fs = require("fs");
const CplacePlugin_1 = require("./CplacePlugin");
const utils_1 = require("../utils");
const AbstractTSConfigGenerator_1 = require("./AbstractTSConfigGenerator");
const CplaceTypescriptCompiler_1 = require("../compiler/CplaceTypescriptCompiler");
class CplaceTSConfigGenerator extends AbstractTSConfigGenerator_1.AbstractTSConfigGenerator {
    constructor(plugin, dependencies, localOnly, isProduction) {
        super(plugin, dependencies, localOnly, isProduction, 'ts');
    }
    createConfigAndGetPath() {
        const { paths, refs } = this.getPathsAndRefs();
        const extraTypes = CplaceTSConfigGenerator.getExtraTypes(this.plugin.pluginDir, this.dependencies.map(d => d.pluginDir));
        const additionalIncludes = extraTypes === null ? [] : extraTypes.definitions;
        this.tsConfig = {
            extends: path.join(this.pathToMain, 'tsconfig.base.json'),
            compilerOptions: {
                rootDir: '.',
                baseUrl: '.',
                outDir: `../${CplaceTypescriptCompiler_1.CplaceTypescriptCompiler.DEST_DIR}`,
                sourceMap: !this.isProduction,
                declarationMap: !this.isProduction
            },
            include: [
                './**/*.ts',
                ...additionalIncludes
            ]
        };
        if (this.plugin.pluginName !== this.platformPlugin) {
            paths['*'].push(`${this.pathToMain}/node_modules/@types/*`, `${this.pathToMain}/cf.cplace.platform/assets/@cplaceTypes/*`);
            this.tsConfig.compilerOptions.paths = paths;
            this.tsConfig.references = refs;
        }
        this.saveConfig();
        return this.getTSConfigPath();
    }
    static getExtraTypes(pluginDir, dependencyPaths) {
        const pluginName = path.basename(pluginDir);
        const assetsDir = CplacePlugin_1.default.getAssetsDir(pluginDir);
        const typesPath = path.resolve(assetsDir, 'ts', 'extra-types.json');
        let result;
        if (fs.existsSync(typesPath)) {
            const content = fs.readFileSync(typesPath, { encoding: 'utf8' });
            try {
                result = JSON.parse(content);
            }
            catch (e) {
                console.error(utils_1.cerr `[${pluginName}] Cannot read extra types: ${typesPath}`);
                return null;
            }
        }
        else {
            result = {
                definitions: [],
                externals: {}
            };
        }
        if (!result.definitions) {
            result.definitions = [];
        }
        else {
            result.definitions = result.definitions.map(p => path.resolve(assetsDir, 'ts', p));
        }
        if (!result.externals) {
            result.externals = {};
        }
        dependencyPaths
            .map(dep => {
            return this.getExtraTypes(dep, []);
        })
            .forEach(r => {
            if (r !== null) {
                result.definitions = [...result.definitions, ...r.definitions];
                result.externals = Object.assign({}, result.externals, r.externals);
            }
        });
        return result;
    }
}
exports.CplaceTSConfigGenerator = CplaceTSConfigGenerator;
//# sourceMappingURL=CplaceTSConfigGenerator.js.map