// src/language-resolver.ts
function resolveLanguage(elt) {
  const dataLang = elt.getAttribute("data-lang");
  if (dataLang) return normalizeLangCode(dataLang);
  const hsLang = elt.getAttribute("data-hyperscript-lang") ?? elt.closest?.("[data-hyperscript-lang]")?.getAttribute("data-hyperscript-lang");
  if (hsLang) return normalizeLangCode(hsLang);
  const htmlLang = typeof document !== "undefined" ? document.documentElement?.lang : null;
  if (htmlLang && htmlLang !== "en") return normalizeLangCode(htmlLang);
  return null;
}
function normalizeLangCode(lang) {
  return lang.split("-")[0].toLowerCase();
}

// src/preprocessor.ts
import {
  translate,
  render,
  createSemanticAnalyzer,
  shouldUseSemanticResult,
  tryGetProfile
} from "@lokascript/semantic";
var DEFAULT_THRESHOLD = 0.5;
var DEFAULT_CONFIG = {
  confidenceThreshold: DEFAULT_THRESHOLD,
  strategy: "semantic",
  fallbackToOriginal: true
};
function resolveThreshold(threshold, lang) {
  if (typeof threshold === "number") return threshold;
  return threshold[lang] ?? threshold["*"] ?? DEFAULT_THRESHOLD;
}
var analyzer = null;
function getAnalyzer() {
  if (!analyzer) {
    analyzer = createSemanticAnalyzer();
  }
  return analyzer;
}
function preprocessToEnglish(src, lang, config = {}) {
  if (lang === "en") return src;
  const cfg = { ...DEFAULT_CONFIG, ...config };
  const fullResult = tryTranslateWithStrategies(src, lang, cfg);
  if (fullResult !== null) return fullResult;
  const stripped = stripEventPrefix(src);
  if (stripped) {
    const translated = tryTranslateWithStrategies(stripped.commands, lang, cfg);
    if (translated !== null) return stripped.prefix + translated;
  }
  return cfg.fallbackToOriginal ? src : src;
}
function tryTranslateWithStrategies(src, lang, cfg) {
  if (cfg.strategy === "semantic" || cfg.strategy === "auto") {
    const threshold = resolveThreshold(cfg.confidenceThreshold, lang);
    const result = trySemanticTranslation(src, lang, threshold);
    if (result !== null) return result;
  }
  if ((cfg.strategy === "auto" || cfg.strategy === "i18n") && cfg.i18nToEnglish) {
    const result = tryI18nTranslation(src, lang, cfg.i18nToEnglish);
    if (result !== null) return result;
  }
  return null;
}
var EVENT_PREFIX_RE = /^(on\s+(?:every\s+)?[\w-]+(?:\[.*?\])?(?:\.[\w-]+(?:\([^)]*\))?)*(?:\s+from\s+\S+)?(?:\s+queue\s+\w+)?\s+)/;
function stripEventPrefix(src) {
  const match = src.match(EVENT_PREFIX_RE);
  if (!match) return null;
  const prefix = match[1];
  const commands = src.slice(prefix.length);
  if (!commands) return null;
  return { prefix, commands };
}
function trySemanticTranslation(src, lang, threshold) {
  try {
    const statements = splitStatements(src, lang);
    if (statements.length > 1) {
      return translateCompound(statements, lang, threshold);
    }
    const sem = getAnalyzer();
    if (!sem.supportsLanguage(lang)) return null;
    const result = sem.analyze(src, lang);
    if (!shouldUseSemanticResult(result, threshold)) return null;
    if (result.node) {
      return render(result.node, "en");
    }
    return translate(src, lang, "en");
  } catch {
    return null;
  }
}
function tryI18nTranslation(src, lang, toEnglish) {
  try {
    const result = toEnglish(src, lang);
    return result !== src ? result : null;
  } catch {
    return null;
  }
}
function getThenKeywords(lang) {
  const keywords = ["then"];
  const profile = tryGetProfile(lang);
  if (profile?.keywords?.then) {
    const kw = profile.keywords.then;
    if (kw.primary && kw.primary !== "then") keywords.push(kw.primary);
    if (kw.alternatives) {
      for (const alt of kw.alternatives) {
        if (!keywords.includes(alt)) keywords.push(alt);
      }
    }
  }
  return keywords;
}
function splitStatements(src, lang) {
  const thenWords = getThenKeywords(lang);
  const escaped = thenWords.map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
  const pattern = new RegExp(`\\s+(?:${escaped.join("|")})\\s+`, "i");
  const lines = src.split("\n").map((l) => l.trim()).filter((l) => l.length > 0);
  const result = [];
  for (const line of lines) {
    const parts = line.split(pattern);
    result.push(...parts);
  }
  return result;
}
function translateCompound(statements, lang, threshold) {
  const translated = [];
  for (const stmt of statements) {
    const result = trySemanticTranslation(stmt.trim(), lang, threshold);
    if (result === null) return null;
    translated.push(result);
  }
  return translated.join(" then ");
}

// src/plugin.ts
function hyperscriptI18n(options = {}) {
  return function plugin(hs) {
    const { internals } = hs;
    const runtime = internals.runtime;
    const originalGetScript = runtime.getScript.bind(runtime);
    runtime.getScript = function(elt) {
      const src = originalGetScript(elt);
      if (!src) return null;
      const lang = resolveLanguageWithOptions(elt, options);
      if (!lang || lang === "en") return src;
      const english = preprocessToEnglish(src, lang, options);
      if (english !== src) {
        if (options.debug) {
          console.log(`[hyperscript-i18n] ${lang}: "${src}" \u2192 "${english}"`);
        }
      } else {
        console.warn(
          `[hyperscript-i18n] Translation unchanged for lang="${lang}": "${src.length > 60 ? src.slice(0, 60) + "\u2026" : src}". The input may not match any known pattern, or the language may not be registered. Original text will be passed to _hyperscript as-is.`
        );
      }
      return english;
    };
  };
}
function resolveLanguageWithOptions(elt, options) {
  if (options.languageAttribute) {
    const custom = elt.getAttribute(options.languageAttribute);
    if (custom) return custom.split("-")[0].toLowerCase();
  }
  const resolved = resolveLanguage(elt);
  if (resolved) return resolved;
  return options.defaultLanguage ?? null;
}
function preprocess(src, lang, config = {}) {
  if (lang === "en") return src;
  return preprocessToEnglish(src, lang, config);
}
export {
  hyperscriptI18n,
  preprocess,
  preprocessToEnglish,
  resolveLanguage
};
//# sourceMappingURL=index.js.map