"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTagNameCasing = getTagNameCasing;
exports.getAttrNameCasing = getAttrNameCasing;
const language_core_1 = require("@vue/language-core");
const signals_1 = require("@vue/language-core/lib/utils/signals");
const nameCasingGraphs = 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 = getNameCasingGraph(root).tag();
        if (detectedCasings.size === 1) {
            return detectedCasings.values().next().value;
        }
    }
    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 = getNameCasingGraph(root).attr();
        if (detectedCasings.size === 1) {
            return detectedCasings.values().next().value;
        }
    }
    if (config === 'preferKebabCase') {
        return 0 /* AttrNameCasing.Kebab */;
    }
    return 1 /* AttrNameCasing.Camel */;
}
function getNameCasingGraph(code) {
    let graph = nameCasingGraphs.get(code);
    if (!graph) {
        nameCasingGraphs.set(code, graph = {
            tag: (0, signals_1.computedSet)(() => detectTagCasing(code.ir.template?.ast)),
            attr: (0, signals_1.computedSet)(() => detectAttrCasing(code.ir.template?.ast)),
        });
    }
    return graph;
}
function detectAttrCasing(ast) {
    const result = new Set();
    if (!ast) {
        return result;
    }
    for (const node of (0, language_core_1.forEachElementNode)(ast)) {
        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;
            }
            else {
                continue;
            }
            if (name !== (0, language_core_1.hyphenateTag)(name)) {
                result.add(1 /* AttrNameCasing.Camel */);
            }
            if (name.includes('-')) {
                result.add(0 /* AttrNameCasing.Kebab */);
            }
        }
    }
    return result;
}
function detectTagCasing(ast) {
    const result = new Set();
    if (!ast) {
        return result;
    }
    for (const { tag, tagType } of (0, language_core_1.forEachElementNode)(ast)) {
        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;
}
//# sourceMappingURL=nameCasing.js.map