"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
const svparser_1 = require("./svparser");
const genutils_1 = require("./genutils");
let _parser = new svparser_1.SystemVerilogParser();
let _preprocCache = new Map();
let _includeFilePaths = [];
let _userDefinesMacroInfo = new Map();
process.on('message', (args) => {
    try {
        if (args[0] == 'exit') {
            process.exit();
        }
        else if (args[0] == 'config') {
            _includeFilePaths = args[1];
            if (args[2] != undefined) {
                _userDefinesMacroInfo = new Map(args[2].map(d => [d[0], { args: undefined, default: undefined, definition: d[1], symbol: undefined, file: "" }]));
            }
        }
        else if (args[0] == 'done') {
            let preprocCache = svparser_1.SystemVerilogParser.preprocCacheToJSON(_preprocCache);
            process.send([preprocCache, []]);
        }
        else {
            let file = args[1];
            if (_preprocCache.has(file)) {
                process.send([[], []]);
            }
            else {
                genutils_1.fsReadFile(file)
                    .then((data) => {
                    let document = vscode_languageserver_textdocument_1.TextDocument.create(genutils_1.pathToUri(file), "SystemVerilog", 0, data.toString());
                    let fileSymbolsInfo;
                    let pkgdeps;
                    [fileSymbolsInfo, pkgdeps] = _parser.parse(document, _includeFilePaths, _preprocCache, _userDefinesMacroInfo, "full");
                    //DBG let symbols: SystemVerilogSymbol[] = SystemVerilogParser.fileAllSymbols(fileSymbolsInfo, false);
                    //DBG ConnectionLogger.log(`DEBUG: Sending ${symbols.length} symbols and ${pkgdeps.length} pkgdeps for ${file}`);
                    process.send([svparser_1.SystemVerilogParser.fileSymbolsInfoToJson(fileSymbolsInfo), pkgdeps]);
                })
                    .catch((err) => {
                    process.send([[], []]);
                });
            }
        }
    }
    catch (error) {
        genutils_1.ConnectionLogger.error(error);
        process.send([[], []]);
    }
});
