import { RATE_LIMIT_ERROR_MESSAGE } from '../../core/models/response.js';
const RATE_LIMIT_ERROR_PATTERNS = [
    /\bhttp\s+429\b/i,
    /\b429\b/i,
    /rate[_\s-]?limit/i,
    /too many requests/i,
    /out of extra usage/i,
    /usage_limit_exceeded/i,
];
const RATE_LIMIT_STREAM_MARKER_PATTERNS = [
    /out of extra usage/i,
    /usage_limit_exceeded/i,
    /resets?\s+\d{1,2}:\d{2}\s*(?:am|pm)?/i,
];
export function containsRateLimitMarker(text) {
    if (!text) {
        return false;
    }
    return RATE_LIMIT_STREAM_MARKER_PATTERNS.some((pattern) => pattern.test(text));
}
export function containsRateLimitError(text) {
    if (!text) {
        return false;
    }
    return RATE_LIMIT_ERROR_PATTERNS.some((pattern) => pattern.test(text));
}
export function resolveRateLimitTextSource(text) {
    if (containsRateLimitMarker(text)) {
        return 'stream_marker';
    }
    if (containsRateLimitError(text)) {
        return 'error_text';
    }
    return undefined;
}
export function buildRateLimitInfo(provider, source, text) {
    const resetAtRaw = text?.match(/resets?\s+([^\n\r]+)/i)?.[1]?.trim();
    return {
        provider,
        detectedAt: new Date(),
        source,
        ...(resetAtRaw ? { resetAtRaw } : {}),
    };
}
export function resolveRateLimitErrorMessage(text) {
    const message = text?.trim();
    return message && message.length > 0 ? message : RATE_LIMIT_ERROR_MESSAGE;
}
export function buildRateLimitedResponseFields(provider, source, text) {
    return {
        status: 'rate_limited',
        content: '',
        error: resolveRateLimitErrorMessage(text),
        errorKind: 'rate_limit',
        rateLimitInfo: buildRateLimitInfo(provider, source, text),
    };
}
//# sourceMappingURL=detection.js.map