"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getComponentProps = getComponentProps;
const transform_1 = require("@volar/typescript/lib/node/transform");
const language_core_1 = require("@vue/language-core");
function getComponentProps(ts, tsLanguageService, session, language, sourceScript, virtualCode, position) {
    const serviceScript = sourceScript.generated.languagePlugin.typescript?.getServiceScript(virtualCode);
    if (!serviceScript) {
        return [];
    }
    const { ir } = virtualCode;
    if (!ir.template?.ast) {
        return [];
    }
    let node;
    for (const child of (0, language_core_1.forEachElementNode)(ir.template.ast)) {
        if (position >= child.loc.start.offset) {
            node = child;
        }
    }
    if (node?.tagType !== 1) {
        return [];
    }
    let position2;
    if (node.props.some(prop => position >= prop.loc.start.offset && position <= prop.loc.end.offset)) {
        position2 = (0, transform_1.toGeneratedOffset)(language, serviceScript, sourceScript, ir.template.startTagEnd + position - (
        // <Comp :foo-| /> -> { "foo"|: ... }
        ir.template.content[position - 1] === '-' ? 1 : 0), (data) => !!data.__propsCompletion);
    }
    position2 ??= (0, transform_1.toGeneratedOffset)(language, serviceScript, sourceScript, ir.template.startTagEnd + node.loc.start.offset, (data) => !!data.__propsCompletion);
    if (!position2) {
        return [];
    }
    const preferences = session['getPreferences']();
    const completion = tsLanguageService.getCompletionsAtPosition(virtualCode.fileName, position2, preferences);
    // skip fallback global completions
    if (!completion?.isMemberCompletion) {
        return [];
    }
    return completion?.entries.map(entry => {
        const modifiers = entry.kindModifiers?.split(',');
        const details = tsLanguageService.getCompletionEntryDetails(virtualCode.fileName, position2, entry.name, session['getFormatOptions'](), entry.source, preferences, entry.data);
        const info = {
            name: entry.name.startsWith('"') && entry.name.endsWith('"')
                ? entry.name.slice(1, -1)
                : entry.name,
            description: ts.displayPartsToString(details?.documentation) + (details?.tags
                ? '\n\n' + details.tags.map(tag => `*@${tag.name}* — ${ts.displayPartsToString(tag.text)}`).join('\n')
                : ''),
        };
        if (modifiers?.includes('optional')) {
            info.optional = true;
        }
        if (details?.displayParts.some((part, i) => i >= 7 && part.text === 'boolean')) {
            info.boolean = true;
        }
        return info;
    });
}
//# sourceMappingURL=getComponentProps.js.map