import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { transcribe } from "./transcribe-core.js";
import { isSTTAvailable } from "./availability.js";
//#region src/voice/stt/preflight.ts
init_logger();
const log = createLogger("STT:Preflight");
/**
* Transcribe audio before mention routing (e.g. group voice → detect spoken @bot).
*/
async function audioPreflightTranscribe(input) {
	const { audioBuffer, sttConfig, language } = input;
	if (!isSTTAvailable(sttConfig)) {
		log.debug("STT not available, skipping preflight");
		return {
			success: false,
			error: "STT not available"
		};
	}
	if (!audioBuffer || audioBuffer.length === 0) return {
		success: false,
		error: "Empty audio buffer"
	};
	const startTime = Date.now();
	try {
		const result = await transcribe(audioBuffer, sttConfig, { language });
		const duration = (Date.now() - startTime) / 1e3;
		if (!result.text?.trim()) {
			log.debug({ duration }, "Preflight transcription returned empty text");
			return {
				success: false,
				provider: result.provider,
				duration,
				error: "Empty transcription result"
			};
		}
		log.info({
			provider: result.provider,
			textLength: result.text.length,
			duration
		}, "Audio preflight transcription completed");
		return {
			success: true,
			transcribedText: result.text,
			provider: result.provider,
			duration
		};
	} catch (error) {
		const duration = (Date.now() - startTime) / 1e3;
		const errorMsg = error instanceof Error ? error.message : String(error);
		log.warn({
			errorMessage: errorMsg,
			duration
		}, "Audio preflight transcription failed");
		return {
			success: false,
			duration,
			error: errorMsg
		};
	}
}
/**
* Spoken @bot variants ("at botname", "hey botname") for STT text.
*/
function checkMentionInTranscription(transcribedText, botNames) {
	const normalizedText = transcribedText.toLowerCase().trim();
	for (const name of botNames) {
		const normalizedName = name.toLowerCase().trim();
		if (!normalizedName) continue;
		if (normalizedText.includes(normalizedName)) return true;
		const fuzzyPatterns = [
			`at ${normalizedName}`,
			`hey ${normalizedName}`,
			`hi ${normalizedName}`
		];
		for (const pattern of fuzzyPatterns) if (normalizedText.includes(pattern)) return true;
	}
	return false;
}
//#endregion
export { audioPreflightTranscribe, checkMentionInTranscription };

//# sourceMappingURL=preflight.js.map