export class ProviderError extends Error {
    code;
    constructor(code) {
        super(code);
        this.code = code;
    }
}
export class BrowserCaptionTranslator {
    async availability(sourceLanguage, targetLanguage) {
        const api = browserTranslator();
        if (!api)
            return "unavailable";
        try {
            return (await api.availability({ sourceLanguage, targetLanguage })) === "available" ? "available" : "unavailable";
        }
        catch {
            return "unavailable";
        }
    }
    async translate(text, sourceLanguage, targetLanguage, signal) {
        if (signal.aborted)
            throw new ProviderError("cancelled");
        const api = browserTranslator();
        if (!api)
            throw new ProviderError("translator_unavailable");
        if (await this.availability(sourceLanguage, targetLanguage) !== "available")
            throw new ProviderError("language_unsupported");
        try {
            const translator = await api.create({ sourceLanguage, targetLanguage });
            if (signal.aborted)
                throw new ProviderError("cancelled");
            const translation = await translator.translate(text);
            translator.destroy?.();
            if (signal.aborted)
                throw new ProviderError("cancelled");
            return translation.trim();
        }
        catch (error) {
            if (error instanceof ProviderError)
                throw error;
            throw new ProviderError("translator_failed");
        }
    }
}
export class UnavailableAudioTranscriber {
    async availability() { return "unconfigured"; }
    push(_frame) { }
    stop() { }
}
function browserTranslator() {
    return globalThis.Translator;
}
