"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTagNameCasing = getTagNameCasing;
exports.getAttrNameCasing = getAttrNameCasing;
const language_core_1 = require("@vue/language-core");
const collectCache = new WeakMap();
async function getTagNameCasing(context, uri) {
    const config = await context.env.getConfiguration?.('vue.suggest.componentNameCasing', uri.toString());
    if (config === 'alwaysKebabCase') {
        return 0 /* TagNameCasing.Kebab */;
    }
    if (config === 'alwaysPascalCase') {
        return 1 /* TagNameCasing.Pascal */;
    }
    const root = context.language.scripts.get(uri)?.generated?.root;
    if (root instanceof language_core_1.VueVirtualCode) {
        const detectedCasings = detectTagCasing(root);
        if (detectedCasings.length === 1) {
            return detectedCasings[0];
        }
    }
    if (config === 'preferKebabCase') {
        return 0 /* TagNameCasing.Kebab */;
    }
    return 1 /* TagNameCasing.Pascal */;
}
async function getAttrNameCasing(context, uri) {
    const config = await context.env.getConfiguration?.('vue.suggest.propNameCasing', uri.toString());
    if (config === 'alwaysKebabCase') {
        return 0 /* AttrNameCasing.Kebab */;
    }
    if (config === 'alwaysCamelCase') {
        return 1 /* AttrNameCasing.Camel */;
    }
    const root = context.language.scripts.get(uri)?.generated?.root;
    if (root instanceof language_core_1.VueVirtualCode) {
        const detectedCasings = detectAttrCasing(root);
        if (detectedCasings.length === 1) {
            return detectedCasings[0];
        }
    }
    if (config === 'preferKebabCase') {
        return 0 /* AttrNameCasing.Kebab */;
    }
    return 1 /* AttrNameCasing.Camel */;
}
function detectAttrCasing(code) {
    const tags = collectTagsWithCache(code);
    const result = new Set();
    for (const [, [_, attrs]] of tags) {
        for (const attr of attrs) {
            if (attr !== (0, language_core_1.hyphenateTag)(attr)) {
                result.add(1 /* AttrNameCasing.Camel */);
                break;
            }
        }
        for (const attr of attrs) {
            if (attr.includes('-')) {
                result.add(0 /* AttrNameCasing.Kebab */);
                break;
            }
        }
    }
    return [...result];
}
function detectTagCasing(code) {
    const tags = collectTagsWithCache(code);
    const result = new Set();
    for (const [tag, [tagType]] of tags) {
        if (tagType === 0
            || tagType === 3) {
            continue;
        }
        if (tag !== (0, language_core_1.hyphenateTag)(tag)) {
            result.add(1 /* TagNameCasing.Pascal */);
        }
        else {
            result.add(0 /* TagNameCasing.Kebab */);
        }
    }
    return [...result];
}
function collectTagsWithCache(code) {
    let cache = collectCache.get(code);
    if (!cache) {
        const ast = code.ir.template?.ast;
        cache = ast ? collectTags(ast) : new Map();
        collectCache.set(code, cache);
    }
    return cache;
}
function collectTags(ast) {
    const tags = new Map();
    for (const node of (0, language_core_1.forEachElementNode)(ast)) {
        let tag = tags.get(node.tag);
        if (!tag) {
            tags.set(node.tag, tag = [node.tagType, []]);
        }
        for (const prop of node.props) {
            let name;
            if (prop.type === 7
                && prop.arg?.type === 4
                && prop.arg.isStatic) {
                name = prop.arg.content;
            }
            else if (prop.type === 6) {
                name = prop.name;
            }
            if (name !== undefined) {
                tag[1].push(name);
            }
        }
    }
    return tags;
}
//# sourceMappingURL=nameCasing.js.map