import{l as N,b as R,r as n,H as _,g as k,k as y,j as e,c as v,B as D}from"./main-TIUJvpaR.js";import{l as M}from"./index-U9JJOqTM.js";import{S as x}from"./skeleton-uSF3jNnm.js";function P({type:t,productId:a,priceId:i}){const{user:s}=N(),f=R(),l=n.useRef(!1),r=n.useRef(null),{localeCode:b}=_(),[c,S]=n.useState(null),[j,h]=n.useState(null),{branding:{site_name:m},billing:{stripe_public_key:o,stripe:{enable:u}}}=k();return n.useEffect(()=>{!u||!o||l.current||(Promise.all([M(o,{locale:b}),t==="setupIntent"?w():B(a,i)]).then(([p,{clientSecret:g}])=>{if(p&&r.current){const d=p.elements({clientSecret:g,appearance:{theme:f?"night":"stripe"}});d.create("payment",{business:{name:m},terms:{card:"never"},fields:{billingDetails:{address:"auto"}},defaultValues:{billingDetails:{email:s==null?void 0:s.email}}}).mount(r.current),S(p),h(d)}}),l.current=!0)},[a,o,u,f,b,m,t,s==null?void 0:s.email]),{stripe:c,elements:j,paymentElementRef:r,stripeIsEnabled:o!=null&&u}}function w(){return y.post("billing/stripe/create-setup-intent").then(t=>t.data)}function B(t,a){return y.post("billing/stripe/create-partial-subscription",{product_id:t,price_id:a}).then(i=>i.data)}function H({productId:t,priceId:a,type:i="subscription",submitLabel:s,returnUrl:f}){const{stripe:l,elements:r,paymentElementRef:b,stripeIsEnabled:c}=P({type:i,productId:t,priceId:a}),[S,j]=n.useState(null),[h,m]=n.useState(!1),o=!c||r!=null&&l!=null,u=async p=>{var g,d;if(p.preventDefault(),!(!l||!r)){m(!0);try{const E=await l[i==="subscription"?"confirmPayment":"confirmSetup"]({elements:r,confirmParams:{return_url:f}});((g=E.error)==null?void 0:g.type)!=="validation_error"&&((d=E.error)!=null&&d.message)&&j(E.error.message)}catch{}m(!1)}};return e.jsxs("form",{onSubmit:u,children:[e.jsx("div",{ref:b,className:v("min-h-[303px]",!c&&"hidden"),children:c&&e.jsx(C,{})}),S&&!h&&e.jsx("div",{className:"mt-20 text-danger",children:S}),e.jsx(D,{variant:"flat",color:"primary",size:"md",className:"mt-40 w-full",type:"submit",disabled:h||!o,children:s})]})}function C(){return e.jsxs(n.Fragment,{children:[e.jsx(x,{className:"mb-20 h-30"}),e.jsx(x,{className:"mb-20 h-30"}),e.jsx(x,{className:"mb-20 h-30"}),e.jsx(x,{className:"h-30"})]})}export{H as S};
