function r(n){return null===n?"":Array.isArray(n)?n.map(r):"object"==typeof n&&null!==n?Object.fromEntries(Object.entries(n).map(function(n){return[n[0],r(n[1])]})):n}function n(r){if("object"==typeof r&&null!==r)return r;if("string"!=typeof r)return null;try{return JSON.parse(r.trim())}catch(t){try{var n=r.replace(/```json|```/g,"").trim();return JSON.parse(n)}catch(r){return console.error("Critical: Could not parse Validation Agent response",r),null}}}function t(n,t){void 0===t&&(t="updated_fields");var e=new RegExp('```json\\s+property="'+t+'"\\s*([\\s\\S]*?)\\s*```'),a=n.match(e);if(!a||!a[1])return console.error('Could not find JSON block with property="'.concat(t,'"')),null;var i=a[1].trim();try{var o=i.replace(/\u00A0/g," ");return o=o.replace(/<ai_reasoning>([\s\S]*?)<\/ai_reasoning>/g,function(r,n){var t=n.replace(/(?<!\\)"/g,'\\"');return t=(t=t.replace(/\n/g,"\\n").replace(/\r/g,"\\r")).replace(/\t/g,"\\t"),"<ai_reasoning>".concat(t,"</ai_reasoning>")}),r(JSON.parse(o))}catch(r){return console.error("Final Parse Attempt Failed for ".concat(t,":"),r),console.log("String that failed:",i),null}}function e(n){var t=n.match(/```json\s*(\{[\s\S]*?\})\s*```/);if(!t||!t[1])return console.error("Could not find JSON block in the response."),null;var e=t[1];try{return r(JSON.parse(e))}catch(r){return console.error("Error parsing JSON:",r),null}}function a(r){try{if((null==r?void 0:r.variant_ids)&&Array.isArray(r.variant_ids))return r.variant_ids;if("string"==typeof r){for(var n=/<!--\s*```json\s*([\s\S]*?)\s*```\s*-->/g,t=void 0;null!==(t=n.exec(r));)try{if((null==(i=JSON.parse(t[1].trim()))?void 0:i.variant_ids)&&Array.isArray(i.variant_ids))return i.variant_ids}catch(r){continue}for(var e=/```json\s*([\s\S]*?)\s*```/g,a=void 0;null!==(a=e.exec(r));)try{if((null==(i=JSON.parse(a[1].trim()))?void 0:i.variant_ids)&&Array.isArray(i.variant_ids))return i.variant_ids}catch(r){continue}try{var i;if((null==(i=JSON.parse(r))?void 0:i.variant_ids)&&Array.isArray(i.variant_ids))return i.variant_ids}catch(r){}}return null}catch(r){return console.error("Error extracting variant_ids:",r),null}}var i=function(r){var n=r.handlers,t=r.setResult;return{processHeaderMessage:function(r){var e,a=null===(e=null==r?void 0:r.header_message)||void 0===e?void 0:e.trim(),i=null==r?void 0:r.responsedata;a&&n&&t&&n[a]&&n[a](i,t)}}};function o(r){return"string"==typeof r?r:Array.isArray(r)?r.map(o).join(""):"object"==typeof r&&null!==r&&"props"in r?o(r.props.children):""}export{e as extractAndParseResponse,t as extractAndParseWithProperty,o as extractTextFromChildren,a as extractVariantIds,n as parseValidationData,i as useHeaderObserver};
//# sourceMappingURL=utils.js.map
