import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
//#region src/voice/tts/preprocess.ts
init_logger();
const log = createLogger("TTS:Preprocess");
function stripMarkdown(text) {
	return text.replace(/```[\s\S]*?```/g, (match) => {
		return match.replace(/```(\w+)?\n?/, "").replace(/```$/, "").trim();
	}).replace(/^#{1,6}\s+/gm, "").replace(/(\*\*|__)(.+?)\1/g, "$2").replace(/(\*|_)(.+?)\1/g, "$2").replace(/`([^`]+)`/g, "$1").replace(/^\s*>\s*/gm, "").replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/~~(.+?)~~/g, "$1").replace(/^(---+|\*\*\*+|___+)\s*$/gm, "").replace(/^\s*[-*+]\s+/gm, "").replace(/^\s*\d+\.\s+/gm, "").replace(/<[^>]+>/g, "");
}
function normalizeWhitespace(text) {
	return text.replace(/[ \t]+/g, " ").replace(/\n{3,}/g, "\n\n").split("\n").map((line) => line.trim()).join("\n").trim();
}
function truncateText(text, maxLength) {
	if (text.length <= maxLength) return {
		text,
		wasTruncated: false
	};
	let truncateAt = maxLength - 3;
	while (truncateAt > 0 && text[truncateAt] !== " " && text[truncateAt] !== "\n") truncateAt--;
	if (truncateAt <= maxLength * .8) truncateAt = maxLength - 3;
	return {
		text: text.slice(0, truncateAt).trim() + "...",
		wasTruncated: true
	};
}
function preprocessText(text, options = {}) {
	const { maxLength = 4096, stripMarkdown: shouldStripMarkdown = true, normalizeWhitespace: shouldNormalizeWhitespace = true } = options;
	const originalLength = text.length;
	let processed = text;
	if (shouldStripMarkdown) {
		processed = stripMarkdown(processed);
		log.debug({
			originalLength,
			afterMarkdown: processed.length
		}, "Stripped markdown");
	}
	if (shouldNormalizeWhitespace) {
		processed = normalizeWhitespace(processed);
		log.debug({ afterWhitespace: processed.length }, "Normalized whitespace");
	}
	const { text: finalText, wasTruncated } = truncateText(processed, maxLength);
	if (wasTruncated) log.warn({
		originalLength,
		finalLength: finalText.length,
		maxLength
	}, "Text truncated for TTS");
	return {
		text: finalText,
		wasTruncated,
		originalLength,
		finalLength: finalText.length
	};
}
function checkTTSSuitability(text) {
	const trimmed = text.trim();
	if (trimmed.length < 10) return {
		suitable: false,
		reason: "Text too short for TTS (min 10 chars)"
	};
	const codeMatches = trimmed.match(/[{};\[\]\(\)=><]/g);
	if ((codeMatches ? codeMatches.length / trimmed.length : 0) > .3) return {
		suitable: false,
		reason: "Text appears to be mostly code"
	};
	if (/^(https?:\/\/\S+\s*)+$/.test(trimmed)) return {
		suitable: false,
		reason: "Text contains only URLs"
	};
	return { suitable: true };
}
//#endregion
export { checkTTSSuitability, normalizeWhitespace, preprocessText, stripMarkdown, truncateText };

//# sourceMappingURL=preprocess.js.map