/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { createConnection, ProposedFeatures, TextDocuments, DidChangeConfigurationNotification, } from 'vscode-languageserver/node.js';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { getServerCapabilities } from './capabilities.js';
import { loadIndex, watchIndex, unwatchIndex } from './index-loader.js';
import { handleCompletion } from './providers/completion.js';
import { handleDefinition } from './providers/definition.js';
import { handleHover } from './providers/hover.js';
import { computeDiagnostics } from './providers/diagnostics.js';
// ─── Connection Setup ─────────────────────────────────────────────────────────
const connection = createConnection(ProposedFeatures.all);
const documents = new TextDocuments(TextDocument);
let workspaceRoot = '';
// ─── Initialization ───────────────────────────────────────────────────────────
connection.onInitialize((params) => {
    // Resolve workspace root from InitializeParams
    if (params.workspaceFolders && params.workspaceFolders.length > 0) {
        workspaceRoot = uriToPath(params.workspaceFolders[0].uri);
    }
    else if (params.rootUri) {
        workspaceRoot = uriToPath(params.rootUri);
    }
    else if (params.rootPath) {
        workspaceRoot = params.rootPath;
    }
    // Load the metadata index
    if (workspaceRoot) {
        loadIndex(workspaceRoot);
        watchIndex(workspaceRoot, () => {
            // Re-validate all open documents when the index changes
            for (const doc of documents.all()) {
                validateDocument(doc);
            }
        });
    }
    return {
        capabilities: getServerCapabilities(),
    };
});
connection.onInitialized(() => {
    void connection.client.register(DidChangeConfigurationNotification.type, undefined);
});
// ─── Shutdown ─────────────────────────────────────────────────────────────────
connection.onShutdown(() => {
    unwatchIndex();
});
// ─── Completion ───────────────────────────────────────────────────────────────
connection.onCompletion((params) => handleCompletion(params, (uri) => documents.get(uri)));
// ─── Definition ───────────────────────────────────────────────────────────────
connection.onDefinition((params) => handleDefinition(params, (uri) => documents.get(uri), workspaceRoot));
// ─── Hover ────────────────────────────────────────────────────────────────────
connection.onHover((params) => handleHover(params, (uri) => documents.get(uri)));
// ─── Diagnostics ──────────────────────────────────────────────────────────────
documents.onDidOpen((event) => {
    validateDocument(event.document);
});
documents.onDidSave((event) => {
    validateDocument(event.document);
});
documents.onDidClose((event) => {
    void connection.sendDiagnostics({ uri: event.document.uri, diagnostics: [] });
});
function validateDocument(document) {
    const diagnostics = computeDiagnostics(document);
    void connection.sendDiagnostics({ uri: document.uri, diagnostics });
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
function uriToPath(uri) {
    if (uri.startsWith('file://')) {
        return decodeURIComponent(uri.slice(7));
    }
    return uri;
}
// ─── Start ────────────────────────────────────────────────────────────────────
documents.listen(connection);
connection.listen();
//# sourceMappingURL=server.js.map