import{l as O}from"./chunk-E24W34C4.js";import{d as E}from"./chunk-ELIH6GCC.js";import{Pb as i,Qb as m,Z as b,da as N}from"./chunk-EYDNSYYE.js";import{g as u}from"./chunk-VKWPEFWQ.js";u();u();var l=class{constructor({export:e,input:r,inputBytes:o,output:n,outputBytes:s,logs:a,functionId:f,fuelConsumed:c,target:A,errorMessage:S,errorType:w,inputQueryVariablesMetafieldValue:g,inputQueryVariablesMetafieldNamespace:I,inputQueryVariablesMetafieldKey:C}){this.export=e,this.input=r,this.inputBytes=o,this.output=n,this.outputBytes=s,this.logs=a,this.functionId=f,this.fuelConsumed=c,this.target=A,this.errorMessage=S,this.errorType=w,this.inputQueryVariablesMetafieldValue=g,this.inputQueryVariablesMetafieldNamespace=I,this.inputQueryVariablesMetafieldKey=C}},h=class{constructor({cacheEntryEpochMs:e,cacheTtlMs:r,httpRequest:o,httpResponse:n}){this.cacheEntryEpochMs=e,this.cacheTtlMs=r,this.httpRequest=o,this.httpResponse=n}},_;(function(t){t[t.NoCachedResponse=0]="NoCachedResponse",t[t.CacheAboutToExpire=1]="CacheAboutToExpire",t[t.Unknown=2]="Unknown"})(_||(_={}));var d=class{constructor({reason:e,httpRequest:r}){this.reason=e,this.httpRequest=r}},y=class{constructor({attempt:e,connectTimeMs:r,writeReadTimeMs:o,httpRequest:n,httpResponse:s,error:a}){this.attempt=e,this.connectTimeMs=r,this.writeReadTimeMs=o,this.httpRequest=n,this.httpResponse=s,this.error=a}};u();function R(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)&&Object.getPrototypeOf(t)===Object.prototype}function M(t,e){return e?.deep&&R(t)||e?.deep&&Array.isArray(t)?p(t,e):t}function p(t,e){if(Array.isArray(t))return t.map(r=>e?.deep?p(r,e):r);if(R(t)){let r={};for(let[o,n]of Object.entries(t))r[b(o)]=M(n,e);return r}return t}var L=450,P=5*1e3,T=60*1e3,H=5,W=1e6,U="function_run",z="function_network_access",V="function_network_access.response_from_cache",q="function_network_access.request_execution_in_background",J="function_network_access.request_execution",Z="no_cached_response",ee="cached_response_about_to_expire";function te(t){let e=JSON.parse(t),r=e.input_query_variables_metafield_value&&x(e.input_query_variables_metafield_value);return new l({export:e.export,input:e.input,inputBytes:e.input_bytes,output:e.output,outputBytes:e.output_bytes,logs:e.logs,functionId:e.function_id,fuelConsumed:e.fuel_consumed,target:e.target,errorMessage:e.error_message,errorType:e.error_type,inputQueryVariablesMetafieldValue:r,inputQueryVariablesMetafieldNamespace:e.input_query_variables_metafield_namespace,inputQueryVariablesMetafieldKey:e.input_query_variables_metafield_key})}function re(t){let e=JSON.parse(t);return new h({cacheEntryEpochMs:e.cache_entry_epoch_ms,cacheTtlMs:e.cache_ttl_ms,httpRequest:e.http_request,httpResponse:e.http_response})}var k={no_cached_response:_.NoCachedResponse,cached_response_about_to_expire:_.CacheAboutToExpire};function se(t){let e=JSON.parse(t);return new d({reason:k[e.reason]??_.Unknown,httpRequest:e.http_request})}function oe(t){let e=JSON.parse(t);return new y({attempt:e.attempt,connectTimeMs:e.connect_time_ms??null,writeReadTimeMs:e.write_read_time_ms??null,httpRequest:e.http_request,httpResponse:e.http_response??null,error:e.error??null})}var ne=async t=>{let{errors:e}=t.response,r=L,o=null,n="not_attempted";if(e.length>0)if(i(`Errors: ${e.map(s=>s.message).join(", ")}`),e.some(s=>s.status===401))try{o=await t.onResubscribe(),n="succeeded"}catch(s){i(`Failed to resubscribe to app logs: ${s}`),r=T,n="failed",t.onThrottle(r)}else e.some(s=>s.status===429)?(r=T,t.onThrottle(r)):(r=P,t.onUnknownError(r));return{retryIntervalMs:r,nextJwtToken:o,resubscribeResult:n}};function ae(t){return t.allExtensions.flatMap(e=>e.isFunctionExtension?[`extensions.${e.configuration.handle}`]:[])}var ie=({appLog:t,appLogPayload:e,storeName:r,prettyPrint:o=!0})=>{let{cursor:n,...s}=t,a=p({...s,payload:e,localTime:N(t.log_timestamp),storeName:r});return e instanceof l&&(a.payload.logs=e.logs.split(`
`).filter(Boolean),a.payload.inputQueryVariablesMetafieldValue&&(a.payload.inputQueryVariablesMetafieldValue=x(a.payload.inputQueryVariablesMetafieldValue))),o?JSON.stringify(a,null,2):JSON.stringify(a)},pe=(t,e)=>{let r=p(JSON.parse(t));return e===U?new l(r):e===V?new h(r):e===q?new d(r):e===J?new y(r):r},ce=async(t,e,r,o)=>{let n=await t.subscribeToAppLogs(e,r);if(!n.appLogsSubscribe)throw new E("Failed to subscribe to app logs: No response received");let{jwtToken:s,success:a,errors:f}=n.appLogsSubscribe;if(i(`Token: ${s}
`),i(`API Key: ${e.apiKey}
`),f&&f.length>0){let c=f.join(", ");throw await O({stripAnsi:!1},()=>{m(`Errors subscribing to app logs: ${c}`,o),m("App log streaming is not available in this session.",o)}),new E(c)}else{if(!s)throw new E("Failed to subscribe to app logs: No response received");let c=Array.isArray(e.shopIds)?e.shopIds:[e.shopIds];i(`Subscribed to App Events for shop ID(s) ${c.join(", ")}`),a!==void 0&&i(`Success: ${a}
`)}return s};function ue(t){try{if(typeof t=="string"){let e=JSON.parse(t);return JSON.stringify(e,null,2)}else return typeof t=="object"&&t!==null?JSON.stringify(t,null,2):void 0}catch(e){throw new Error(`Error parsing JSON: ${e}`)}}var x=t=>{try{return JSON.parse(t)}catch{return t}};export{l as a,h as b,_ as c,d,y as e,p as f,L as g,P as h,H as i,W as j,U as k,z as l,V as m,q as n,J as o,Z as p,ee as q,te as r,re as s,se as t,oe as u,ne as v,ae as w,ie as x,pe as y,ce as z,ue as A};
