function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
import { mapOpenAIMessagesToMessageBubbleData } from "./mapOpenAIMessages";
import { normalizeOllamaMessagesToOpenAI } from "./normalizeOllamaMessages";
function defaultOllamaGetMessageId(msg, index, originals) {
    var _ref, _ref1;
    var _originals_index;
    return (_ref = (_ref1 = (_originals_index = originals[index]) === null || _originals_index === void 0 ? void 0 : _originals_index.id) !== null && _ref1 !== void 0 ? _ref1 : msg.id) !== null && _ref !== void 0 ? _ref : "ollama-msg-".concat(index);
}
/**
 * 将 Ollama `/api/chat` 的 `messages` 转为 `MessageBubbleData[]`
 */ export function mapOllamaMessagesToMessageBubbleData(messages, options, mapMessage) {
    var _ref = options !== null && options !== void 0 ? options : {}, _ref_appendThinkingToContent = _ref.appendThinkingToContent, appendThinkingToContent = _ref_appendThinkingToContent === void 0 ? true : _ref_appendThinkingToContent, _ref_appendImagesPlaceholder = _ref.appendImagesPlaceholder, appendImagesPlaceholder = _ref_appendImagesPlaceholder === void 0 ? true : _ref_appendImagesPlaceholder, _ref_preserveOllamaRawInExtra = _ref.preserveOllamaRawInExtra, preserveOllamaRawInExtra = _ref_preserveOllamaRawInExtra === void 0 ? true : _ref_preserveOllamaRawInExtra, userGetMessageId = _ref.getMessageId, rest = _object_without_properties(_ref, [
        "appendThinkingToContent",
        "appendImagesPlaceholder",
        "preserveOllamaRawInExtra",
        "getMessageId"
    ]);
    var openai = normalizeOllamaMessagesToOpenAI(messages, {
        appendThinkingToContent: appendThinkingToContent,
        appendImagesPlaceholder: appendImagesPlaceholder
    });
    var getMessageId = userGetMessageId !== null && userGetMessageId !== void 0 ? userGetMessageId : function(msg, index) {
        return defaultOllamaGetMessageId(msg, index, messages);
    };
    if (!preserveOllamaRawInExtra && !mapMessage) {
        return mapOpenAIMessagesToMessageBubbleData(openai, _object_spread_props(_object_spread({}, rest), {
            getMessageId: getMessageId
        }));
    }
    var innerMap = function innerMap(msg, index, draft) {
        var d = draft;
        if (preserveOllamaRawInExtra && messages[index]) {
            d = _object_spread_props(_object_spread({}, d), {
                extra: _object_spread_props(_object_spread({}, d.extra), {
                    ollama: {
                        raw: messages[index]
                    }
                })
            });
        }
        if (mapMessage) {
            d = mapMessage(msg, index, d);
        }
        return d;
    };
    return mapOpenAIMessagesToMessageBubbleData(openai, _object_spread_props(_object_spread({}, rest), {
        getMessageId: getMessageId
    }), innerMap);
}
