// Public translation-engine abstraction. One implementation today
// (`chromeEngine` — the Chromium on-device Translator/LanguageDetector API);
// a Bergamot-WASM engine can implement the same interface later with no
// call-site changes. Privacy invariant: an engine MUST run on-device only —
// message text may never leave the machine. There is intentionally no network
// engine.

export type TranslateAvailability = 'unavailable' | 'downloadable' | 'downloading' | 'available';

export interface TranslationEngine {
  /** Is this engine usable in the current browser right now? */
  isSupported(): boolean;
  /** Detect the source language of `text` — a normalized base code (e.g. "de") or undefined. */
  detect(text: string): Promise<string | undefined>;
  /** Can this source→target pair be translated (and is the model downloaded)? */
  availability(source: string, target: string): Promise<TranslateAvailability>;
  /**
   * Translate `text` from `source` to `target`. May trigger a one-time on-device
   * model download (reported via `onDownloadProgress`, 0..1). Must be invoked
   * from a user gesture the first time a pair's model needs downloading.
   */
  translate(
    text: string,
    source: string,
    target: string,
    onDownloadProgress?: (progress: number) => void,
  ): Promise<string>;
}

// ── Ambient types for the Chromium on-device APIs ──────────────────────────────
// Experimental globals not present in TS's lib.dom; declared minimally here.
// See https://developer.chrome.com/docs/ai/translator-api and the MDN
// Translator_and_Language_Detector_APIs page.

interface CreateMonitor {
  addEventListener(type: 'downloadprogress', listener: (event: { loaded: number }) => void): void;
}

export interface ChromeTranslatorInstance {
  translate(input: string): Promise<string>;
  destroy?(): void;
}

export interface ChromeTranslatorFactory {
  availability(opts: {
    sourceLanguage: string;
    targetLanguage: string;
  }): Promise<TranslateAvailability>;
  create(opts: {
    sourceLanguage: string;
    targetLanguage: string;
    monitor?: (m: CreateMonitor) => void;
  }): Promise<ChromeTranslatorInstance>;
}

export interface ChromeDetectorResult {
  detectedLanguage: string;
  confidence: number;
}

export interface ChromeLanguageDetectorInstance {
  detect(input: string): Promise<ChromeDetectorResult[]>;
  destroy?(): void;
}

export interface ChromeLanguageDetectorFactory {
  create(opts?: { monitor?: (m: CreateMonitor) => void }): Promise<ChromeLanguageDetectorInstance>;
}

declare global {
  // eslint-disable-next-line vars-on-top
  let Translator: ChromeTranslatorFactory | undefined;
  // eslint-disable-next-line vars-on-top
  let LanguageDetector: ChromeLanguageDetectorFactory | undefined;
}
