import{Lt as e,Xt as t}from"./630ba094.L_X-5TXi.js";import{Gb as n,Mo as r,gf as s,hw as a,io as o,xO as l}from"./6ec9a0fa.rdcQt2v8.js";import{m as i}from"./674595ba.CBCkPO6g.js";import{t as u}from"./173d1fdf.ANJvZbGg.js";import{t as c,u as d}from"./74e5a557.CRSHWf3c.js";var p={id:"4cdafe053d77f87bba559bbbb6a22eec2f45f79b0c78adce5fa4cd72653f6135",type:"query",name:"SupportedLocales",source:"query SupportedLocales($countryCode:String!){installmentsSupportedLocales(countryCode:$countryCode){locales default __typename}}"},m=s(({checkout:t,graphql:n,i18n:r})=>{const s=e(()=>t.proposal.proposed.billingAddress.value.countryCode??void 0),a=n.shopPayGraphql.query(p,{variables:()=>({countryCode:s.value??""})}),o=e(()=>{const e=r.locale,t=e.indexOf("-");return-1===t?e:e.slice(0,t)}),l=e(()=>a.data.value?.installmentsSupportedLocales?.locales),i=e(()=>a.data.value?.installmentsSupportedLocales?.default??"en");return{checkoutLocaleSupported:e(()=>Boolean(l.value?.includes(o.value))),countryCode:s,defaultLocale:i,error:a.error,loadingSupportedLocales:a.loading,locale:o,supportedLocales:l}}),_=["PAYMENTS_UNACCEPTABLE_PAYMENT_AMOUNT","DELIVERY_DELIVERY_LINE_DETAIL_CHANGED","CARD_DECLINED","NAME_MISMATCH","INCORRECT_CVC",c.InvalidShippingAddress,a.InvalidExtensionState,a.InvalidCaptcha],y=["DISABLE_PAYMENT_METHOD","RESTART_PAYMENT_METHOD","RETRY_DIFFERENT_INSTRUMENT","RETRY_SAME_INSTRUMENT"];function f(){const{source:e,checkout:{configuration:n},shop:s,shopPay:a,observability:o,userEvents:i,client:u}=l(),{installments:p,session:m}=a,f=a.user.installments,h=t((t,{terminal:a=!1}={})=>{const l=v(t),h=function(e){return e&&"failure"in e&&"payment"===e.failure?.type?e.failure.recoveryAction??null:null}(t),g=a?null:function(e){return e&&y.includes(e)?e:null}(h),E=function(e){return e&&"failure"in e&&"payment"===e.failure?.type?e.failure.code:void 0}(t),A=!a&&(g?"DISABLE_PAYMENT_METHOD"!==g:function(e){if(!e)return!1;if("reasons"in e&&e.reasons)return e.reasons.some(e=>_.includes(e));if("failure"in e&&e.failure){const t=e.failure;return"payment"===t.type&&_.includes(t.code)}return"violations"in e&&e.violations.some(({code:e})=>_.includes(e))}(t));let S;A?(S=r.InstallmentsRetryError,p.unstable_setModalCloseForRetry(!0)):S=r.InstallmentsPermanentRejection,function(e){return"agreement_invalid"===e?.status&&e.violations.some(({code:e})=>e===c.InvalidShippingAddress)}(t)&&p.unstable_setAgreementError(c.InvalidShippingAddress);const I=f.wasRejected.value;p.unstable_setFailedAttempt({retryable:A,recoveryAction:g,...E?{errorCode:E}:{},...h?{coreRecoveryAction:h}:{}}),f.unstable_recordFailedAttempt();const C=A,T=null!==p.failedAttempt.value,b=p.agreementError.value;o.counter({name:"shop_pay_installments_decline",value:1,attributes:{checkout_error_code:E??"unknown",recovery_action:g??"none",core_recovery_action:h??"none",retryable:A}}),o.log("shop_pay_installments_error_occurred","[Installments] An error has occurred",{errors:l,eventType:S,installmentsRetryableSignal:C,installmentsRejectedSignal:I,installmentsFailedAttemptSignal:T,installmentsAgreementErrorSignal:b,recoveryAction:g,rawRecoveryAction:h}),i.monorailEvent({schemaId:"shopify_pay_payment_page_installments/1.4",payload:{...d(e,{configuration:n},s,u.userAgentDetails),eventType:S,success:!1,errorMessage:l,retryEligible:A,metadata:JSON.stringify({recommended_recovery_action:g,checkout_error_code:E})}}),m.unstable_setInternalProgressing(!1)},[o,i,p,f,e,n,s,m,u.userAgentDetails]);return{onInstallmentsError:h,onProgressionResult:t((e,t)=>{if("success"===t.status||"alreadyCompleted"===t.status)return;let n=!1;if(m.showSessionRevalidator.value||m.sessionRevalidationOptions.value?.challenge){const e=p.stepUpDismissals.value;if(e<3)return p.unstable_setModalOpen(!1),void o.log("shop_pay_installments_step_up_required","[Installments] Shop Pay step-up required, keeping the plan",{eventType:r.InstallmentsStepUpRequired,errors:v(t),stepUpDismissals:e});n=!0,o.log("shop_pay_installments_step_up_limit_reached","[Installments] Shop Pay step-up limit reached, clearing the plan",{errors:v(t),stepUpDismissals:e})}h(t,{terminal:n}),p.payment.value===e&&p.unstable_updatePayment(void 0)},[p,o,h,m])}}function v(e){if(!e)return"no_result";if("errors"in e&&e.errors.length>0){const t=e.errors.map(e=>e.code).join(", ");if(t)return t}return"failure"in e&&e.failure?`failed_receipt_${e.failure.type}`:"status"in e&&e.status?"invalid"===e.status?`error_result_invalid__${e.reasons.join("__")}`:`error_result_${e.status}`:void 0}function h({shopPay:e,checkout:t}){const r=e.user.creditCards.value,{paymentMethods:s}=t.negotiator.current.fields,a=n(s.value)?.availableLoanTypes;return{vaultedCardCount:r.length,expiredCardCount:r.filter(({expired:e})=>e).length,disabledCardCount:r.filter(n=>u(n,s,e.session.isInstallmentsSelected,t.installments.paymentMethodMustSupportInterestBearingLoans)).length,selectedMethodType:e.session.selectedPaymentMethods.value[0]?.paymentMethod??"none",onlyInterestLoans:o(a),cardlessCheckout:i({shopPay:e,checkout:t})}}function g({checkoutToken:e,cardId:t,cardlessCheckout:n}){return Boolean(e)&&(Boolean(t)||n)}export{m as i,h as n,f as r,g as t};