const WORD_SPLIT_PATTERN = /\s+/g;
export function normalizeForPrompt(input) {
    return stripInvisibleUnicode(input)
        .replaceAll("\u00A0", " ")
        .replaceAll(/[\t ]+/g, " ")
        .replaceAll(/\s*\n\s*/g, "\n")
        .replaceAll(/\n{3,}/g, "\n\n")
        .trim();
}
export function normalizeWhitespace(input) {
    return stripInvisibleUnicode(input)
        .replaceAll("\u00A0", " ")
        .replaceAll(/[\t ]+/g, " ")
        .replaceAll(/\s*\n\s*/g, "\n")
        .trim();
}
export function decodeHtmlEntities(input) {
    return input
        .replaceAll("&amp;", "&")
        .replaceAll("&lt;", "<")
        .replaceAll("&gt;", ">")
        .replaceAll("&quot;", '"')
        .replaceAll("&#39;", "'")
        .replaceAll("&#x27;", "'")
        .replaceAll("&#x2F;", "/")
        .replaceAll("&nbsp;", " ");
}
export function stripInvisibleUnicode(input) {
    return input.replaceAll(/[\u200B-\u200F\u202A-\u202E\u2060-\u2069\uFEFF\u{E0000}-\u{E007F}]/gu, "");
}
export function normalizeCandidate(value) {
    if (!value) {
        return null;
    }
    const trimmed = value.replaceAll(/\s+/g, " ").trim();
    return trimmed.length > 0 ? trimmed : null;
}
export function clipAtSentenceBoundary(input, maxLength) {
    if (input.length <= maxLength) {
        return input;
    }
    const slice = input.slice(0, maxLength);
    const lastSentenceBreak = Math.max(slice.lastIndexOf(". "), slice.lastIndexOf("! "), slice.lastIndexOf("? "), slice.lastIndexOf("\n\n"));
    if (lastSentenceBreak > maxLength * 0.5) {
        return slice.slice(0, lastSentenceBreak + 1);
    }
    return slice;
}
export function applyContentBudget(baseContent, maxCharacters) {
    const totalCharacters = baseContent.length;
    const truncated = totalCharacters > maxCharacters;
    const clipped = truncated ? clipAtSentenceBoundary(baseContent, maxCharacters) : baseContent;
    const content = clipped.trim();
    const wordCount = content.length > 0
        ? content.split(WORD_SPLIT_PATTERN).filter((part) => part.length > 0).length
        : 0;
    return { content, truncated, totalCharacters, wordCount };
}
//# sourceMappingURL=cleaner.js.map