import{r as e}from"./rolldown-runtime-DCMCV6zW.js";import{W as t}from"./vendor-other-C_YjAAj3.js";import{t as n}from"./types-BW--Ya-K.js";import{o as r}from"./i18n-C2nvaQjh.js";import{t as i}from"./telemetry-C4UotafO.js";import{ar as a,ir as o,rr as s}from"./dialogService-Daa_oID7.js";var c=[`gclid`,`gbraid`,`wbraid`],l=`comfy_checkout_attribution`;function readStoredClickIds(){try{let e=localStorage.getItem(l);if(!e)return{};let n=JSON.parse(e);if(!t(n))return{};let r={};for(let e of c){let t=n[e];typeof t==`string`&&t.length>0&&(r[e]=t)}return r}catch{return{}}}function persistClickIds(e){try{localStorage.setItem(l,JSON.stringify(e))}catch{return}}function readClickIdsFromUrl(e){let t=new URLSearchParams(e),n={};for(let e of c){let r=t.get(e);r&&(n[e]=r)}return n}function asNonEmptyString(e){return typeof e==`string`&&e.length>0?e:void 0}function getGaIdentity(){if(typeof window>`u`)return;let e=window.__ga_identity__;if(t(e))return{client_id:asNonEmptyString(e.client_id),session_id:asNonEmptyString(e.session_id),session_number:asNonEmptyString(e.session_number)}}function getCheckoutAttribution(){if(typeof window>`u`)return{};let e=readStoredClickIds(),t=readClickIdsFromUrl(window.location.search),n={...e,...t};Object.keys(t).length>0&&persistClickIds(n);let r=getGaIdentity();return{...n,ga_client_id:r?.client_id,ga_session_id:r?.session_id,ga_session_number:r?.session_number}}var getCheckoutTier=(e,t)=>t===`yearly`?`${e}-yearly`:e;async function performSubscriptionCheckout(e,t,c=!0){if(!n)return;let{getFirebaseAuthHeader:l,userId:u}=o(),d=i(),f=await l();if(!f)throw new s(r(`toastMessages.userNotAuthenticated`));let p=getCheckoutTier(e,t),m=getCheckoutAttribution(),h={...m},g=await fetch(`${a()}/customers/cloud-subscription-checkout/${p}`,{method:`POST`,headers:{...f,"Content-Type":`application/json`},body:JSON.stringify(h)});if(!g.ok){let e=`Failed to initiate checkout`;try{e=(await g.json()).message||e}catch{try{e=await g.text()||`HTTP ${g.status} ${g.statusText}`}catch{e=`HTTP ${g.status} ${g.statusText}`}}throw new s(r(`toastMessages.failedToInitiateSubscription`,{error:e}))}let _=await g.json();_.checkout_url&&(u&&d?.trackBeginCheckout({user_id:u,tier:e,cycle:t,checkout_type:`new`,...m}),c?window.open(_.checkout_url,`_blank`):globalThis.location.href=_.checkout_url)}export{getCheckoutAttribution as n,performSubscriptionCheckout as t};
//# sourceMappingURL=subscriptionCheckoutUtil-BUzL3mZ0.js.map