import { parse as babelParse } from '@babel/parser';
import { wrapMinimal } from '../utils.js';
function extractTags(codigo) {
    const tags = {};
    const tagRegex = /<(\/?)([A-Za-z][A-Za-z0-9]*)\b[^>]*>/g;
    let match;
    while ((match = tagRegex.exec(codigo)) !== null) {
        if (match[1] !== '/') {
            const name = match[2].toLowerCase();
            tags[name] = (tags[name] || 0) + 1;
        }
    }
    return tags;
}
function extractCssClasses(codigo) {
    return [...new Set(Array.from(codigo.matchAll(/\.([A-Za-z_][\w-]*)/g)).map(m => m[1]))];
}
export function parseComVue(codigo) {
    try {
        if (!codigo || !codigo.trim())
            return null;
        const templateMatch = codigo.match(/<template\b[^>]*>([\s\S]*?)<\/template\b[^>]*>/i);
        const scriptMatch = codigo.match(/<script\b[^>]*>([\s\S]*?)<\/script\b[^>]*>/i);
        const styleMatch = codigo.match(/<style\b[^>]*>([\s\S]*?)<\/style\b[^>]*>/i);
        const template = templateMatch ? templateMatch[1].trim() : '';
        const script = scriptMatch ? scriptMatch[1].trim() : '';
        const style = styleMatch ? styleMatch[1].trim() : '';
        if (!template && !script && !style)
            return null;
        let scriptInfo = { parsed: false };
        if (script) {
            try {
                const ast = babelParse(script, {
                    sourceType: 'module',
                    plugins: ['typescript', 'jsx'],
                    allowImportExportEverywhere: true
                });
                const imports = [];
                const components = [];
                for (const node of ast.program.body) {
                    if (node.type === 'ImportDeclaration') {
                        imports.push(node.source.value);
                    }
                    if (node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ObjectExpression') {
                        for (const prop of node.declaration.properties) {
                            if (prop.type === 'ObjectProperty' && prop.key.type === 'Identifier' && prop.key.name === 'components' && prop.value.type === 'ObjectExpression') {
                                for (const comp of prop.value.properties) {
                                    if (comp.type === 'ObjectProperty' && comp.key.type === 'Identifier') {
                                        components.push(comp.key.name);
                                    }
                                }
                            }
                        }
                    }
                }
                scriptInfo = { parsed: true, imports, components };
            }
            catch {
                try {
                    babelParse(script, {
                        sourceType: 'module',
                        plugins: ['jsx'],
                        allowImportExportEverywhere: true
                    });
                    scriptInfo = { parsed: true };
                }
                catch {
                    scriptInfo = { parsed: false };
                }
            }
        }
        let templateInfo = { parsed: false };
        if (template) {
            const tags = extractTags(template);
            let totalTags = 0;
            let uniqueTags = 0;
            for (const _key in tags) {
                totalTags += tags[_key];
                uniqueTags++;
            }
            templateInfo = {
                parsed: true,
                tags,
                totalTags,
                uniqueTags
            };
        }
        let styleInfo = { parsed: false };
        if (style) {
            const classes = extractCssClasses(style);
            styleInfo = {
                parsed: true,
                classes,
                scoped: /scoped/.test(styleMatch?.[0] || ''),
                hasVars: /\$/.test(style)
            };
        }
        return wrapMinimal('vue', {
            template: template ? 'present' : null,
            script: script ? 'present' : null,
            style: style ? 'present' : null,
            hasTemplate: !!template,
            hasScript: !!script,
            hasStyle: !!style,
            templateContent: template,
            scriptContent: script,
            styleContent: style,
            scriptAnalysis: scriptInfo,
            templateAnalysis: templateInfo,
            styleAnalysis: styleInfo
        });
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=vue.js.map