"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const vscode = require("vscode");
const path = require("path");
const fs = require("fs");
const config_1 = require("../config");
const no_cache_require_1 = require("../utils/no-cache-require");
class default_1 {
    constructor(parser, filepath) {
        this.langs = [];
        this.fileExt = 'json';
        this.fileReg = /\.(tsx|ts|js|vue)$/;
        this.isCurrentFileLogError = false;
        this.localeDir = null;
        // 上面的属性是需要传进来的配置
        this.projectDir = '';
        // @ts-ignore
        this.langMap = {};
        this.publisher = config_1.default.publisher;
        this.name = config_1.default.name;
        this.parser = parser;
        this.filepath = filepath;
    }
    init() {
        // 找文件
        const configFile = this.findConfigFile();
        if (configFile) {
            return this.handleConfigFile(configFile);
        }
        else {
            return Promise.reject();
        }
    }
    getFirstLangKey() {
        return this.langs[0].key;
    }
    findConfigFile() {
        const vsConfigFile = vscode.workspace.getConfiguration(this.name);
        var localConfigFileName = vsConfigFile.get('localConfigFileName');
        const findConfigFile = (fileDir) => {
            if (!path.isAbsolute(fileDir)) {
                return null;
            }
            if (fileDir == '/') {
                return null;
            }
            else if (!fs.existsSync(`${fileDir}/${localConfigFileName}`)) {
                return findConfigFile(path.dirname(fileDir));
            }
            else {
                this.projectDir = fileDir;
                return `${fileDir}/${localConfigFileName}`;
            }
        };
        const configFile = findConfigFile(path.dirname(this.filepath));
        return configFile;
    }
    getTempDir(configFile) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!configFile) {
                configFile = this.findConfigFile();
            }
            if (configFile) {
                return path.join(path.dirname(configFile), '.locale');
            }
            else {
                return Promise.reject();
            }
        });
    }
    handleConfigFile(configFile) {
        return __awaiter(this, void 0, void 0, function* () {
            const initConfig = no_cache_require_1.default(configFile);
            const config = initConfig(this);
            this.langs = config.langs.map((item) => {
                return Object.assign({
                    color: 'red',
                    check: true
                }, item);
            });
            this.langs.forEach(langItem => {
                this.langMap[langItem.key] = langItem;
            });
            if (config.fileExt) {
                this.fileExt = config.fileExt;
            }
            const tempLocal = yield this.getTempDir(configFile);
            if (!fs.existsSync(tempLocal)) {
                fs.mkdirSync(tempLocal);
            }
            if (!config.localeDir) {
                const langDir = path.join(tempLocal, 'langs');
                if (!fs.existsSync(langDir)) {
                    fs.mkdirSync(langDir);
                }
                this.localeDir = langDir;
            }
            else {
                this.localeDir = config.localeDir;
            }
            if (config.plugins) {
                this.parser.plugins = this.parser.plugins.concat(config.plugins);
            }
        });
    }
}
exports.default = default_1;
//# sourceMappingURL=Config.js.map