import * as fs from 'fs';
import { basename, dirname, join } from 'path';
import { convertCompilerOptionsFromJson, findConfigFile, parseConfigFileTextToJson, } from 'typescript';
import { diagnosticToString } from './diagnosticToString';
export function findTsConfig(cwd = process.cwd()) {
    const configPath = findConfigFile(cwd, (fileName) => fs.existsSync(fileName));
    if (!configPath) {
        throw new Error('Unable to find TypeScript configuration');
    }
    const baseConfig = parseConfigFile(cwd, configPath);
    if (baseConfig.extends) {
        const extensions = Array.isArray(baseConfig.extends) ? baseConfig.extends : [baseConfig.extends];
        const extendedConfigPaths = extensions.map(ext => join(dirname(configPath), ext));
        const extendedConfigs = extendedConfigPaths.map(path => parseConfigFile(cwd, path));
        if (extendedConfigs.length === 1) {
            return mergeConfigs(baseConfig, extendedConfigs[0]);
        }
        return extendedConfigs.reduceRight(mergeConfigs, baseConfig);
    }
    return baseConfig;
}
function mergeConfigs(base, extension) {
    return Object.assign({}, extension, base, { compilerOptions: Object.assign({}, extension.compilerOptions, base.compilerOptions) });
}
function parseConfigFile(cwd, filePath) {
    const fileName = basename(filePath);
    const contents = fs.readFileSync(filePath).toString();
    const { config } = parseConfigFileTextToJson(filePath, contents);
    const { options, errors } = convertCompilerOptionsFromJson(config.compilerOptions, cwd, fileName);
    if (errors && errors.length > 0) {
        throw new Error(errors.map(x => diagnosticToString(x, cwd)).join('\n'));
    }
    return Object.assign({}, config, { compilerOptions: options, configPath: filePath });
}
//# sourceMappingURL=findTsConfig.js.map