import{useEditorMode as e,useQuantity as n,useCartData as t,useCurrentVariant as o,useShopStore as i,useCurrentVariantInStock as d}from"@gem-sdk/core";import{useMemo as r}from"react";import{getStoreUrl as c,getIdFromGid as s}from"../components/shop-pay-button/common/helpers.js";let useShopPayButton=w=>{let m=e(),{quantity:p}=n(),{data:a}=t(),u=o(),h=i(e=>e.storefrontUrl),l=d(),y=c(h),$=r(()=>{let e=a?.lineItems.map(e=>({id:e.variantId,quantity:e.quantity}))?.reduce((e,n)=>{let t=s(n?.id);return t&&e.push(`${t}:${n?.quantity??1}`),e},[]);return e},[a?.lineItems]),f=r(()=>w?.kind==="cart-buy"?$:u?.baseID&&l&&p?[`${s(u.baseID)}:${p}`]:void 0,[w?.kind,u?.baseID,l,p,$]),g=`${y}/cart/${f?.join(",")}?payment=shop_pay`,b=({inputWidth:e=600,inputHeight:n=600})=>{if("edit"!==m){if(w?.openMode==="popup"){let t=void 0!==window.screenLeft?window.screenLeft:window.screenX,o=void 0!==window.screenTop?window.screenTop:window.screenY,i=window.innerWidth?window.innerWidth:document.documentElement.clientWidth?document.documentElement.clientWidth:screen.width,d=window.innerHeight?window.innerHeight:document.documentElement.clientHeight?document.documentElement.clientHeight:screen.height,r=i/window.screen.availWidth,c=(i-e)/2/r+t,s=(d-n)/2/r+o;window.open(g,"shop_pay",`scrollbars=yes, width=${e/r}, height=${n/r}, top=${s}, left=${c}`)}else g&&window.open(g)}};return{storeUrl:y,variants:f,handleOnPay:b}};export{useShopPayButton};
