"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Server = void 0;
const node_1 = require("vscode-languageserver/node");
const service_1 = require("./service");
class Server {
    constructor() {
        this.connection = (0, node_1.createConnection)(node_1.ProposedFeatures.all);
        this.setupEventHandlers();
    }
    setupEventHandlers() {
        this.connection.onInitialize(async (params) => {
            this.service = new service_1.Service(this.connection, params);
            await this.service.init();
            const result = {
                capabilities: {
                    textDocumentSync: node_1.TextDocumentSyncKind.Incremental,
                    documentFormattingProvider: true,
                },
            };
            if (this.service.settings.hasWorkspaceFolderCapability) {
                result.capabilities.workspace = {
                    workspaceFolders: {
                        supported: true,
                    },
                };
            }
            return result;
        });
        this.connection.onInitialized(() => {
            if (this.service.settings.hasConfigurationCapability) {
                // Register for all configuration changes.
                this.connection.client.register(node_1.DidChangeConfigurationNotification.type, undefined);
            }
            if (this.service.settings.hasWorkspaceFolderCapability) {
                this.connection.workspace.onDidChangeWorkspaceFolders((_event) => {
                    this.connection.console.log("Workspace folder change event received.");
                });
            }
            this.connection.client.register(node_1.DidChangeWatchedFilesNotification.type, {
                watchers: [
                    { globPattern: `**/**/*.html.erb` },
                    { globPattern: `**/**/.herb-lsp/config.json` },
                ],
            });
        });
        this.connection.onDidChangeConfiguration(async (change) => {
            if (this.service.settings.hasConfigurationCapability) {
                // Reset all cached document settings
                this.service.settings.documentSettings.clear();
            }
            else {
                this.service.settings.globalSettings = ((change.settings.languageServerHerb || this.service.settings.defaultSettings));
            }
            await this.service.refresh();
        });
        this.connection.onDidOpenTextDocument(async (params) => {
            const document = this.service.documentService.get(params.textDocument.uri);
            if (document) {
                await this.service.diagnostics.refreshDocument(document);
            }
        });
        this.connection.onDidChangeWatchedFiles((params) => {
            params.changes.forEach(async (event) => {
                if (event.uri.endsWith("/.herb-lsp/config.json")) {
                    await this.service.refreshConfig();
                    const documents = this.service.documentService.getAll();
                    await Promise.all(documents.map(document => this.service.diagnostics.refreshDocument(document)));
                }
            });
        });
        this.connection.onDocumentFormatting((params) => {
            return this.service.formatting.formatDocument(params);
        });
    }
    listen() {
        this.connection.listen();
    }
}
exports.Server = Server;
//# sourceMappingURL=server.js.map