Module 237299

3,585 characters · in 3177c2c192ad92c2.js, 475c9724deb2220f.js
Dependencies
Required by (1)
Lazily required by (0)none
Requires (2)
Lazily requires (0)none
Source Raw
function(e,t,n){n.d(t,{ut:()=>i,ws:()=>u});var r=n(582128),l=n(652215);function i(e){let{giftCardWallet:t,dropdownPaymentSources:n,subscriptionPaymentSourceId:r,defaultPaymentSourceId:l}=e;if(null!=t&&(null==r||r===t.id))return t.id;if(null!=r){let e=n.find(e=>e.id===r);if(null!=e&&e.enabled)return r}if(null!=l){let e=n.find(e=>e.id===l);if(null!=e&&e.enabled)return l}return n.find(e=>e.enabled)?.id??null}class o{checkoutPaymentSources;isReady;pendingPaymentSourceId;selectedPaymentSourceId;dropdownPaymentSources;subscriptionPaymentSourceId;giftCardWallet;giftCardsEnabled;hasInitialPaymentSourceSeed;hasAddedPaymentSourceThisSession;constructor({checkoutPaymentSources:e,dropdownPaymentSources:t,subscriptionPaymentSourceId:n,giftCardWallet:r,isReady:l,pendingPaymentSourceId:i,paymentSourceId:o,giftCardsEnabled:u,hasInitialPaymentSourceSeed:s,hasAddedPaymentSourceThisSession:a}){this.checkoutPaymentSources=e,this.isReady=l,this.pendingPaymentSourceId=i,this.selectedPaymentSourceId=o,this.dropdownPaymentSources=t,this.subscriptionPaymentSourceId=n,this.giftCardWallet=r,this.giftCardsEnabled=u,this.hasInitialPaymentSourceSeed=s,this.hasAddedPaymentSourceThisSession=a}get hasPaymentSourcesFromCheckoutStore(){return this.isReady&&this.checkoutPaymentSources.length>0}get isPendingPaymentSourceEqualToCurrent(){return null!=this.pendingPaymentSourceId&&this.pendingPaymentSourceId===this.selectedPaymentSourceId}getResolvedPaymentSourceId(){return i({giftCardWallet:this.giftCardWallet,dropdownPaymentSources:this.dropdownPaymentSources,subscriptionPaymentSourceId:this.subscriptionPaymentSourceId,defaultPaymentSourceId:this.selectedPaymentSourceId})}shouldSetInitialPaymentSourceId(e){let{hasCheckedInitialPaymentSourceRef:t}=e;return!t.current&&!!this.hasPaymentSourcesFromCheckoutStore&&!this.isPendingPaymentSourceEqualToCurrent&&(!this.hasInitialPaymentSourceSeed&&!this.hasAddedPaymentSourceThisSession||!this.isSelectedPaymentSourceValid)}checkAndResolveInitialPaymentSourceId(e){let{hasCheckedInitialPaymentSourceRef:t}=e;if(!this.shouldSetInitialPaymentSourceId({hasCheckedInitialPaymentSourceRef:t}))return{shouldSet:!1,initialPaymentSourceId:null};t.current=!0;let n=this.getResolvedPaymentSourceId();return n!==this.selectedPaymentSourceId?{shouldSet:!0,initialPaymentSourceId:n}:{shouldSet:!1,initialPaymentSourceId:null}}get isSelectedPaymentSourceValid(){return this.checkoutPaymentSources.some(e=>(function(e){let{paymentSource:t,paymentSourceId:n,giftCardsEnabled:r}=e;return(t.type!==l.hes.TDS_WALLET||!!r)&&null!=n&&t.id===n&&t.enabled})({paymentSource:e,paymentSourceId:this.selectedPaymentSourceId,giftCardsEnabled:this.giftCardsEnabled}))}shouldSelfCorrectPaymentSourceId(e){let{hasCheckedInitialPaymentSourceRef:t}=e;return!!this.hasPaymentSourcesFromCheckoutStore&&null!=this.selectedPaymentSourceId&&!!t.current&&!this.isPendingPaymentSourceEqualToCurrent&&!this.isSelectedPaymentSourceValid}resolveSelfCorrectPaymentSourceId(e){let{hasCheckedInitialPaymentSourceRef:t}=e;return this.shouldSelfCorrectPaymentSourceId({hasCheckedInitialPaymentSourceRef:t})?{shouldSet:!0,correctedPaymentSourceId:this.getResolvedPaymentSourceId()}:{shouldSet:!1,correctedPaymentSourceId:null}}}function u(e,t){let n=r.useRef(!1),l=r.useMemo(()=>new o(t),[t]);r.useEffect(()=>{let{shouldSet:t,initialPaymentSourceId:r}=l.checkAndResolveInitialPaymentSourceId({hasCheckedInitialPaymentSourceRef:n});t&&e(r)},[l,e]),r.useEffect(()=>{let{shouldSet:t,correctedPaymentSourceId:r}=l.resolveSelfCorrectPaymentSourceId({hasCheckedInitialPaymentSourceRef:n});t&&e(r)},[l,e])}}