/*
 * 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 { MarkupKind } from 'vscode-languageserver';
import { getWordAtPosition, getQualifiedNameAtPosition } from '../document-context.js';
import { getEntry, isIndexLoaded } from '../index-loader.js';
// ─── Hover Handler ────────────────────────────────────────────────────────────
export function handleHover(params, getDocument) {
    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, then simple word
    const qualifiedName = getQualifiedNameAtPosition(lineText, position.character);
    const simpleWord = getWordAtPosition(lineText, position.character);
    const entry = resolveEntry(qualifiedName, simpleWord);
    if (!entry) {
        return null;
    }
    return {
        contents: {
            kind: MarkupKind.Markdown,
            value: formatHoverContent(entry),
        },
    };
}
// ─── Resolution ───────────────────────────────────────────────────────────────
function resolveEntry(qualifiedName, simpleWord) {
    if (qualifiedName && qualifiedName !== simpleWord) {
        const entry = getEntry(qualifiedName);
        if (entry)
            return entry;
    }
    if (simpleWord) {
        const entry = getEntry(simpleWord);
        if (entry)
            return entry;
    }
    return undefined;
}
// ─── Formatting ───────────────────────────────────────────────────────────────
function formatListSection(options) {
    const { items, heading, maxDisplay } = options;
    if (items.length === 0)
        return [];
    const lines = ['', heading];
    const displayed = items.slice(0, maxDisplay);
    for (const item of displayed) {
        lines.push(`- \`${item}\``);
    }
    if (items.length > maxDisplay) {
        lines.push(`- ... and ${items.length - maxDisplay} more`);
    }
    return lines;
}
function formatFieldsSection(entry) {
    if (!entry.fields || entry.fields.length === 0)
        return [];
    return formatListSection({ items: entry.fields, heading: `**Fields:** ${entry.fields.length}`, maxDisplay: 10 });
}
function formatMethodsSection(entry) {
    if (!entry.methods || entry.methods.length === 0)
        return [];
    return formatListSection({ items: entry.methods, heading: `**Methods:** ${entry.methods.length}`, maxDisplay: 10 });
}
function formatDependenciesSection(entry) {
    if (!entry.dependencies || entry.dependencies.length === 0)
        return [];
    return formatListSection({
        items: entry.dependencies,
        heading: `**Dependencies** (${entry.dependencies.length}):`,
        maxDisplay: 8,
    });
}
function formatDependentsSection(entry) {
    if (!entry.dependents || entry.dependents.length === 0)
        return [];
    return formatListSection({
        items: entry.dependents,
        heading: `**Dependents** (${entry.dependents.length}):`,
        maxDisplay: 8,
    });
}
function formatHoverContent(entry) {
    const lines = [];
    // Header
    lines.push(`**${entry.apiName}**`);
    lines.push('');
    // Type and category
    const typeLine = entry.category ? `${entry.type} (${entry.category})` : entry.type;
    lines.push(`**Type:** ${typeLine}`);
    // Path
    if (entry.path) {
        lines.push(`**Path:** \`${entry.path}\``);
    }
    // Description
    if (entry.description) {
        lines.push('');
        lines.push(entry.description);
    }
    lines.push(...formatFieldsSection(entry));
    lines.push(...formatMethodsSection(entry));
    lines.push(...formatDependenciesSection(entry));
    lines.push(...formatDependentsSection(entry));
    return lines.join('\n');
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
function getLineText(document, line) {
    const start = { line, character: 0 };
    const end = { line, character: Number.MAX_SAFE_INTEGER };
    return document.getText({ start, end });
}
//# sourceMappingURL=hover.js.map