1import{u as M,a as A,j as e}from"./query-BfvAKYUz.js";import{e as $,f as R,r as o}from"./react-vendor-BTIQH4FS.js";import{u as Q,B as E,s as m,t as h}from"./index-DK8CeD3M.js";import{u as H,H as U}from"./Header-BpKcuf2S.js";import{S as Y}from"./SEO-BoCqCqkv.js";import{x as D,S as O,Z as u,f as y}from"./icons-B_WIxKAl.js";import"./radix-DIIfJIjL.js";import"./supabase-CXidNrjy.js";import"./charts-z6ppYIt6.js";const a="#1A1A1A",b="#4F3AD9",f="#F5B915",G="#FEF9F0",re=()=>{var _,L,P,K;const n=$(),{user:i,loading:g}=Q(),l=H(),p=M(),[k,j]=R(),[N,w]=o.useState(null),[T,x]=o.useState(!1),v=k.get("status"),c=k.get("ref");o.useEffect(()=>{!g&&!i&&n("/")},[g,i,n]);const{data:C=[],isLoading:B}=A({queryKey:["topup-packages"],queryFn:async()=>{const{data:s,error:r}=await m.from("payment_products").select("id, product_key, label, credit_value, price_myr").eq("kind","credits").eq("is_active",!0).order("display_order",{ascending:!0});if(r)throw r;return s??[]},enabled:!!i});o.useEffect(()=>{if(v!=="success"||!c||!i)return;let s=!1;x(!0);const r=async(t=0)=>{if(s)return;const{data:d}=await m.from("payment_intents").select("status, credits_granted").eq("external_ref",c).maybeSingle();if((d==null?void 0:d.status)==="claimed"){p.invalidateQueries({queryKey:["story-usage",i.id]}),p.invalidateQueries({queryKey:["profile",i.id]}),h({title:"Bayaran berjaya! ð",description:`${d.credits_granted??0} cerita telah ditambah ke akaun anda.`}),x(!1),j({},{replace:!0});return}if(t===2||t===6)try{await m.functions.invoke("verify-topup",{body:{reference:c}})}catch{}if(t>=10){x(!1),h({title:"Bayaran sedang diproses",description:"Kredit akan masuk sebentar lagi. Muat semula halaman ini."});return}setTimeout(()=>r(t+1),2e3)};return r(),()=>{s=!0}},[v,c,i,p,j]);const F=async s=>{w(s.product_key);try{const{data:r,error:t}=await m.functions.invoke("create-topup",{body:{productKey:s.product_key}});if(t||!(r!=null&&r.url))throw new Error((r==null?void 0:r.error)??(t==null?void 0:t.message)??"Gagal");window.location.href=r.url}catch(r){h({title:"Ralat",description:(r==null?void 0:r.message)??"Tidak dapat membuka halaman bayaran.",variant:"destructive"}),w(null)}},S=((_=l.usage)==null?void 0:_.isPremium)===!0||((L=l.usage)==null?void 0:L.tier)==="silver"||((P=l.usage)==null?void 0:P.tier)==="gold",q=o.useMemo(()=>{var s;return l.isLoading?"â¦":Number(((s=l.usage)==null?void 0:s.remaining)??0).toLocaleString("ms-MY")},[l.isLoading,(K=l.usage)==null?void 0:K.remaining]);return e.jsxs("div",{className:"min-h-screen",style:{backgroundColor:G},children:[e.jsx(Y,{title:"Topup Kredit Cerita â CeritaNilam",description:"Tambah jumlah cerita anda dengan topup pantas. Bayar sekali, kredit terus masuk.",path:"/topup"}),e.jsx(U,{}),e.jsxs("main",{className:"max-w-4xl mx-auto px-4 py-10 space-y-8",children:[e.jsxs("button",{onClick:()=>n(-1),className:"inline-flex items-center gap-2 font-semibold",style:{color:a},children:[e.jsx(D,{className:"h-4 w-4"})," Kembali"]}),e.jsxs("div",{className:"rounded-2xl border-4 p-6 sm:p-8",style:{borderColor:a,backgroundColor:f,boxShadow:`6px 6px 0 ${a}`},children:[e.jsx("h1",{className:"font-fredoka text-3xl sm:text-4xl font-bold",style:{color:b},children:"Topup Kredit Cerita"}),e.jsx("p",{className:"mt-2 font-semibold",style:{color:a},children:"Pilih pakej, buat bayaran, dan kredit cerita anda bertambah serta-merta."}),S&&e.jsxs("div",{className:"mt-4 inline-flex items-center gap-2 rounded-xl border-2 bg-white px-4 py-2 font-bold",style:{borderColor:a,color:a},children:[e.jsx(O,{className:"h-4 w-4"})," Baki kredit anda: ",q," cerita"]})]}),T&&e.jsxs("div",{className:"rounded-2xl border-4 p-5 flex items-center gap-3 bg-white",style:{borderColor:a,boxShadow:`6px 6px 0 ${a}`},children:[e.jsx(u,{className:"h-5 w-5 animate-spin"}),e.jsx("span",{className:"font-semibold",children:"Mengesahkan bayaran andaâ¦"})]}),l.isLoading?e.jsx("div",{className:"py-16 flex justify-center",children:e.jsx(u,{className:"h-8 w-8 animate-spin"})}):S?B?e.jsx("div",{className:"py-16 flex justify-center",children:e.jsx(u,{className:"h-8 w-8 animate-spin"})}):C.length===0?e.jsx("div",{className:"rounded-2xl border-4 bg-white p-8 text-center",style:{borderColor:a,boxSh
1adow:`6px 6px 0 ${a}`},children:e.jsx("p",{className:"font-semibold",children:"Pakej topup belum tersedia. Sila cuba sebentar lagi."})}):e.jsx("div",{className:"grid sm:grid-cols-2 lg:grid-cols-3 gap-5",children:C.map(s=>e.jsxs("div",{className:"rounded-2xl border-4 bg-white p-6 flex flex-col",style:{borderColor:a,boxShadow:`6px 6px 0 ${a}`},children:[e.jsx("h2",{className:"font-fredoka text-2xl font-bold",style:{color:b},children:s.label}),e.jsxs("p",{className:"mt-1 font-semibold",style:{color:a},children:["+",Number(s.credit_value??0).toLocaleString("ms-MY")," cerita"]}),e.jsxs("p",{className:"mt-4 font-fredoka text-3xl font-bold",style:{color:a},children:["RM ",Number(s.price_myr).toFixed(2)]}),e.jsxs("ul",{className:"mt-4 space-y-2 text-sm flex-1",style:{color:a},children:[e.jsxs("li",{className:"flex gap-2",children:[e.jsx(y,{className:"h-4 w-4 mt-0.5 shrink-0"})," Bayaran sekali, tiada langganan"]}),e.jsxs("li",{className:"flex gap-2",children:[e.jsx(y,{className:"h-4 w-4 mt-0.5 shrink-0"})," Kredit masuk automatik"]}),e.jsxs("li",{className:"flex gap-2",children:[e.jsx(y,{className:"h-4 w-4 mt-0.5 shrink-0"})," Tiada tarikh luput"]})]}),e.jsx(E,{onClick:()=>F(s),disabled:N!==null,className:"mt-6 w-full rounded-xl border-4 font-bold h-12",style:{borderColor:a,backgroundColor:f,color:a},children:N===s.product_key?e.jsxs(e.Fragment,{children:[e.jsx(u,{className:"h-4 w-4 animate-spin mr-2"})," Menyediakanâ¦"]}):"Topup Sekarang"})]},s.id))}):e.jsxs("div",{className:"rounded-2xl border-4 bg-white p-8 text-center space-y-4",style:{borderColor:a,boxShadow:`6px 6px 0 ${a}`},children:[e.jsx("h2",{className:"font-fredoka text-2xl font-bold",style:{color:b},children:"Topup untuk pengguna Silver"}),e.jsx("p",{className:"font-semibold",style:{color:a},children:"Topup kredit hanya tersedia selepas anda memiliki pakej Silver. Dapatkan pakej Silver dahulu untuk mula membaca."}),e.jsx(E,{onClick:()=>n("/harga"),className:"rounded-xl border-4 font-bold h-12 px-6",style:{borderColor:a,backgroundColor:f,color:a},children:"Lihat Pakej Silver"})]}),e.jsx("p",{className:"text-sm text-center",style:{color:a},children:"Bayaran diproses dengan selamat melalui HitPay (FPX, kad kredit/debit, e-wallet)."})]})]})};export{re as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.