var t,e=t=>{throw TypeError(t)},i=(t,i,s)=>i.has(t)||e("Cannot "+s),s=(t,e,s)=>(i(t,e,"read from private field"),s?s.call(t):e.get(t));function a(t){return null!==window.location.href.match(new RegExp(`^https://${t}/`))}function r(t){return a(`[0-9a-z.-]+.${t}`)}function n(){return r("spin.dev")||r("tunnel.shopifycloud.tech")||r("shop.dev")||a("arrive-server-staging(|-lqmn).shopifycloud.com")}function o(){return!n()&&(!!window.ShopifyPay&&window.ShopifyPay.Config.isProduction)}function u(t,e){if(!{}.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var c=0;function l(t){return"__private_"+c+++"_"+t}function h(t){return Object.entries(t).map((([t,e])=>({key:t,value:{stringValue:String(e)}})))}function m(t){if(Array.isArray(t))return{arrayValue:{values:t.map((t=>m(t)))}};switch(typeof t){case"boolean":return{boolValue:Boolean(t)};case"number":return{doubleValue:Number(t)};default:return{stringValue:String(t)}}}const p=function(t,e,i){const s=[0];for(let a=0;a{switch(e.attributes={...u(this,g)[g],...e.attributes},e.type){case"histogram":!function(t,e){var i;const{name:s,value:a,unit:r,timeUnixNano:n,attributes:o}=e,u=null!==(i=e.bounds)&&void 0!==i?i:p,c=new Array(u.length+1).fill(0);t[s]||(t[s]={name:s,unit:r||"1",histogram:{aggregationTemporality:1,dataPoints:[]}});for(let l=0;l0?m:0,bucketCounts:s>0?[1]:[]};t[i].exponentialHistogram.dataPoints.push({attributes:h(null!=n?n:{}),startTimeUnixNano:r,timeUnixNano:r,count:1,sum:u,scale:c,zeroCount:p,positive:g,negative:d,min:u,max:u,zeroThreshold:0})}(t,e);break;case"counter":!function(t,e){const{name:i,value:s,unit:a,timeUnixNano:r,attributes:n}=e;t[i]||(t[i]={name:i,unit:a||"1",sum:{aggregationTemporality:1,isMonotonic:!0,dataPoints:[]}}),t[i].sum.dataPoints.push({startTimeUnixNano:r,timeUnixNano:r,asDouble:s,attributes:h(null!=n?n:{})})}(t,e);break;case"gauge":!function(t,e){const{name:i,value:s,unit:a,timeUnixNano:r,attributes:n}=e;t[i]||(t[i]={name:i,unit:a||"1",gauge:{dataPoints:[]}}),t[i].gauge.dataPoints.push({startTimeUnixNano:r,timeUnixNano:r,asDouble:s,attributes:h(null!=n?n:{})})}(t,e)}}));const e=Object.values(t);0!==e.length&&(u(this,v)[v]=[],await u(this,d)[d].exportMetrics(e))}async exportLogs(){const t=u(this,b)[b].map((t=>{const e={timeUnixNano:t.timeUnixNano,observedTimeUnixNano:t.timeUnixNano,attributes:(i={...u(this,g)[g],...t.attributes},Object.entries(i).map((([t,e])=>({key:t,value:m(e)}))))};var i;return t.body&&(e.body={stringValue:t.body}),e}));0!==t.length&&(u(this,b)[b]=[],await u(this,d)[d].exportLogs(t))}}var f=l("url"),w=l("serviceName"),x=l("logger"),N=l("fetchFn");class P extends Error{constructor(t,e){super(t),this.metadata=void 0,this.name="OpenTelemetryClientError",this.metadata=e}}t=new WeakMap;class T{constructor(t,e){this.telemetry=t,this.metric=e,this.startTime=window.performance.now()}end(t){const e=window.performance.now();this.telemetry.histogram({...this.metric,...t,value:e-this.startTime})}}const U=new class{constructor(t,e,i){Object.defineProperty(this,f,{writable:!0,value:void 0}),Object.defineProperty(this,w,{writable:!0,value:void 0}),Object.defineProperty(this,x,{writable:!0,value:void 0}),Object.defineProperty(this,N,{writable:!0,value:void 0}),u(this,f)[f]=t.replace(/\/v1\/(logs|metrics|traces)\/?$/,""),u(this,w)[w]=e,u(this,x)[x]=null==i?void 0:i.logger,u(this,N)[N]=null==i?void 0:i.fetchFn}async exportMetrics(t){const e={resourceMetrics:[{resource:{attributes:[{key:"service.name",value:{stringValue:u(this,w)[w]}}]},scopeMetrics:[{scope:{name:"open-telemetry-mini-client",version:"1.1.0",attributes:[]},metrics:t}]}]};await this.exportTo(e,"/v1/metrics")}async exportLogs(t){const e={resourceLogs:[{resource:{attributes:[{key:"service.name",value:{stringValue:u(this,w)[w]}}]},scopeLogs:[{scope:{name:"open-telemetry-mini-client",version:"1.1.0",attributes:[]},logRecords:t}]}]};await this.exportTo(e,"/v1/logs")}async exportTo(t,e){var i;const s=await this.exporterFetch()(`${u(this,f)[f]}${e}`,{method:"POST",keepalive:!0,headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});if(null===(i=u(this,x)[x])||void 0===i||i.log({status:s.status}),!s.ok){if(400===s.status){const t=await s.text();throw new P(`Invalid OpenTelemetry Data: ${t}`)}if(429===s.status||503===s.status){const e=await s.text(),i=s.headers.get("Retry-After"),a=i?{seconds:Number(i)}:void 0;throw new P("Server did not accept data",{errorData:e,retryAfter:a,body:t})}throw new P(`Server responded with ${s.status}`)}}exporterFetch(){return u(this,N)[N]||fetch}}("https://otlp-http-production.shopifysvc.com/v1/metrics","shop-server-js"),O={exportMetrics:async t=>(console.log(" [Log: metrics]: ",JSON.stringify(t)),Promise.resolve()),exportLogs:async t=>(console.log(" [Log: logs]: ",JSON.stringify(t)),Promise.resolve())},M=n()?O:new class{constructor(s){var a,r,n;a=this,(r=t).has(a)?e("Cannot add the same private member more than once"):r instanceof WeakSet?r.add(a):r.set(a,n),((t,e,s,a)=>{i(t,e,"write to private field"),a?a.call(t,s):e.set(t,s)})(this,t,s)}async exportMetrics(e){await this.withRetry((()=>s(this,t).exportMetrics(e)),(()=>this.exportMetrics(e)))}async exportLogs(e){await this.withRetry((()=>s(this,t).exportLogs(e)),(()=>this.exportLogs(e)))}async withRetry(t,e){var i;try{await t()}catch(s){if(s instanceof P){const t=null==(i=s.metadata)?void 0:i.retryAfter;if(t)return void(await new Promise((i=>{setTimeout((()=>{e().finally(i)}),1e3*t.seconds)})))}throw s}}}(U),k=new class extends y{counter(t){super.counter(t),this.exportMetrics()}histogram(t){super.histogram(t),this.exportMetrics()}log(...t){super.log(...t),this.exportLogs()}timing(t){return new T(this,t)}}({exporter:M});export{n as a,o as i,k as o};