import { normalJsonParser } from "../service-caller/json.mjs";
import { createLocateResultAdapter } from "../shared/model-locate-result/factory.mjs";
import { defaultExtractContentAndReasoning } from "./chat-content.mjs";
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;
}
const defaultReplanningCycleLimit = 20;
const defaultImageDetail = (_input)=>void 0;
const defaultChatCompletionParams = ({ midsceneDefaults, userConfig })=>({
        config: {
            temperature: userConfig.temperature ?? midsceneDefaults.temperature
        }
    });
const midsceneChatCompletionDefaults = {
    temperature: 0
};
const defaultLocateResultAdapterDefinition = {
    coordinates: {
        shape: 'bbox',
        order: 'xy',
        normalizedBy: 1000
    }
};
function resolveJsonParser(jsonParser) {
    if (!jsonParser || 'lenient-json' === jsonParser) return normalJsonParser;
    if ('function' == typeof jsonParser) return jsonParser;
    throw new Error(`Unknown json parser preset: ${jsonParser}`);
}
function resolveChatCompletion(chatCompletion) {
    const buildChatCompletionParams = chatCompletion?.buildChatCompletionParams ?? defaultChatCompletionParams;
    const resolveImageDetail = chatCompletion?.resolveImageDetail ?? defaultImageDetail;
    const unsupportedUserConfig = chatCompletion?.unsupportedUserConfig ?? [];
    const extractContentAndReasoning = chatCompletion?.extractContentAndReasoning ?? defaultExtractContentAndReasoning;
    return {
        unsupportedUserConfig,
        buildChatCompletionParams: (input)=>{
            const context = {
                ...input,
                userConfig: input.userConfig ?? {},
                midsceneDefaults: midsceneChatCompletionDefaults
            };
            return buildChatCompletionParams(context);
        },
        resolveImageDetail: (input)=>resolveImageDetail({
                ...input,
                userConfig: input.userConfig ?? {},
                midsceneDefaults: midsceneChatCompletionDefaults
            }),
        extractContentAndReasoning
    };
}
function resolveImagePreprocess(imagePreprocess) {
    return {
        padBlockSize: imagePreprocess?.padBlockSize
    };
}
function resolvePlanning(planning) {
    if (planning?.kind === 'custom') return {
        kind: 'custom',
        cacheEnabled: planning.cacheEnabled ?? true,
        defaultReplanningCycleLimit: planning.defaultReplanningCycleLimit ?? defaultReplanningCycleLimit,
        supportsActionDeepLocate: planning.supportsActionDeepLocate ?? false,
        planFn: planning.planFn
    };
    return {
        kind: 'standard',
        cacheEnabled: planning?.cacheEnabled ?? true,
        defaultReplanningCycleLimit: planning?.defaultReplanningCycleLimit ?? defaultReplanningCycleLimit,
        supportsActionDeepLocate: planning?.supportsActionDeepLocate ?? true
    };
}
function resolveLocate(locate) {
    if (locate?.kind === 'custom') return {
        kind: 'custom',
        supportsSearchArea: locate.supportsSearchArea ?? false,
        locateFn: locate.locateFn
    };
    return {
        kind: 'standard',
        supportsSearchArea: locate?.supportsSearchArea ?? true,
        resultAdapter: createLocateResultAdapter(locate?.resultAdapter ?? defaultLocateResultAdapterDefinition)
    };
}
class ResolvedModelAdapter {
    constructor(config, modelFamily){
        _define_property(this, "jsonParser", void 0);
        _define_property(this, "chatCompletion", void 0);
        _define_property(this, "imagePreprocess", void 0);
        _define_property(this, "planning", void 0);
        _define_property(this, "locate", void 0);
        this.jsonParser = resolveJsonParser(config.jsonParser);
        this.chatCompletion = resolveChatCompletion(config.chatCompletion);
        this.imagePreprocess = resolveImagePreprocess(config.imagePreprocess);
        this.planning = resolvePlanning(config.planning);
        this.locate = resolveLocate(config.locate);
    }
}
export { ResolvedModelAdapter };

//# sourceMappingURL=resolved.mjs.map