"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatMediaChat = exports.MTMD_DEFAULT_MEDIA_MARKER = void 0;
exports.MTMD_DEFAULT_MEDIA_MARKER = '<__media__>';
const formatMediaChat = (messages) => {
    if (!messages)
        return {
            messages,
            has_media: false,
        };
    const mediaPaths = [];
    return {
        messages: messages.map((msg) => {
            if (Array.isArray(msg.content)) {
                const content = msg.content.map((part) => {
                    var _a;
                    // Handle multimodal content
                    if (part.type === 'image_url') {
                        let path = ((_a = part.image_url) === null || _a === void 0 ? void 0 : _a.url) || '';
                        mediaPaths.push(path);
                        return {
                            type: 'text',
                            text: exports.MTMD_DEFAULT_MEDIA_MARKER,
                        };
                    }
                    else if (part.type === 'input_audio') {
                        const { input_audio: audio } = part;
                        if (!audio)
                            throw new Error('input_audio is required');
                        const { format } = audio;
                        if (format != 'wav' && format != 'mp3') {
                            throw new Error(`Unsupported audio format: ${format}`);
                        }
                        if (audio.url) {
                            const path = audio.url.replace(/file:\/\//, '');
                            mediaPaths.push(path);
                        }
                        else if (audio.data) {
                            mediaPaths.push(audio.data);
                        }
                        return {
                            type: 'text',
                            text: exports.MTMD_DEFAULT_MEDIA_MARKER,
                        };
                    }
                    return part;
                });
                return Object.assign(Object.assign({}, msg), { content });
            }
            return msg;
        }),
        has_media: mediaPaths.length > 0,
        media_paths: mediaPaths,
    };
};
exports.formatMediaChat = formatMediaChat;
