/*
 * 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 { getWordAtPosition, getQualifiedNameAtPosition } from '../document-context.js';
import { getEntry, isIndexLoaded } from '../index-loader.js';
// ─── Definition Handler ───────────────────────────────────────────────────────
export function handleDefinition(params, getDocument, workspaceRoot) {
    if (!isIndexLoaded()) {
        return null;
    }
    const document = getDocument(params.textDocument.uri);
    if (!document) {
        return null;
    }
    const position = params.position;
    const lineText = getLineText(document, position.line);
    // Try qualified name first (e.g., Account__c.Field__c), then simple word
    const qualifiedName = getQualifiedNameAtPosition(lineText, position.character);
    const simpleWord = getWordAtPosition(lineText, position.character);
    // Try to resolve in this order:
    // 1. Full qualified name
    // 2. Simple word as-is
    // 3. Simple word with common suffixes (__c, __e, __mdt)
    const entry = resolveEntry(qualifiedName, simpleWord);
    if (!entry?.path) {
        return null;
    }
    return createLocation(entry, workspaceRoot);
}
// ─── Resolution Logic ─────────────────────────────────────────────────────────
function resolveEntry(qualifiedName, simpleWord) {
    if (qualifiedName && qualifiedName !== simpleWord) {
        const entry = getEntry(qualifiedName);
        if (entry)
            return entry;
        // Try progressive left-trimming (e.g. "record.Account__c.Name__c" → "Account__c.Name__c")
        const segments = qualifiedName.split('.');
        for (let i = 1; i < segments.length; i++) {
            const partial = segments.slice(i).join('.');
            const partialEntry = getEntry(partial);
            if (partialEntry)
                return partialEntry;
        }
    }
    if (simpleWord) {
        const entry = getEntry(simpleWord);
        if (entry)
            return entry;
    }
    return undefined;
}
function createLocation(entry, workspaceRoot) {
    // Resolve the path to an absolute URI
    const absolutePath = entry.path.startsWith('/') ? entry.path : `${workspaceRoot}/${entry.path}`;
    const fileUri = `file://${absolutePath}`;
    // Point to the beginning of the file (line 0, character 0)
    const range = {
        start: { line: 0, character: 0 },
        end: { line: 0, character: 0 },
    };
    return { uri: fileUri, range };
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
function getLineText(document, line) {
    const start = { line, character: 0 };
    const end = { line, character: Number.MAX_SAFE_INTEGER };
    return document.getText({ start, end });
}
//# sourceMappingURL=definition.js.map