const r=window.paypalV6Config,y=e=>document.getElementById(e),p=e=>{const t=y("paypal-sdk-status");t&&(t.textContent=e||"")};async function f(e,t){const n=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json","X-CSRF-TOKEN":r.csrfToken,"X-Requested-With":"XMLHttpRequest"},body:JSON.stringify(t||{})}),a=await n.json().catch(()=>({}));return{ok:n.ok,data:a}}function m(e){return async()=>{const{ok:t,data:n}=await f(r.routes.createOrder,{payment_source:e});if(!t||!n.orderId)throw new Error(n.error||r.translations.error);return{orderId:n.orderId}}}async function l(e){p(r.translations.processing);const t=e&&(e.orderId||e.orderID)||null,{ok:n,data:a}=await f(r.routes.capture,{paypal_order_id:t});if(n&&a.status==="success"&&a.redirect){window.location.href=a.redirect;return}p(a.error||r.translations.error),a.redirect&&(window.location.href=a.redirect)}function d(){p(r.translations.cancelled)}function c(e){p(r.translations.error),console.error("[PayPal v6]",e)}function P(e){return new Promise((t,n)=>{const a=document.createElement("script");a.src=e,a.async=!0,a.onload=t,a.onerror=()=>n(new Error("PayPal SDK load failed")),document.head.appendChild(a)})}async function g(){if(!r.needsClientToken)return null;const{ok:e,data:t}=await f(r.routes.clientToken,{});return e&&t.clientToken||null}function u({containerId:e,tagName:t,session:n,source:a}){const o=y(e);if(!o||!n)return;let i;try{i=document.createElement(t)}catch{return}o.appendChild(i),i.addEventListener("click",async()=>{try{await n.start({presentationMode:"auto"},m(a)())}catch(s){c(s)}})}function w(e){if(typeof e.createPayPalOneTimePaymentSession!="function")return;const t=e.createPayPalOneTimePaymentSession({onApprove:l,onCancel:d,onError:c});u({containerId:"paypal-button-container",tagName:"paypal-button",session:t,source:"paypal"})}function h(e){if(typeof e.createPayLaterOneTimePaymentSession!="function")return;const t=e.createPayLaterOneTimePaymentSession({onApprove:l,onCancel:d,onError:c});u({containerId:"paypal-paylater-container",tagName:"paypal-pay-later-button",session:t,source:"pay_later"})}function T(e){if(typeof e.createVenmoOneTimePaymentSession!="function")return;const t=e.createVenmoOneTimePaymentSession({onApprove:l,onCancel:d,onError:c});u({containerId:"paypal-venmo-container",tagName:"venmo-button",session:t,source:"venmo"})}function b(e){if(typeof e.createCardFields!="function")return;const t=y("paypal-card-fields");t&&t.classList.remove("d-none");let n;try{n=e.createCardFields({onApprove:l,onError:c,createOrder:m("card")})}catch(o){c(o);return}try{n.NumberField&&n.NumberField().render("#paypal-card-number"),n.ExpiryField&&n.ExpiryField().render("#paypal-card-expiry"),n.CVVField&&n.CVVField().render("#paypal-card-cvv")}catch{}const a=y("paypal-card-submit");a&&a.addEventListener("click",async()=>{p(r.translations.processing);try{await n.submit()}catch(o){c(o)}})}function I(e){if(typeof e.createApplePayOneTimePaymentSession!="function")return;const t=e.createApplePayOneTimePaymentSession({onApprove:l,onCancel:d,onError:c});u({containerId:"paypal-applepay-container",tagName:"apple-pay-button",session:t,source:"applepay"})}function O(e){if(typeof e.createGooglePayOneTimePaymentSession!="function")return;const t=e.createGooglePayOneTimePaymentSession({onApprove:l,onCancel:d,onError:c});u({containerId:"paypal-googlepay-container",tagName:"google-pay-button",session:t,source:"googlepay"})}function E(e){if(typeof e.createFastlane=="function")try{e.createFastlane({onApprove:l,onError:c,createOrder:m("card")})}catch(t){console.error("[PayPal v6] fastlane",t)}}async function S(){if(!(!r||!r.clientId))try{await P(r.sdkUrl);const e=await g(),t={components:r.components,pageType:"checkout"};e?t.clientToken=e:t.clientId=r.clientId,window.crypto&&typeof window.crypto.randomUUID=="function"&&(t.clientMetadataId=window.crypto.randomUUID());const n=await window.paypal.createInstance(t);let a=null;try{typeof n.findEligibleMethods=="function"&&(a=await n.findEligibleMethods({currencyCode:r.currency}))}catch{}const o=s=>{try{return a&&typeof a.isEligible=="function"?a.isEligible(s):!0}catch{return!0}},i=s=>Array.isArray(r.funding)&&r.funding.includes(s);i("paypal")&&o("paypal")&&w(n),i("paylater")&&o("paylater")&&h(n),i("venmo")&&o("venmo")&&T(n),i("card")&&b(n),i("applepay")&&o("applepay")&&I(n),i("googlepay")&&o("googlepay")&&O(n),i("fastlane")&&E(n)}catch(e){p(r.translations.error),console.error("[PayPal v6] init failed",e)}}document.addEventListener("DOMContentLoaded",S);
