window.aui.amd_define("@amzn/flow-metrics-web-lib-latency",function(){"use strict";function S(e,t){return`${e}+${t}`}class h extends Error{constructor(t){super(t),this.name="MetricsRuleError"}}var m=(e=>(e.aboveTheFold="aboveTheFold",e.functional="functional",e.firstContentfulPaint="firstContentfulPaint",e))(m||{}),N=(e=>(e.EXISTS="EXISTS",e.MAX="MAX",e.MIN="MIN",e.SUM="SUM",e.UNKNOWN="UNKNOWN",e))(N||{});const C={aboveTheFold:"af",functional:"fn",firstContentfulPaint:"fcp_ovr"};function T(e,t,a="ERROR",u){let o;t instanceof Error?o=t:o={message:t?String(t):""},o.message.length===0?o.message=e:o.message=`${e} - ${o.message}`;const i={logLevel:a,attribution:"FlowMetricsWebLib",message:""};typeof window<"u"&&window.ueLogError?window.ueLogError(o,i):console.error(`CSM ueLogError not found: ${o} , ${JSON.stringify(i)}`)}const b=(e,t,a)=>{T(e,t,"ERROR")},I=(e,t)=>{typeof window<"u"&&window.uet?window.uet(C[e],void 0,void 0,t):console.error("CSM uet not found!")},O=()=>{if(typeof window>"u"){console.error("window is not initialized");return}if(window.ue===void 0){console.error("CSM ue not found");return}if(window.ue.isl){if(window.uex===void 0){console.error("CSM uex not found");return}window.uex("at")}};var p=(e=>(e.NodeEventDataNotFound="M003",e.MetricsCalculatorError="M004",e.MetricInstructionParsingError="M005",e))(p||{});function $(e,t){const a=function(r){const i=S(r.id,r.event);return e.get(i)},u=function(r){const i=(()=>{switch(r.operator){case N.MAX:return Math.max;case N.MIN:return Math.min;case N.SUM:return(f,l)=>f+l;default:throw new h(`Calculation error: Unknown operator : ${r.operator}!`)}})();return r.metricRules.map(f=>{const{combination:l,nodeEvent:d}=f;if(l)return u(l);if(d)return a(d);throw new h(`Calculation error: Unknown rule type : ${Object.keys(f)}!`)}).reduce((f,l)=>i(f,l))};return Object.freeze({calculateMetrics:function(){const r={};try{t.forEach(i=>{const{metricName:f,metricRule:{combination:l,nodeEvent:d}}=i;let w=0;if(l)w=u(l);else if(d)w=a(d);else throw new h(`Calculation error: Unknown rule type : ${Object.keys(i.metricRule)}!`);r[f]=w})}catch(i){b(p.MetricsCalculatorError,i)}return r}})}function F(e,t){const u=$(e,t).calculateMetrics(),o=u[m.aboveTheFold],r=u[m.functional],i=u[m.firstContentfulPaint];o&&r?(I(m.aboveTheFold,o),I(m.functional,r)):console.error("sendExperienceRenderMetrics: missing aboveTheFold or functional timestamp!"),i?I(m.firstContentfulPaint,i):console.error("sendExperienceRenderMetrics: using the CSM default fcp!"),O()}function U(){let e=[];const t=new Set,a=new Map,u=new Map,o=new Map,r=new Map,i=function(n,s,c){console.log("registerInstrumentationEvents",JSON.stringify({nodeId:n,payload:s,timestamp:c})),c=c||Date.now();for(const[g,M]of Object.entries(s)){const v=S(n,g);if(r.set(v,{lastTimestamp:c,count:M.length,instrumentationEvents:new Set}),M.length===0)console.log("No need to record instrumentation events.",v,c),d(v,c);else for(const E of M)u.set(E,v),l(E)}},f=function(n,s){console.log("recordInstrumentationEvent",JSON.stringify({instrumentationId:n,timestampOverride:s})),o.set(n,s||Date.now()),l(n)},l=function(n){const s=o.get(n),c=u.get(n);if(!s||!c)return;const g=r.get(c);if(!g){b(p.NodeEventDataNotFound);return}const{lastTimestamp:M,count:v,instrumentationEvents:E}=g,y=Math.max(M,s);E.add(n),r.set(c,{lastTimestamp:y,count:v,instrumentationEvents:E}),v===E.size&&(console.log("All instrumentation events recorded",c,y),d(c,y))},d=function(n,s){t.has(n)?a.set(n,s):console.error(`The ${n} is not registered!`),z()&&(console.log("All required node events recorded!"),F(a,e))},w=function(n){console.log("updateMetricInstructions",JSON.stringify(n)),e=[...e,...n],P(e)},k=function(n,s){t.add(S(n,s))},R=function(n){const{combination:s,nodeEvent:c}=n;if(s)s.metricRules.forEach(g=>{R(g)});else if(c)k(c.id,c.event);else throw new h(`Unknown rule type : ${Object.keys(n)}!`)},P=function(n){try{n.forEach(s=>{R(s.metricRule)})}catch(s){t.clear(),b(p.MetricInstructionParsingError,s)}},z=function(){return t.size===a.size};return Object.freeze({registerInstrumentationEvents:i,recordInstrumentationEvent:f,updateMetricInstructions:w,reset:function(){u.clear(),r.clear(),a.clear(),t.clear(),e=[]},get instrumentation2NodeEvent(){return u},get nodeEventsData(){return r},get nodeEvents(){return a},get registerNodeEvents(){return t}})}const x=U();function L(){const e=window==null?void 0:window.fm;if(!e)return;const{registerInstrumentationEvents:t,recordInstrumentationEvent:a,updateMetricInstructions:u}=x;e.registerInstrumentationEvents=t,e.recordInstrumentationEvent=a,u(e.i),e._calls&&(e._calls.registerInstrumentationEvents.forEach(o=>t(...o)),e._calls.recordInstrumentationEvent.forEach(o=>a(...o))),delete e._calls}L()});
