import { getMessages } from '../../core/messages/index.js';
import { parseComBabel, parseComCss, parseComHtmlFunc, parseComPhp, parseComPython, parseComTypeScript, parseComXml, } from '../../core/parsing/langs/index.js';
const { logCore } = getMessages();
export class CorePlugin {
    name = 'core';
    version = '0.2.0';
    extensions = ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '.html', '.htm', '.css', '.xml', '.php', '.py', '.d.ts'];
    async parse(codigo, opts) {
        const ext = this.inferExtension(codigo, opts);
        switch (ext) {
            case '.js':
            case '.jsx':
            case '.mjs':
            case '.cjs':
                return this.parseJavaScript(codigo, opts);
            case '.ts':
            case '.tsx':
                return this.parseTypeScript(codigo, opts);
            case '.d.ts':
                return null;
            case '.html':
            case '.htm':
                return parseComHtmlFunc(codigo);
            case '.css':
                return parseComCss(codigo);
            case '.xml':
                return parseComXml(codigo);
            case '.php':
                return parseComPhp(codigo);
            case '.py':
                return parseComPython(codigo);
            default:
                logCore.extensaoNaoSuportada(ext);
                return null;
        }
    }
    parseJavaScript(codigo, opts) {
        let result = parseComBabel(codigo, opts?.plugins);
        if (result === null) {
            const pareceFlow = /@flow\b/.test(codigo) || /\bimport\s+type\b/.test(codigo);
            if (pareceFlow) {
                const flowPlugins = ['flow', 'jsx', 'decorators-legacy', 'importAttributes', 'importAssertions', 'classProperties', 'classPrivateProperties', 'classPrivateMethods', 'optionalChaining', 'nullishCoalescingOperator', 'topLevelAwait'];
                result = parseComBabel(codigo, flowPlugins);
            }
            if (result === null) {
                const jsModernPlugins = ['jsx', 'decorators-legacy', 'importAttributes', 'importAssertions', 'classProperties', 'classPrivateProperties', 'classPrivateMethods', 'optionalChaining', 'nullishCoalescingOperator', 'topLevelAwait'];
                result = parseComBabel(codigo, jsModernPlugins);
            }
        }
        return result;
    }
    parseTypeScript(codigo, opts) {
        const ext = this.inferExtension(codigo, opts);
        const isTsx = ext === '.tsx';
        let result = parseComBabel(codigo, opts?.plugins);
        if (result === null) {
            result = parseComTypeScript(codigo, isTsx);
        }
        return result;
    }
    inferExtension(codigo, opts) {
        if (opts?.pluginConfig?.extension) {
            return String(opts.pluginConfig.extension);
        }
        if (/^<\?php/.test(codigo.trim())) {
            return '.php';
        }
        if (/^(import |from .+ import |def |class )/m.test(codigo)) {
            return '.py';
        }
        if (/\bfrom\s+['"][^'"]+\.tsx?['"]/.test(codigo) || /<[A-Z]/.test(codigo)) {
            return codigo.includes('interface ') || codigo.includes('type ') ? '.tsx' : '.jsx';
        }
        if (/\binterface\s+\w+|\btype\s+\w+\s*=/.test(codigo)) {
            return '.ts';
        }
        if (/^<\?xml/.test(codigo.trim())) {
            return '.xml';
        }
        if (/<[a-z][\w-]*/.test(codigo)) {
            return '.html';
        }
        if (/\{[\s\S]*\}/.test(codigo) && /[a-z-]+\s*:/.test(codigo)) {
            return '.css';
        }
        return '.js';
    }
    validate(codigo) {
        if (!codigo || typeof codigo !== 'string') {
            return false;
        }
        if (/[\x00-\x08\x0E-\x1F\x7F]/.test(codigo)) {
            return false;
        }
        return true;
    }
}
export const corePlugin = new CorePlugin();
//# sourceMappingURL=core-plugin.js.map