class e{isAvailable(){return"undefined"!=typeof window&&("function"==typeof window.gtag||void 0!==window.dataLayer)}trackEvent(e,t){if(this.isAvailable())try{const n=function(e,t=80){if(!e||0===e.trim().length)return"";let n=e.trim();return n=n.replace(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g,"[EMAIL]"),n=n.replace(/\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/g,"[PHONE]"),n=n.replace(/\b\(\d{3}\)\s*\d{3}[-.]?\d{4}\b/g,"[PHONE]"),n=n.replace(/\+\d{1,3}[-\s.]?\d{1,4}[-\s.]?\d{1,4}[-\s.]?\d{1,9}/g,"[PHONE]"),n=n.replace(/\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b/g,"[CARD]"),n=n.replace(/\b\d{3}-\d{2}-\d{4}\b/g,"[SSN]"),n=n.replace(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g,"[IP]"),n=n.replace(/https?:\/\/[^\s]+/g,"[URL]"),n.length>t&&(n=n.substring(0,t-3)+"..."),n}(t.questionText||"");"function"==typeof window.gtag?window.gtag("event",e,Object.assign({conversation_id:t.conversationId,feedback_type:t.feedback,agent_id:t.agentId,question_text:n,answer_id:t.answerId||"",timestamp:t.timestamp,environment:t.env},this.getCustomParams(t))):window.dataLayer&&window.dataLayer.push(Object.assign({event:e,conversation_id:t.conversationId,feedback_type:t.feedback,agent_id:t.agentId,question_text:n,answer_id:t.answerId||"",timestamp:t.timestamp,environment:t.env},this.getCustomParams(t)))}catch(e){console.error("Error sending analytics event:",e)}else console.warn("Google Analytics is not available. Event not sent:",e)}getCustomParams(e){const t=["conversationId","feedback","agentId","projectId","questionText","answerId","answerText","timestamp","env"],n={};return Object.keys(e).forEach((s=>{t.includes(s)||(n[s]=e[s])})),n}}class t{isAvailable(){return"undefined"!=typeof console}trackEvent(e,t){console.log(`[Analytics Debug] Event: ${e}`,t)}}class n{constructor(){this.providers=[],this.feedbackCache=new Set,this.providers.push(new e),"undefined"!=typeof window&&"localhost"===window.location.hostname&&this.providers.push(new t)}static getInstance(){return n.instance||(n.instance=new n),n.instance}registerProvider(e){this.providers.push(e)}trackFeedback(e){if(!e.conversationId)return void console.warn("[Analytics] Cannot send feedback event: conversationId is required");if(!e.feedback||!["up","down"].includes(e.feedback))return void console.warn("[Analytics] Cannot send feedback event: feedback must be 'up' or 'down'");const t=this.generateIdempotencyKey(e);this.feedbackCache.has(t)?console.log("[Analytics] Duplicate feedback event prevented:",t):(this.feedbackCache.add(t),this.providers.forEach((t=>{if(t.isAvailable())try{t.trackEvent("ai12z_feedback",e)}catch(e){console.error("Error in analytics provider:",e)}})),setTimeout((()=>{this.feedbackCache.delete(t)}),3e5))}changeFeedback(e,t){const n=this.generateIdempotencyKey(e);this.feedbackCache.delete(n),this.trackFeedback(t)}generateIdempotencyKey(e){return`${e.conversationId}_${e.answerId||e.timestamp}_${e.feedback}`}clearCache(){this.feedbackCache.clear()}}function s(e){switch(e){case"local":return"local";case"dev":return"dev";case"staging":return"staging";default:return"prod"}}function i(e,t){if(!e||0===e.length)return"";for(let n=e.length-1;n>=0;n--){const s=e[n];if(s.id===t||s.insightId===t)for(let t=n-1;t>=0;t--)if("user"===e[t].type)return e[t].message||""}for(let t=e.length-1;t>=0;t--)if("user"===e[t].type)return e[t].message||"";return""}function o(e,t,i,o){n.getInstance().trackFeedback({conversationId:i.conversationId||o||t||"unknown",feedback:e,agentId:i.projectId||"unknown",questionText:i.questionText||"",answerId:t||"unknown",timestamp:(new Date).toISOString(),env:s(i.dataMode)})}export{i as g,o as t}