Loading
function(e,t,n){n.d(t,{Y0:()=>h,iB:()=>d});var l=n(64700),a=n(702841),i=n(666646),r=n(202613),s=n(459357),o=n(176095),u=n(652215);function c(e){let{giftCardsEnabled:t,checkoutPaymentSources:n}=e,r=l.useMemo(()=>p(n)?.id,[n]),s=(0,i.sw)(),u=s?.subtotal??s?.total,c=(0,a.bG)([o.A],()=>t&&null!=r?o.A.getBalance(r):null,[t,r]);return t&&null!=c?{walletCoversSubtotal:null!=u&&c.amount>=u,walletCoversTotal:null!=s&&c.amount>=s.total,walletId:r,isWalletBalanceLoaded:null!=u}:{walletCoversSubtotal:!1,walletCoversTotal:!1,walletId:r,isWalletBalanceLoaded:!1}}function d(e){let{checkoutPaymentSources:t,paymentSourceId:n,location:l}=e,{enabled:a}=(0,s.c)({location:l}),{walletCoversTotal:i,walletId:r,isWalletBalanceLoaded:o}=c({giftCardsEnabled:a,checkoutPaymentSources:t});return o&&null!=n&&n===r&&!i}function p(e){return e.find(e=>e.source instanceof r.LQ)??null}function m(e){let{giftCardWallet:t,dropdownPaymentSources:n,subscriptionPaymentSourceId:l,defaultPaymentSourceId:a}=e;if(null!=t&&(null==l||l===t.id))return t.id;if(null!=l){let e=n.find(e=>e.id===l);if(null!=e&&e.enabled)return l}if(null!=a){let e=n.find(e=>e.id===a);if(null!=e&&e.enabled)return a}return n.find(e=>e.enabled)?.id??null}function h(e){let{checkoutPaymentSources:t,paymentSourceId:n,setPaymentSourceId:a,location:i,isReady:r=!0,subscriptionPaymentSourceId:o}=e,{enabled:d}=(0,s.c)({location:i}),{dropdownPaymentSources:h,giftCardWallet:C}=l.useMemo(()=>{let e=t.filter(e=>e.type!==u.hes.TDS_WALLET);return d?{dropdownPaymentSources:e,giftCardWallet:p(t)}:{dropdownPaymentSources:e,giftCardWallet:null}},[t,d]),[A,E]=l.useState(null),y=null!=C&&n===C.id,P=l.useMemo(()=>null!=A?A:m({giftCardWallet:null,dropdownPaymentSources:h,subscriptionPaymentSourceId:o}),[h,o,A]),S=l.useMemo(()=>y?P:n??null,[P,y,n]),_=l.useRef(!1);l.useEffect(()=>{if(!r||0===t.length||_.current)return;_.current=!0;let e=m({giftCardWallet:C,dropdownPaymentSources:h,subscriptionPaymentSourceId:o,defaultPaymentSourceId:n});e!==n&&a(e)},[r,t,h,C,n,o,a]),l.useEffect(()=>{r&&0!==t.length&&null!=n&&_.current&&(t.some(e=>(e=>{let{paymentSource:t,paymentSourceId:n,giftCardsEnabled:l}=e;return(t.type!==u.hes.TDS_WALLET||!!l)&&null!=n&&t.id===n&&t.enabled})({paymentSource:e,paymentSourceId:n,giftCardsEnabled:d}))||a(m({giftCardWallet:C,dropdownPaymentSources:h,subscriptionPaymentSourceId:o,defaultPaymentSourceId:n})))},[r,d,t,h,C,n,o,a]);let T=l.useCallback(e=>{a(null!=e?e.id:null)},[a]),f=l.useCallback(e=>{if(e&&null!=C){n!==C.id&&E(n??null),a(C.id);return}a(P)},[a,C,n,P]),{walletCoversSubtotal:N,walletCoversTotal:x,isWalletBalanceLoaded:I}=c({giftCardsEnabled:d,checkoutPaymentSources:t});l.useEffect(()=>{I&&!N&&y&&a(P)},[I,N,y,a,P]);let g=l.useMemo(()=>null==C?null:{giftCardWallet:C,checked:y,onChange:f,disabled:!N},[C,y,f,N]);return{giftCardsEnabled:d,dropdownPaymentSources:h,dropdownPaymentSourceId:S,giftCardWallet:C,isGiftCardCreditsChecked:y,handleGiftCardCreditsToggle:f,handleDropdownPaymentSourceChange:T,giftCardCheckboxProps:g,walletCoversSubtotal:N,walletCoversTotal:x}}}