import{v as e,T as t,h as n,t as a,w as r,m as s}from"./2c1ab0fe.Dbn2KvkT.js";import{aM as o,d as i,bF as d,bG as u,aO as p,I as l,bH as c,aP as y,bI as m,bJ as h,bK as v,bL as f,bM as b,bN as g,bO as P,bP as C,w as M,P as A,bQ as T,i as k,br as S}from"./4b4bc88f.z1yoAwwH.js";import{w as _,ci as w,cj as I,ck as E}from"./164ad2e3.FTBuhkTQ.js";import{h as N}from"./e12db40c.DF32J314.js";import{s as O,i as j}from"./08a3b809.Ce8w6gv6.js";class q extends Error{constructor(){super(...arguments),this.name="UnsupportedShopPayCustomerCardError"}}function x({allowPreselect:r=!0,displayedPaymentMethods:s}={}){const A=o(),{checkout:{identity:T,negotiator:k},observability:S,userEvents:E,shop:O}=i(),{hasNegotiatedAlternativePaymentCurrencySignal:j,newCardSignal:x}=k.context,B=k.current.fields.paymentMethods,{value:G}=d(),{negotiate:J}=_(),H=k.current.fields.paymentLines,R=u(),{currencyCode:D,id:K,paymentMethodAutoSelectionDisabled:Q}=O,V="shopPay"===T.current.value,z=!V,W=s??G,X=e(()=>L(W,V),[W,V]),{value:Y}=k.current.fields.buyerIdentity,Z=k.parts.paymentLines,{value:$}=k.current.fields.deferredTotal,{value:ee}=k.current.fields.paymentFlexibilityPaymentTermsTemplate,te=p(),ne=Y?.presentmentCurrency||D,ae=w(),re=t(!1),se=t(!1),oe=V?Z.value.find(e=>e.method.type===l.CreditCard)?.method.type:void 0;n(()=>{oe&&!se.current&&(se.current=!0,S.counter({name:"shop_pay_unsupported_customer_card_on_payline",value:1,attributes:{payment_method_type:oe}}),S.error(new q("Shop Pay proposed payment line contains an unsupported Customer Account saved card"),{severity:"warning",metadata:{value:{paymentMethodType:oe}}}))},[S,oe]);const ie=e(()=>{const e=X.find(c);if(z&&e&&y(Z.value))return m(e);const t=Z.value.filter(e=>h(e,X,{identity:T})||v(e)),n=f(t),[a]=n;return void 0!==a?a:(S.leaveErrorBreadcrumb("Unexpected: Selected payment method is empty because `paymentLines` array is empty",{displayedPaymentMethods:JSON.stringify(X),displayedProposedPaymentLines:JSON.stringify(t)}),"")},[X,T,S,Z.value,z]),de=a((e,t,{rebuild:n=!1,buyerInitiated:a=!1}={})=>{const r=Z.value,o=L(s??B.value??[],V),i=U(r,o),d=r.some(e=>"direct"===e.method.type),u=b([e],[...r,...!d&&x.value?[{method:x.value}]:[]],o,{negotiatedPaymentLines:H.value?.lines,deferredTotal:$,hasPayableDeposit:Boolean(R.value?.amount),hasFixedSellingPlan:te.value,rebuild:n,paymentTermsTemplateType:ee?.type,preserveShopPayApproval:z});Z.value=t?function(e,t){return e.map(e=>function(e,t){return e.method.type===t.type?{...e,method:{...e.method,...t}}:e}(e,t))}(u,t):u;const p=U(u,o),l=g(i,p,ne);l&&(j.value=!0,E.monorailEvent(function({uniqueToken:e,shopGid:t,shopCurrencyCode:n,presentmentCurrencyCode:a,checkoutSessionIdentifier:r,previousPaymentMethod:s,currentPaymentMethod:o}){return{schemaId:"multi_currency_checkout_payment_method_change/2.0",payload:{shopId:parseInt(M(t),10),userToken:e,checkoutToken:r||"",shopCurrencyCode:n,checkoutPresentmentCurrencyCode:a,priorPaymentMethodHandle:F(s),priorPaymentType:I(s?.type||""),newPaymentMethodHandle:F(o),newPaymentType:I(o?.type||""),isPriorPaymentMethodMc:C(a,s),isNewPaymentMethodMc:C(a,o)}}}({uniqueToken:A.value.defaultAttributes?.uniqToken||"",shopGid:K,shopCurrencyCode:D,presentmentCurrencyCode:ne,previousPaymentMethod:i,currentPaymentMethod:p})));const c=a&&P(i,p);(l||c)&&J({})},[j,B,H.value?.lines,x,$,R.value?.amount,ne,Z,E,A.value.defaultAttributes?.uniqToken,K,D,J,ee,te,s,V,z]);n(()=>{ae&&!ie&&0===Z.value.length&&(re.current=!1)},[ie]);const ue=X.at(0);if(r&&!ie&&!re.current&&ue&&!N(Z.value)&&!(Q&&X.length>1)){const e=m(ue);S.leaveErrorBreadcrumb("rebuilding payment lines on missing selected payment line",{displayedPaymentMethods:JSON.stringify(X),selectedPaymentMethod:e}),de(e,void 0,{rebuild:!0}),re.current=!0}return e(()=>[ie,de],[ie,de])}function L(e,t){return t?e.filter(e=>e.type!==l.CreditCard):e}function F(e){if(void 0===e)return"";const t="name"in e&&e.name||void 0,n="paymentMethodIdentifier"in e&&e.paymentMethodIdentifier||void 0;return E(e.type,t,n)}function U(e,t){return t.find(t=>e.find(e=>"paymentMethodIdentifier"in e.method&&"paymentMethodIdentifier"in t?e.method.paymentMethodIdentifier===t.paymentMethodIdentifier:"wallet"===e.method.type&&"wallet"===t.type?e.method.name===t.name:e.method.type===t.type))}function B({isApplePayAvailable:t}){const{checkout:{identity:n,negotiator:a},shopPay:s}=i(),o=r(t);return e(()=>O({identity:n,shopPay:s,negotiatedFields:a.current.fields},{isApplePayAvailable:o}),[n,o,a.current.fields,s])}function G(){const{shop:e}=i(),t=e.hasFlagEnabled(A),n=T();return s(()=>!t&&n.value)}function J(){const{negotiate:e}=_(),{shopPay:t,checkout:n,observability:r}=i(),s=n.configuration.addressSettings.nonBillingAddressSettings;return{updateCheckoutAddress:a(async({shippingAddress:a,billingAddress:o,fallbackPhoneNumber:i},d=!1)=>{if(!a&&!o)return;const u=[];if(a){u.push("shippingAddress");const e=s.value.isRequired("phone",{countryCode:a.countryCode,useMerchantSettings:!0})&&!a.phone?i:a.phone;r.log("wallets_shipping_address_update","useUpdateCheckoutAddress: update with Shop Pay vaulted address",{isPostalCodePadded:k(a.postalCode,a.countryCode),countryCode:a.countryCode}),n.negotiator.parts.shippingAddress.value={...a,phone:e,oneTimeUse:a.oneTimeUse||!1}}if(o){u.push("billingAddress");const e=s.value.isRequired("phone",{countryCode:o.countryCode,useMerchantSettings:!0})&&!o.phone?i:o.phone;n.negotiator.parts.billingAddress.value={...o,phone:e}}const p=j(a??n.negotiator.parts.shippingAddress.value,o??n.negotiator.parts.billingAddress.value);t.user.hasCreditCards.value&&(n.negotiator.parts.billingAddressOption.value=p?"shipping":"custom"),d||await e({silenceViolations:["non-stock",...S],fieldsToResolve:u})},[e,n.negotiator.parts.shippingAddress,n.negotiator.parts.billingAddress,n.negotiator.parts.billingAddressOption,s,t.user,r])}}export{G as a,J as b,B as c,x as u};