"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 Parser_1 = require("./Parser");
const index_1 = require("../utils/index");
var readfiles = require('node-readfiles');
var CRC32 = require('crc-32');
const path = require("path");
class ParserManger {
    constructor() {
        this.caches = {};
        this.lastUpdateTime = new Date().getTime();
    }
    static getSingleInstance() {
        if (ParserManger.instance) {
            return ParserManger.instance;
        }
        ParserManger.instance = new ParserManger();
        return ParserManger.instance;
    }
    handleTimerQueue() {
        return __awaiter(this, void 0, void 0, function* () {
            const filePath = index_1.default.getCurrentFilePath();
            if (filePath) {
                const parser = this.caches[filePath];
                if (parser) {
                    if (new Date().getTime() - this.lastUpdateTime > 1 * 1000) {
                        const updateQueues = yield parser.updateHook.promise([]);
                        for (var i = 0, len = updateQueues.length; i < len; i++) {
                            updateQueues[i]();
                        }
                    }
                }
            }
        });
    }
    parseCurrentFile() {
        return __awaiter(this, void 0, void 0, function* () {
            const filePath = index_1.default.getCurrentFilePath();
            if (filePath) {
                return this.parseFile(filePath);
            }
            else {
                return Promise.reject();
            }
        });
    }
    parseDir(dirName) {
        return __awaiter(this, void 0, void 0, function* () {
            // 获取所有文件，然后出个排查
            yield index_1.default.diagnostic.clear();
            return readfiles(dirName, {
                filter: [
                    '.ts$',
                    '.tsx$',
                    '.js$',
                    '.jsx$',
                ]
            }, (err, filename) => {
                if (err)
                    throw err;
            }).then((files) => __awaiter(this, void 0, void 0, function* () {
                var index = 0;
                var run = () => {
                    const filename = path.join(dirName, files[index]);
                    this.parseFile(filename).then((parser) => {
                        index++;
                        parser.logErrors();
                        if (index < files.length) {
                            run();
                        }
                    });
                };
                run();
            }));
        });
    }
    parseFile(filepath) {
        if (!this.caches[filepath]) {
            this.caches[filepath] = new Parser_1.default(filepath);
        }
        return this.caches[filepath].parse();
    }
    showHoverMenu(params) {
        const fileName = params.document.fileName;
        var errorHoverMenus;
        const parser = this.caches[fileName];
        if (parser) {
            for (var i = 0; i < this.caches[fileName].errors.length; i++) {
                const tempErrorHoverMenus = this.caches[fileName].errors[i].showMenu(params);
                if (tempErrorHoverMenus) {
                    errorHoverMenus = tempErrorHoverMenus;
                }
            }
        }
        return errorHoverMenus;
    }
}
exports.default = ParserManger;
//# sourceMappingURL=ParserManager.js.map