/** SDK-owned platform module. This implementation is maintained in goodvibes-sdk. */
// ---------------------------------------------------------------------------
// Sanitizer implementations
// ---------------------------------------------------------------------------
/**
 * `none` mode — returns content verbatim.
 */
function sanitizeNone(content) {
    return { content, mode: 'none', modified: false };
}
/**
 * `safe-text` mode — strips script/style elements and C0/C1 control
 * characters (except tab, newline, carriage return).
 *
 * This mode is appropriate for general external content where HTML injection
 * or binary smuggling via control sequences is a concern.
 */
function sanitizeSafeText(content) {
    const original = content;
    let sanitized = content
        // Remove script blocks (including event handlers encoded in attributes)
        .replace(/<script[\s\S]*?<\/script>/gi, '')
        // Remove style blocks
        .replace(/<style[\s\S]*?<\/style>/gi, '')
        // Strip C0 control characters (0x00-0x08, 0x0B, 0x0C, 0x0E-0x1F) and DEL (0x7F)
        // Keep: 0x09 (tab), 0x0A (LF), 0x0D (CR)
        // eslint-disable-next-line no-control-regex
        .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '')
        // Strip C1 control characters (0x80-0x9F)
        // eslint-disable-next-line no-control-regex
        .replace(/[\x80-\x9F]/g, '');
    return {
        content: sanitized,
        mode: 'safe-text',
        modified: sanitized !== original,
    };
}
/**
 * `strict` mode — strips all HTML tags, script/style blocks, and any
 * character outside printable ASCII (0x20-0x7E) plus tab (0x09),
 * newline (0x0A), and carriage return (0x0D).
 *
 * Use for unknown or untrusted hosts where maximum reduction of attack
 * surface is required.
 */
function sanitizeStrict(content) {
    const original = content;
    let sanitized = content
        // Remove script blocks
        .replace(/<script[\s\S]*?<\/script>/gi, '')
        // Remove style blocks
        .replace(/<style[\s\S]*?<\/style>/gi, '')
        // Strip all remaining HTML tags
        .replace(/<[^>]+>/g, ' ')
        // Allow only: printable ASCII (0x20-0x7E), tab (0x09), LF (0x0A), CR (0x0D)
        // eslint-disable-next-line no-control-regex
        .replace(/[^\x09\x0A\x0D\x20-\x7E]/g, '')
        // Collapse multiple consecutive spaces to single space
        .replace(/ {2,}/g, ' ')
        .trim();
    return {
        content: sanitized,
        mode: 'strict',
        modified: sanitized !== original,
    };
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Apply the specified sanitization mode to response content.
 *
 * This function is deterministic: given the same `content` and `mode`, it
 * always produces the same output. The `modified` field in the result
 * indicates whether any transformation occurred.
 *
 * @param content - Raw response body string.
 * @param mode    - Sanitization mode to apply.
 * @returns       `SanitizeResult` with sanitized content, mode applied, and
 *                whether the content was modified.
 */
export function applySanitizer(content, mode) {
    switch (mode) {
        case 'none':
            return sanitizeNone(content);
        case 'safe-text':
            return sanitizeSafeText(content);
        case 'strict':
            return sanitizeStrict(content);
        default: {
            // Exhaustiveness guard — TypeScript ensures this is unreachable
            const _exhaustive = mode;
            return sanitizeSafeText(content);
        }
    }
}
/**
 * Resolve the effective sanitize mode, applying the rollback default.
 *
 * When no explicit mode is requested, defaults to `'safe-text'` — the
 * rollback-safe default for fetch sanitization.
 *
 * @param requested - Caller-supplied mode (may be undefined).
 * @returns         Effective `SanitizeMode` to apply.
 */
export function resolveSanitizeMode(requested) {
    return requested ?? 'safe-text';
}
