"use strict";function r(e){return null===e?"":Array.isArray(e)?e.map(r):"object"==typeof e&&null!==e?Object.fromEntries(Object.entries(e).map(function(e){return[e[0],r(e[1])]})):e}Object.defineProperty(exports,"__esModule",{value:!0});exports.extractAndParseResponse=function(e){var t=e.match(/```json\s*(\{[\s\S]*?\})\s*```/);if(!t||!t[1])return console.error("Could not find JSON block in the response."),null;var n=t[1];try{return r(JSON.parse(n))}catch(r){return console.error("Error parsing JSON:",r),null}},exports.extractAndParseWithProperty=function(e,t){void 0===t&&(t="updated_fields");var n=new RegExp('```json\\s+property="'+t+'"\\s*([\\s\\S]*?)\\s*```'),a=e.match(n);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,e){var t=e.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}},exports.extractTextFromChildren=function r(e){return"string"==typeof e?e:Array.isArray(e)?e.map(r).join(""):"object"==typeof e&&null!==e&&"props"in e?r(e.props.children):""},exports.extractVariantIds=function(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 e=/<!--\s*```json\s*([\s\S]*?)\s*```\s*-->/g,t=void 0;null!==(t=e.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 n=/```json\s*([\s\S]*?)\s*```/g,a=void 0;null!==(a=n.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}},exports.parseValidationData=function(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 e=r.replace(/```json|```/g,"").trim();return JSON.parse(e)}catch(r){return console.error("Critical: Could not parse Validation Agent response",r),null}}},exports.useHeaderObserver=function(r){var e=r.handlers,t=r.setResult;return{processHeaderMessage:function(r){var n,a=null===(n=null==r?void 0:r.header_message)||void 0===n?void 0:n.trim(),i=null==r?void 0:r.responsedata;a&&e&&t&&e[a]&&e[a](i,t)}}};
//# sourceMappingURL=utils.js.map
