import{Qg as e,bD as t,la as s,yO as o}from"./6ec9a0fa.rdcQt2v8.js";import{_ as n,v as r}from"./41038fc5.BSZHhowl.js";var i=class{#e;#t;#s;#o;#n;#r;constructor({context:s,serializedReceipt:o,activatedExtensions:n,isCheckoutEditor:i,extensions:c,metafields:p,afterHydrate:h}){const{client:d,shop:l,observability:f}=s;this.#o=s,this.#t=c,this.#s=p,this.#n=o,this.#e=new e,this.syncPermissions(c.value),this.sandboxCache=r(d,{observability:f,appSandboxPermissions:this.#e,deferThankYouPreloads:!i,useUpgradedRemoteDomRuntime:l.hasFlagEnabled(t)}),this.storefrontTokens=new a({activatedExtensions:n,globalExtensions:l.globalUIExtensions,context:s}),h.then(()=>this.#i()).catch(()=>{})}registerExtensionsManager(e){this.#r=e}syncPermissions(e){for(const t of e)t.appId&&this.#e.app(t.appId).syncExtension(t)}removePermissions(e){for(const t of e)t.appId&&this.#e.app(t.appId).removeExtension(t.id)}destroyExtensions(e,t){e.forEach(e=>{this.#r?.findAll({id:e.id,target:e.extensionPoint.target}).forEach(e=>{e.destroy({reason:t})})})}refreshStorefrontTokens(e){e.forEach(e=>{e.apiTokens?.storefrontApi&&this.storefrontTokens.cache.set(this.storefrontTokens.cacheKey(e.extension.appId,"persisted"),e.apiTokens.storefrontApi)})}#i(){s({metafields:this.#s,persistence:this.#o.persistence,proposal:this.#o.checkout.proposal,serializedReceipt:this.#n,source:this.#o.source}),this.#t.subscribe(e=>{this.sandboxCache.preload({extensions:e,storefrontUrl:this.#o.shop.storefrontUrl??"",myshopifyDomain:this.#o.shop.myshopifyDomain})})}},a=class{#a;#o;constructor({activatedExtensions:e,globalExtensions:t=[],context:s}){this.#o=s,this.#a=new Map;for(const o of e){const e=this.cacheKey(o.extension.appId,"persisted");o.apiTokens?.storefrontApi&&this.#a.set(e,o.apiTokens.storefrontApi)}for(const o of t){const e=this.cacheKey(o.extension.appId,"global");o.apiTokens?.storefrontApi&&this.#a.set(e,o.apiTokens.storefrontApi)}}async get(e,t,s){const o=this.cacheKey(e,t),r=this.#a.get(o);if(r){if(!this.#c(r.expiresAt))return this.#p(t),r.token;this.#a.delete(o)}try{const{data:r,error:i}=await this.#o.graphql.coreGraphql.staticQuery(n,{cache:!1,variables:{appId:e,extensionType:t}});if(i)return this.#h(i,e,t),null;const a=r?.extensionApiTokens?.storefrontApi;return a?(this.#a.set(o,a),this.#p(t),a.token):(this.#o.observability.log("use_ensure_storefront_token_not_found_in_response","Extension storefront token not found in API response",{appId:e,extensionType:t,sourceType:this.#o.source.type,reason:s}),null)}catch(i){return this.#h(i,e,t),null}}get cache(){return this.#a}cacheKey(e,t){return`${e}-${t}`}#h(e,t,s){const n=e instanceof Error?e:new o(`Failed to fetch extension storefront token for app ${t}: ${String(e)}`);this.#o.observability.error(n,{severity:"error",metadata:{graphQL:{name:"ExtensionTokensQuery",appId:t,extensionType:s},source:{type:this.#o.source.type}}}),this.#o.observability.log("use_ensure_storefront_token_fetch_failed","Failed to fetch extension storefront token",{appId:t,extensionType:s,sourceType:this.#o.source.type,error:e instanceof Error?e.message:String(e),errorType:e instanceof Error?e.constructor.name:typeof e})}#c(e){return Date.now()>new Date(e).getTime()-6e4}#p(e){"local"===e&&"draftOrder"===this.#o.source.type&&console.warn("Queries to the storefront API might not return data for certain products. The extension is currently using the online store channel for product availability. This is because the extension is in development mode, the extension is not published to the checkout configuration, and the current checkout is a draft order invoice. Install the app and add the extension in order to use draft order product availability.")}};export{a as StorefrontTokensForBrowser,i as UIExtensibilityBrowserEngine};