"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");
const utils_1 = require("./utils");
function getComponentProps(ts, tsLanguageService, program, 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 completion = tsLanguageService.getCompletionsAtPosition(virtualCode.fileName, position2, {
        includeSymbol: true,
    });
    // skip fallback global completions
    if (!completion?.isMemberCompletion) {
        return [];
    }
    const props = [];
    const checker = program.getTypeChecker();
    for (const entry of completion.entries) {
        // skip unchecked JS identifiers
        if (entry.isFromUncheckedFile) {
            continue;
        }
        const modifiers = entry.kindModifiers?.split(',');
        const type = entry.symbol && checker.getTypeOfSymbol(entry.symbol);
        const info = {
            name: entry.name.startsWith('"') && entry.name.endsWith('"')
                ? entry.name.slice(1, -1)
                : entry.name,
            description: entry.symbol && (ts.displayPartsToString(entry.symbol.getDocumentationComment(checker))
                + '\n\n'
                + entry.symbol.getJsDocTags(checker).map(tag => `*@${tag.name}*${tag.text?.length ? ` — ${ts.displayPartsToString(tag.text)}` : ''}`).join('\n\n')).trimEnd(),
        };
        if (modifiers?.includes('optional')) {
            info.optional = true;
        }
        if (!utils_1.booleanExceptionProps.has(entry.name) && type && (0, utils_1.hasBooleanType)(ts, type)) {
            info.boolean = true;
        }
        props.push(info);
    }
    return props;
}
//# sourceMappingURL=getComponentProps.js.map