import{r as e}from"./chunk-QTnfLwEv.js";import{n as t}from"./index-7yDgay5u.js";import{i as n,o as r,t as i}from"./jsx-runtime-DHSfUA5-.js";import{P as a,T as o}from"./test.id-DxQrYxPk.js";import{t as s}from"./useSuccessToast-CW8oLO3e.js";import{n as c,t as l}from"./membershipService-iYu9OIlX.js";import{t as u}from"./useTopTips-B9oW_JMO.js";import{n as d}from"./index-9BPhDxrb.js";import{n as f}from"./useMembershipStatus-CuwuZX56.js";import{t as p}from"./useLoadingToast-BBb6RyKX.js";import{t as m}from"./account-DO4Si9F8.js";import{MembershipPageView as h}from"./view-CTt677jt.js";var g=e(r(),1),_=i();function v(){let e=a(l),r=p(),i=s(),v=u(),[S,C]=(0,g.useState)(),[w,T]=(0,g.useState)(!1),[E,D]=(0,g.useState)(),{status:O,isLoading:k}=f();(0,g.useEffect)(()=>{if(O.active||!O.configured)return;let t=!1;return T(!0),e.getAppStoreProduct().then(e=>{t||C(e)}).catch(()=>void 0).finally(()=>{t||T(!1)}),()=>{t=!0}},[e,O.active,O.configured]);let A=async()=>{if(!(w||E)){T(!0);try{C(await e.getAppStoreProduct())}catch(e){v({message:x(e)})}finally{T(!1)}}},j=async(e,n)=>{if(E)return;let a=r({message:y(e)});D(e);try{let e=await n();switch(e.status){case`cancelled`:return;case`pending`:v({message:t(`settings.membership.appStore.pending`,`The purchase is awaiting approval. After approval, use Restore Purchase to activate membership.`)});return;case`not-found`:v({message:t(`settings.membership.appStore.restoreNotFound`,`No purchase was found. Make sure you are signed in with the Apple Account used to buy membership.`)});return;case`activated`:await d(c,e.membership,{revalidate:!1}),i({message:t(`settings.membership.activated`,`Membership activated`)})}}catch(e){v({message:x(e)})}finally{D(void 0),a.dispose()}};return(0,_.jsx)(h,{status:O,account:(0,_.jsx)(m,{memberId:O.memberId}),purchaseActions:(0,_.jsxs)(_.Fragment,{children:[(0,_.jsx)(`button`,{className:n.Membership.PurchaseButton,type:`button`,"data-test-id":o.purchaseEntry,disabled:k||!O.configured||!!E||w,onClick:()=>{if(!S){A();return}j(`purchase`,()=>e.purchaseWithAppStore())},children:b(S,w)}),(0,_.jsx)(`button`,{className:n.Membership.AppStoreRestoreButton,type:`button`,"data-test-id":o.appStoreRestore,disabled:k||!O.configured||!!E,onClick:()=>void j(`restore`,()=>e.restoreAppStorePurchase()),children:t(`settings.membership.appStore.restore`,`Restore Purchase`)}),(0,_.jsx)(`p`,{className:n.Membership.AppStoreTransferNote,children:t(`settings.membership.appStore.restoreTransfer`,`Restoring transfers this purchase to the current Account ID. The previous Account ID will lose membership access.`)})]})})}function y(e){return e===`purchase`?t(`settings.membership.appStore.purchasing`,`Processing purchase...`):t(`settings.membership.appStore.restoring`,`Restoring purchase...`)}function b(e,n){return e?t(`settings.membership.appStore.buyPrice`,`Buy with App Store · {0}`,e.displayPrice):n?t(`settings.membership.appStore.loadingProduct`,`Loading App Store price...`):t(`settings.membership.appStore.reloadProduct`,`Reload App Store product`)}function x(e){let n=e&&typeof e==`object`&&`code`in e?String(e.code):void 0;return n===`PAYMENTS_NOT_ALLOWED`?t(`settings.membership.appStore.error.notAllowed`,`App Store purchases are not allowed on this device. Check Screen Time or account restrictions.`):n===`PRODUCT_NOT_FOUND`||n===`INVALID_PRODUCT_TYPE`?t(`settings.membership.appStore.error.productUnavailable`,`The App Store product is temporarily unavailable. Try again later.`):t(`settings.membership.appStore.error.purchaseFailed`,`Membership could not be activated. Check your connection and try again. Your purchase will not be lost; you can restore it later.`)}export{v as IosSettingsMembershipPage};
//# sourceMappingURL=ios-kegbzmOj.js.map