"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getComponentType = getComponentType;
exports.getSelfComponentName = getSelfComponentName;
exports.getVariableType = getVariableType;
const shared_1 = require("@vue/shared");
const path = require("node:path");
function getComponentType(ts, languageService, vueCode, components, fileName, tag) {
    const program = languageService.getProgram();
    const checker = program.getTypeChecker();
    const name = tag.split('.');
    let componentSymbol = components.type.getProperty(name[0])
        ?? components.type.getProperty((0, shared_1.camelize)(name[0]))
        ?? components.type.getProperty((0, shared_1.capitalize)((0, shared_1.camelize)(name[0])));
    let componentType;
    if (!componentSymbol) {
        const name = getSelfComponentName(fileName);
        if (name === (0, shared_1.capitalize)((0, shared_1.camelize)(tag))) {
            componentType = getVariableType(ts, languageService, vueCode, '__VLS_self')?.type;
        }
    }
    else {
        componentType = checker.getTypeOfSymbolAtLocation(componentSymbol, components.node);
        for (let i = 1; i < name.length; i++) {
            componentSymbol = componentType.getProperty(name[i]);
            if (componentSymbol) {
                componentType = checker.getTypeOfSymbolAtLocation(componentSymbol, components.node);
            }
        }
    }
    return componentType;
}
function getSelfComponentName(fileName) {
    const baseName = path.basename(fileName);
    return (0, shared_1.capitalize)((0, shared_1.camelize)(baseName.slice(0, baseName.lastIndexOf('.'))));
}
function getVariableType(ts, languageService, vueCode, name) {
    const program = languageService.getProgram();
    let tsSourceFile;
    if (tsSourceFile = program.getSourceFile(vueCode.fileName)) {
        const node = searchVariableDeclarationNode(ts, tsSourceFile, name);
        const checker = program.getTypeChecker();
        if (checker && node) {
            return {
                node: node,
                type: checker.getTypeAtLocation(node),
            };
        }
    }
}
function searchVariableDeclarationNode(ts, sourceFile, name) {
    let componentsNode;
    walk(sourceFile);
    return componentsNode;
    function walk(node) {
        if (componentsNode) {
            return;
        }
        else if (ts.isVariableDeclaration(node) && node.name.getText() === name) {
            componentsNode = node;
        }
        else {
            node.forEachChild(walk);
        }
    }
}
//# sourceMappingURL=utils.js.map