"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getComponentType = getComponentType;
exports.getSelfComponentName = getSelfComponentName;
exports.getVariableType = getVariableType;
exports.hasBooleanType = hasBooleanType;
exports.forEachTouchingNode = forEachTouchingNode;
const language_core_1 = require("@vue/language-core");
const shared_1 = require("@vue/shared");
const path = __importStar(require("path-browserify"));
function getComponentType(ts, checker, sourceFile, { fileName, ir }, tag) {
    const testNames = new Set([
        tag,
        (0, shared_1.camelize)(tag),
        (0, shared_1.capitalize)((0, shared_1.camelize)(tag)),
    ]);
    const codegen = language_core_1.tsCodegen.get(ir);
    for (const importedName of codegen?.getImportedComponents() ?? []) {
        if (testNames.has(importedName)) {
            const node = searchDefaultImportIdentifier(ts, sourceFile, importedName);
            if (node) {
                return {
                    node,
                    type: checker.getTypeAtLocation(node),
                };
            }
        }
    }
    const components = getVariableType(ts, checker, sourceFile, language_core_1.names.components);
    if (components) {
        const nameParts = tag.split('.');
        let componentSymbol = components.type.getProperty(nameParts[0])
            ?? components.type.getProperty((0, shared_1.camelize)(nameParts[0]))
            ?? components.type.getProperty((0, shared_1.capitalize)((0, shared_1.camelize)(nameParts[0])));
        if (componentSymbol) {
            let componentType = checker.getTypeOfSymbolAtLocation(componentSymbol, components.node);
            for (let i = 1; i < nameParts.length; i++) {
                componentSymbol = componentType.getProperty(nameParts[i]);
                if (componentSymbol) {
                    componentType = checker.getTypeOfSymbolAtLocation(componentSymbol, components.node);
                }
            }
            if (componentType) {
                return {
                    node: components.node,
                    type: componentType,
                };
            }
        }
        else {
            const name = getSelfComponentName(fileName);
            if (name === (0, shared_1.capitalize)((0, shared_1.camelize)(tag))) {
                return getVariableType(ts, checker, sourceFile, language_core_1.names.export);
            }
        }
    }
    const selfName = getSelfComponentName(fileName);
    if (testNames.has(selfName)) {
        return getVariableType(ts, checker, sourceFile, language_core_1.names.export);
    }
}
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, checker, sourceFile, name) {
    const node = searchVariableDeclarationNode(ts, sourceFile, name);
    if (node) {
        return {
            node: node,
            type: checker.getTypeAtLocation(node),
        };
    }
}
function searchVariableDeclarationNode(ts, sourceFile, name) {
    let result;
    walk(sourceFile);
    return result;
    function walk(node) {
        if (result) {
            return;
        }
        else if (ts.isVariableDeclaration(node) && node.name.getText() === name) {
            result = node;
        }
        else {
            node.forEachChild(walk);
        }
    }
}
function searchDefaultImportIdentifier(ts, sourceFile, name) {
    let result;
    walk(sourceFile);
    return result;
    function walk(node) {
        if (result) {
            return;
        }
        else if (ts.isImportDeclaration(node)
            && node.importClause?.name?.text === name) {
            result = node.importClause.name;
        }
        else {
            node.forEachChild(walk);
        }
    }
}
function hasBooleanType(ts, type) {
    if (type.flags & ts.TypeFlags.BooleanLike) {
        return true;
    }
    if (type.isUnionOrIntersection()) {
        return type.types.some(type => hasBooleanType(ts, type));
    }
    return false;
}
function* forEachTouchingNode(ts, sourceFile, position) {
    yield* binaryVisit(ts, sourceFile, sourceFile, position);
}
function* binaryVisit(ts, sourceFile, node, position) {
    const nodes = [];
    ts.forEachChild(node, child => {
        nodes.push(child);
    });
    let left = 0;
    let right = nodes.length - 1;
    while (left <= right) {
        const mid = Math.floor((left + right) / 2);
        const node = nodes[mid];
        if (position > node.end) {
            left = mid + 1;
        }
        else if (position < node.getStart(sourceFile)) {
            right = mid - 1;
        }
        else {
            yield node;
            yield* binaryVisit(ts, sourceFile, node, position);
            return;
        }
    }
}
//# sourceMappingURL=utils.js.map