'use strict';

var semantic = require('@lokascript/semantic');

let _semanticModule = null;
async function getSemanticModule() {
    if (!_semanticModule) {
        _semanticModule = await import('@lokascript/semantic');
    }
    return _semanticModule;
}
class SemanticGrammarBridge {
    constructor(config = {}) {
        this.analyzer = null;
        this.config = {
            confidenceThreshold: config.confidenceThreshold ?? semantic.DEFAULT_CONFIDENCE_THRESHOLD,
            fallbackOnLowConfidence: config.fallbackOnLowConfidence ?? true,
        };
    }
    async initialize() {
        const semantic = await getSemanticModule();
        this.analyzer = semantic.createSemanticAnalyzer();
    }
    isInitialized() {
        return this.analyzer !== null;
    }
    async transform(input, sourceLang, targetLang) {
        if (!this.isInitialized()) {
            await this.initialize();
        }
        if (sourceLang === targetLang) {
            return {
                output: input,
                usedSemantic: false,
                confidence: 1.0,
                sourceLang,
                targetLang,
            };
        }
        const semantic = await getSemanticModule();
        try {
            const output = semantic.translate(input, sourceLang, targetLang);
            if (output !== input) {
                return {
                    output,
                    usedSemantic: true,
                    confidence: 0.9,
                    sourceLang,
                    targetLang,
                };
            }
        }
        catch {
        }
        return {
            output: input,
            usedSemantic: false,
            confidence: 0,
            sourceLang,
            targetLang,
        };
    }
    async parse(input, lang) {
        if (!this.isInitialized()) {
            await this.initialize();
        }
        if (!this.analyzer)
            return null;
        const result = this.analyzer.analyze(input, lang);
        return result.node ?? null;
    }
    async render(node, targetLang) {
        const semantic = await getSemanticModule();
        return semantic.render(node, targetLang);
    }
    async parseToAST(input, lang) {
        if (!this.isInitialized()) {
            await this.initialize();
        }
        if (!this.analyzer)
            return null;
        const result = this.analyzer.analyze(input, lang);
        if (result.confidence >= this.config.confidenceThreshold && result.node) {
            const semantic = await getSemanticModule();
            try {
                const buildResult = semantic.buildAST(result.node);
                return buildResult.ast;
            }
            catch (error) {
                console.warn('[SemanticGrammarBridge] AST build failed, using fallback:', error);
            }
        }
        return null;
    }
    async parseToASTWithDetails(input, lang) {
        if (!this.isInitialized()) {
            await this.initialize();
        }
        if (!this.analyzer) {
            return {
                ast: null,
                usedDirectPath: false,
                confidence: 0,
                lang,
                fallbackText: null,
            };
        }
        const result = this.analyzer.analyze(input, lang);
        const semantic = await getSemanticModule();
        if (result.confidence >= this.config.confidenceThreshold && result.node) {
            try {
                const buildResult = semantic.buildAST(result.node);
                return {
                    ast: buildResult.ast,
                    usedDirectPath: true,
                    confidence: result.confidence,
                    lang,
                    fallbackText: null,
                    warnings: buildResult.warnings,
                };
            }
            catch {
            }
        }
        if (result.node && this.config.fallbackOnLowConfidence) {
            const englishText = semantic.render(result.node, 'en');
            return {
                ast: null,
                usedDirectPath: false,
                confidence: result.confidence,
                lang,
                fallbackText: englishText,
            };
        }
        return {
            ast: null,
            usedDirectPath: false,
            confidence: result.confidence,
            lang,
            fallbackText: null,
        };
    }
    async getAllTranslations(input, sourceLang) {
        const semantic = await getSemanticModule();
        const languages = semantic.getSupportedLanguages();
        const results = {};
        for (const lang of languages) {
            results[lang] = await this.transform(input, sourceLang, lang);
        }
        return results;
    }
}
let _defaultBridge = null;
async function getDefaultBridge() {
    if (!_defaultBridge) {
        _defaultBridge = new SemanticGrammarBridge();
        await _defaultBridge.initialize();
    }
    return _defaultBridge;
}
async function translate(input, sourceLang, targetLang) {
    const bridge = await getDefaultBridge();
    const result = await bridge.transform(input, sourceLang, targetLang);
    return result.output;
}

class MultilingualHyperscript {
    constructor(config = {}) {
        this.initialized = false;
        this.bridge = new SemanticGrammarBridge(config);
    }
    async initialize() {
        if (this.initialized)
            return;
        await this.bridge.initialize();
        this.initialized = true;
    }
    isInitialized() {
        return this.initialized;
    }
    async ensureInitialized() {
        if (!this.initialized) {
            await this.initialize();
        }
    }
    async parse(input, lang = 'en') {
        await this.ensureInitialized();
        return this.bridge.parse(input, lang);
    }
    async parseToAST(input, lang = 'en') {
        await this.ensureInitialized();
        return this.bridge.parseToAST(input, lang);
    }
    async parseToASTWithDetails(input, lang = 'en') {
        await this.ensureInitialized();
        return this.bridge.parseToASTWithDetails(input, lang);
    }
    async translate(input, from, to) {
        await this.ensureInitialized();
        const result = await this.bridge.transform(input, from, to);
        return result.output;
    }
    async translateWithDetails(input, from, to) {
        await this.ensureInitialized();
        return this.bridge.transform(input, from, to);
    }
    async render(node, lang) {
        await this.ensureInitialized();
        return this.bridge.render(node, lang);
    }
    async getAllTranslations(input, from) {
        await this.ensureInitialized();
        return this.bridge.getAllTranslations(input, from);
    }
    getSupportedLanguages() {
        return [
            'en',
            'ja',
            'ar',
            'es',
            'ko',
            'zh',
            'tr',
            'pt',
            'fr',
            'de',
            'id',
            'qu',
            'sw',
        ];
    }
    isLanguageSupported(lang) {
        return this.getSupportedLanguages().includes(lang);
    }
    getLanguageInfo(lang) {
        return LANGUAGE_INFO[lang];
    }
    getAllLanguageInfo() {
        return { ...LANGUAGE_INFO };
    }
}
const LANGUAGE_INFO = {
    en: { code: 'en', name: 'English', nativeName: 'English', direction: 'ltr', wordOrder: 'SVO' },
    ja: { code: 'ja', name: 'Japanese', nativeName: '日本語', direction: 'ltr', wordOrder: 'SOV' },
    ar: { code: 'ar', name: 'Arabic', nativeName: 'العربية', direction: 'rtl', wordOrder: 'VSO' },
    es: { code: 'es', name: 'Spanish', nativeName: 'Español', direction: 'ltr', wordOrder: 'SVO' },
    ko: { code: 'ko', name: 'Korean', nativeName: '한국어', direction: 'ltr', wordOrder: 'SOV' },
    zh: { code: 'zh', name: 'Chinese', nativeName: '中文', direction: 'ltr', wordOrder: 'SVO' },
    tr: { code: 'tr', name: 'Turkish', nativeName: 'Türkçe', direction: 'ltr', wordOrder: 'SOV' },
    pt: {
        code: 'pt',
        name: 'Portuguese',
        nativeName: 'Português',
        direction: 'ltr',
        wordOrder: 'SVO',
    },
    fr: { code: 'fr', name: 'French', nativeName: 'Français', direction: 'ltr', wordOrder: 'SVO' },
    de: { code: 'de', name: 'German', nativeName: 'Deutsch', direction: 'ltr', wordOrder: 'SVO' },
    id: {
        code: 'id',
        name: 'Indonesian',
        nativeName: 'Bahasa Indonesia',
        direction: 'ltr',
        wordOrder: 'SVO',
    },
    qu: { code: 'qu', name: 'Quechua', nativeName: 'Runasimi', direction: 'ltr', wordOrder: 'SOV' },
    sw: { code: 'sw', name: 'Swahili', nativeName: 'Kiswahili', direction: 'ltr', wordOrder: 'SVO' },
};
let _defaultInstance = null;
async function getMultilingual() {
    if (!_defaultInstance) {
        _defaultInstance = new MultilingualHyperscript();
        await _defaultInstance.initialize();
    }
    return _defaultInstance;
}
const multilingual = new MultilingualHyperscript();

exports.MultilingualHyperscript = MultilingualHyperscript;
exports.SemanticGrammarBridge = SemanticGrammarBridge;
exports.getDefaultBridge = getDefaultBridge;
exports.getMultilingual = getMultilingual;
exports.multilingual = multilingual;
exports.translate = translate;
//# sourceMappingURL=index.js.map
