function isRecord(value) {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function detectMediaKind(part) {
    const typeValue = typeof part.type === 'string' ? part.type.trim().toLowerCase() : '';
    if (!typeValue) {
        return null;
    }
    if (typeValue.includes('video')) {
        return 'video';
    }
    if (typeValue.includes('image')) {
        return 'image';
    }
    return null;
}
function isInlineBase64(value) {
    const normalized = value.trim().toLowerCase();
    if (!normalized) {
        return false;
    }
    if (normalized.startsWith('data:') && normalized.includes(';base64,')) {
        return true;
    }
    if (normalized.startsWith('base64,')) {
        return true;
    }
    return false;
}
function mediaPartCarriesInlineBase64(part) {
    const candidates = [];
    const imageUrl = part.image_url;
    const videoUrl = part.video_url;
    if (typeof imageUrl === 'string') {
        candidates.push(imageUrl);
    }
    if (typeof videoUrl === 'string') {
        candidates.push(videoUrl);
    }
    if (isRecord(imageUrl)) {
        const url = typeof imageUrl.url === 'string' ? imageUrl.url : '';
        const data = typeof imageUrl.data === 'string' ? String(imageUrl.data) : '';
        const base64 = typeof imageUrl.base64 === 'string'
            ? String(imageUrl.base64)
            : '';
        if (url)
            candidates.push(url);
        if (data)
            candidates.push(data);
        if (base64)
            candidates.push(`base64,${base64}`);
    }
    if (isRecord(videoUrl)) {
        const url = typeof videoUrl.url === 'string' ? videoUrl.url : '';
        const data = typeof videoUrl.data === 'string' ? String(videoUrl.data) : '';
        const base64 = typeof videoUrl.base64 === 'string'
            ? String(videoUrl.base64)
            : '';
        if (url)
            candidates.push(url);
        if (data)
            candidates.push(data);
        if (base64)
            candidates.push(`base64,${base64}`);
    }
    const url = typeof part.url === 'string' ? String(part.url) : '';
    const uri = typeof part.uri === 'string' ? String(part.uri) : '';
    const data = typeof part.data === 'string' ? String(part.data) : '';
    const base64 = typeof part.base64 === 'string' ? String(part.base64) : '';
    if (url)
        candidates.push(url);
    if (uri)
        candidates.push(uri);
    if (data)
        candidates.push(data);
    if (base64)
        candidates.push(`base64,${base64}`);
    return candidates.some((value) => isInlineBase64(value));
}
function buildPlaceholderPart(kind) {
    return {
        type: 'text',
        text: kind === 'video' ? '[history_video_base64_omitted]' : '[history_image_base64_omitted]'
    };
}
/**
 * iFlow/Kimi multimodal compat:
 * - Preserve latest user turn media payload.
 * - Replace historical inline-base64 image/video parts with text placeholders.
 */
export function applyIflowKimiHistoryMediaPlaceholder(payload) {
    try {
        if (!isRecord(payload)) {
            return payload;
        }
        const root = structuredClone(payload);
        const model = typeof root.model === 'string' ? root.model.trim().toLowerCase() : '';
        if (model !== 'kimi-k2.5') {
            return root;
        }
        const messagesValue = root.messages;
        if (!Array.isArray(messagesValue) || !messagesValue.length) {
            return root;
        }
        const messages = messagesValue.map((item) => (isRecord(item) ? item : null));
        let latestUserIndex = -1;
        for (let index = messages.length - 1; index >= 0; index -= 1) {
            const message = messages[index];
            if (!message)
                continue;
            const role = typeof message.role === 'string' ? message.role.trim().toLowerCase() : '';
            if (role === 'user') {
                latestUserIndex = index;
                break;
            }
        }
        if (latestUserIndex <= 0) {
            return root;
        }
        const nextMessages = [];
        for (let index = 0; index < messages.length; index += 1) {
            const message = messages[index];
            if (!message)
                continue;
            if (index >= latestUserIndex) {
                nextMessages.push(message);
                continue;
            }
            const contentValue = message.content;
            if (!Array.isArray(contentValue)) {
                nextMessages.push(message);
                continue;
            }
            let changed = false;
            const nextContent = contentValue.map((part) => {
                if (!isRecord(part)) {
                    return part;
                }
                const mediaKind = detectMediaKind(part);
                if (!mediaKind) {
                    return part;
                }
                if (!mediaPartCarriesInlineBase64(part)) {
                    return part;
                }
                changed = true;
                return buildPlaceholderPart(mediaKind);
            });
            if (!changed) {
                nextMessages.push(message);
                continue;
            }
            nextMessages.push({ ...message, content: nextContent });
        }
        root.messages = nextMessages;
        return root;
    }
    catch {
        return payload;
    }
}
