import{t}from"./chunk.window_CAbVMZox.esm.js";import"./chunk.init_4RtmBDrg.esm.js";import{S as e,_ as n,g as o,i as r,j as s,k as i,r as a,t as c,u,w as d}from"./chunk.register_Cnn1s0Rw.esm.js";import{n as l}from"./chunk.useUserRecognitionSignal_BnfzXl6K.esm.js";import{i as p,n as m,r as f}from"./chunk.storage_n5jjEbMM.esm.js";import{t as _}from"./chunk.hooks_DeX7xKd-.esm.js";import{n as h}from"./chunk.authorize_DINGMyE5.esm.js";import{t as y}from"./chunk.useEventListener_BH3yPESc.esm.js";const g="signInWithShop:cartSyncTransferAttemptedAt",w="signInWithShop:cartSyncNextRecognitionAt",v="signInWithShop:cartSyncTransferCompletedAt";function S({now:t=Date.now(),recognitionTtlSeconds:e}={}){const{source:n,windowMs:o}=function(t){if(!("number"==typeof t&&Number.isFinite(t)&&t>0))return{source:"default",windowMs:3e5};const e=Math.round(1e3*t);return e>2592e6?{source:"server_clamped",windowMs:2592e6}:{source:"server",windowMs:e}}(e),r=f(w,`${t+o}`);return r&&m(v),{stored:r,windowMs:o,windowSource:n}}function b(t,e){if(null===t||""===t)return{status:"missing"};const n=Number(t);return!Number.isFinite(n)||n<=0?{status:"invalid"}:n>e?{status:"future"}:{status:"valid",timestamp:n}}function j({now:t=Date.now()}={}){const{ok:e,value:n}=p(w),{ok:o,value:r}=p(g);if(!e||!o)return{reason:"read_storage_unavailable",shouldAttempt:!1};const s=function(t,e){if(null===t||""===t)return"missing";const n=Number(t);return!Number.isFinite(n)||n<=0||n-e>2592e6?"invalid":n>e?"active":"passed"}(n,t);let i;if("active"===s)return{reason:"timestamp_within_ttl",shouldAttempt:!1,step:"completed"};if("passed"===s)i={reason:"timestamp_expired",shouldAttempt:!0,step:"completed"};else if("invalid"===s)i={reason:"timestamp_invalid",shouldAttempt:!0,step:"completed"};else if(function(t){const{ok:e,value:n}=p(v);if(!e)return!1;const o=b(n,t);return"valid"===o.status&&t-o.timestamp<3e5}(t))return{reason:"timestamp_within_ttl",shouldAttempt:!1,step:"completed"};const a=b(r,t);var c;return"missing"===a.status?null!==(c=i)&&void 0!==c?c:{reason:"no_prior_attempt",shouldAttempt:!0}:"invalid"===a.status?{reason:"timestamp_invalid",shouldAttempt:!0,step:"attempted"}:"future"===a.status?{reason:"timestamp_in_future",shouldAttempt:!0,step:"attempted"}:t-a.timestamp>=3e5?{reason:"timestamp_expired",shouldAttempt:!0,step:"attempted"}:{reason:"timestamp_within_ttl",shouldAttempt:!1,step:"attempted"}}function T(t){var e;if(null===t)return"null";if(Array.isArray(t))return"Array";const n=null==t||null===(e=t.constructor)||void 0===e?void 0:e.name;return"string"==typeof n&&""!==n?n:typeof t}function k(t){const e={type:T(t),typeofValue:typeof t};if("object"==typeof t&&null!==t){e.ownKeys=Object.keys(t).slice(0,20),e.prototypeTag=Object.prototype.toString.call(t);const{code:n,name:o,status:r}=t;void 0!==n&&(e.code=String(n)),void 0!==o&&(e.name=String(o)),void 0!==r&&(e.status=String(r))}return e}function A(t,e){return 429===function(t){if("object"!=typeof t||null===t)return;const{code:e,status:n}=t;for(const t of[n,e]){const e="string"==typeof t?Number(t):t;if("number"==typeof e&&Number.isFinite(e))return e}}(t)||/too many requests|rate.?limit/i.test(e.message)}function z({onComplete:n,source:r}){const{notify:s}=o(),{recordCounter:a,recordHistogram:c}=u(),d=l(),p=e(Date.now()).current,m=e(!1),f=i((e=>{if(["blocked","completed"].includes(e.type)){if(m.current||(c("shop_js_iframe_load_duration",{value:Date.now()-p}),m.current=!0),"blocked"===e.type)return a("shop_js_cart_sync_finalize_blocked"),void(null==n||n());if("completed"===e.type){if("boolean"==typeof e.recognized)return a("shop_js_cart_sync_finalize_skipped",{attributes:{recognized:e.recognized}}),null==n||n("number"==typeof e.recognition_ttl_seconds?{recognitionTtlSeconds:e.recognition_ttl_seconds}:void 0),void d({recognized:e.recognized});a("shop_js_cart_sync_finalize_fetch");const o=Date.now();!function({onError:e,onResolve:n}){fetch(`${t.location.origin}/services/login_with_shop/buyer/finalize`).then((async t=>{try{await n(t)}catch(t){e(t,"resolve")}}),(t=>e(t,"fetch")))}({onError:(t,e)=>{const n=function(t){if(t instanceof Error)return t;if("string"==typeof t)return new Error(t);if("object"==typeof t&&null!==t){const{message:e}=t;return"string"==typeof e&&""!==e?new Error(e):new Error(`Non-Error rejection (${T(t)})`)}return new Error(String(t))}(t),o=!(t instanceof Error),r="fetch"===e&&A(t,n);a("shop_js_cart_sync_finalize_error",{attributes:{phase:e,rateLimited:r,nonErrorRejection:o}}),r||"fetch"===e&&o||s(n,{metadata:{cartSyncFinalize:{phase:e,...k(t)}}})},onResolve:async t=>{const{status:e}=t;if(c("shop_js_fetch_duration",{attributes:{action:"shop_user_recognition_finalize"},value:Date.now()-o}),a("shop_js_cart_sync_finalize_resolve",{attributes:{status:e}}),200===e){const e=await t.json().catch((()=>null)),o="number"==typeof(null==e?void 0:e.recognition_ttl_seconds)?e.recognition_ttl_seconds:void 0;null==n||n(void 0===o?void 0:{recognitionTtlSeconds:o}),d({recognized:!0===(null==e?void 0:e.recognized)})}}})}}}),[d,p,s,n,a,c]),{destroy:_}=y({allowedOrigins:[h,t.location.origin],handler:f,source:r});return{destroy:_}}function I({analyticsTraceId:t,shopDomain:e,storefrontProxied:n,targetOrigin:o}){const r=function({analyticsTraceId:t,shopDomain:e,storefrontProxied:n,targetOrigin:o}){return new URLSearchParams({analytics_trace_id:t,target_origin:o,client_handle:e,...n?{sf_proxied:"1"}:{}})}({analyticsTraceId:t,shopDomain:e,storefrontProxied:n,targetOrigin:o});return`${h}/pay/hop?${r}`}function x({reason:t,step:e}){const n={reason:t};return e&&(n.step=e),n}const D=({storefrontProxied:o})=>{const{log:r,recordCounter:a}=u(),c=e(null),l=e(""),p=e(o);p.current=o;const{analyticsData:{analyticsTraceId:h}}=_(),y=i((t=>{const{reason:e,step:n}=t,o=x(t);a("shop_js_cart_sync_transfer_session_skipped",{attributes:o});r({body:"write_storage_failed"===e?`ShopCartSync transfer session ${n} timestamp could not be written`:"ShopCartSync transfer session skipped",attributes:{analyticsTraceId:h,...o}})}),[h,r,a]),w=i((t=>{a("shop_js_cart_sync_transfer_session_attempted",{attributes:x(t)})}),[a]),v=i((t=>{const{stored:e,windowMs:n,windowSource:o}=S({recognitionTtlSeconds:null==t?void 0:t.recognitionTtlSeconds});e?(a("shop_js_cart_sync_next_recognition_stored",{attributes:{source:o}}),r({body:"ShopCartSync recognition TTL applied",attributes:{analyticsTraceId:h,appliedWindowMs:n,recognitionTtlSeconds:null==t?void 0:t.recognitionTtlSeconds,windowSource:o}}),m(g)):y({reason:"write_storage_failed",step:"completed"})}),[h,r,a,y]),{destroy:b}=z({onComplete:v,source:c});s((()=>{const t=c.current;return()=>{t&&b()}}),[b]);const T=d((()=>{var e,n;if(l.current)return l.current;const o=t.location.origin,s=null===(e=t.Shopify)||void 0===e?void 0:e.shop;if(!s)return r({body:"Missing Shopify domain from window.Shopify",attributes:{analyticsTraceId:h,domain:o}}),"";if(null===(n=t.Shopify)||void 0===n?void 0:n.designMode)return"";const i=j();if(!i.shouldAttempt)return y(i),"";if(!f("signInWithShop:cartSyncTransferAttemptedAt",`${Date.now()}`))return y({reason:"write_storage_failed",step:"attempted"}),"";w(i);const a=I({analyticsTraceId:h,shopDomain:s,storefrontProxied:p.current,targetOrigin:o});return l.current=a,a}),[h,r,w,y]);return n("iframe",{"aria-hidden":"true","data-testid":"shop-cart-sync-iframe",ref:c,src:T,style:{display:"none"},tabIndex:-1,title:"Shop Pay cart sync"})};c((({element:t,...e})=>n(a,{element:t,featureName:"ShopCartSync",children:n(r,{children:n(D,{...e})})})),{name:"shop-cart-sync",props:{storefrontProxied:"boolean"},shadow:"open"}); import"./chunk.document_DMcRsBXN.esm.js";import"./chunk.casing_U_9x-Om3.esm.js";import"./chunk.v4_C3N_0s0v.esm.js";import"./chunk.networkErrorMessages_Du380vAr.esm.js";import"./chunk.utils_Cjeq_SSZ.esm.js";import"./chunk.validators_BbO3UEjF.esm.js"; //# sourceMappingURL=client.shop-cart-sync_D5Lat9V8.it.esm.js.map