"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    extractJSONFromCodeBlock: ()=>extractJSONFromCodeBlock,
    normalJsonParser: ()=>normalJsonParser,
    safeParseJson: ()=>safeParseJson
});
const external_jsonrepair_namespaceObject = require("jsonrepair");
function extractJSONFromCodeBlock(response) {
    try {
        const jsonMatch = response.match(/^\s*(\{[\s\S]*\})\s*$/);
        if (jsonMatch) return jsonMatch[1];
        const codeBlockMatch = response.match(/```(?:json)?\s*(\{[\s\S]*?\})\s*```/);
        if (codeBlockMatch) return codeBlockMatch[1];
        const jsonLikeMatch = response.match(/\{[\s\S]*\}/);
        if (jsonLikeMatch) return jsonLikeMatch[0];
    } catch  {}
    return response;
}
function normalizeJsonObject(obj, context = {}) {
    if (null == obj) return obj;
    if (Array.isArray(obj)) return obj.map((item)=>normalizeJsonObject(item, context));
    if ('object' == typeof obj) {
        const normalized = {};
        for (const [key, value] of Object.entries(obj)){
            const trimmedKey = key.trim();
            const preserveStringValue = context.preserveStringValueKeys?.includes(trimmedKey) ?? false;
            const normalizedValue = 'string' == typeof value ? preserveStringValue ? value : value.trim() : normalizeJsonObject(value, context);
            normalized[trimmedKey] = normalizedValue;
        }
        return normalized;
    }
    if ('string' == typeof obj) return obj.trim();
    return obj;
}
const parseNormalJson = (input, rawResponse, context)=>{
    if (input?.match(/\((\d+),(\d+)\)/)) return input.match(/\((\d+),(\d+)\)/)?.slice(1).map(Number);
    let parsed;
    let lastError;
    try {
        parsed = JSON.parse(input);
        return normalizeJsonObject(parsed, context);
    } catch (error) {
        lastError = error;
    }
    try {
        parsed = JSON.parse((0, external_jsonrepair_namespaceObject.jsonrepair)(input));
        return normalizeJsonObject(parsed, context);
    } catch (error) {
        lastError = error;
    }
    return {
        parsed: void 0,
        lastError,
        rawResponse
    };
};
function safeParseJson(raw, context) {
    const cleanJsonString = extractJSONFromCodeBlock(raw);
    const result = parseNormalJson(cleanJsonString, raw, context);
    if (result && 'object' == typeof result && 'parsed' in result && void 0 === result.parsed) throw Error(`failed to parse LLM response into JSON. Error - ${String(result.lastError ?? 'unknown error')}. Response - \n ${raw}`);
    return result;
}
const normalJsonParser = safeParseJson;
exports.extractJSONFromCodeBlock = __webpack_exports__.extractJSONFromCodeBlock;
exports.normalJsonParser = __webpack_exports__.normalJsonParser;
exports.safeParseJson = __webpack_exports__.safeParseJson;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "extractJSONFromCodeBlock",
    "normalJsonParser",
    "safeParseJson"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=json.js.map