1import{v as le,u as me,r as i,s as g,w as E,j as e,T as ue,m as xe,p as pe,q as V,x as he,k as ge,i as fe,y as be,t as b}from"./index-DQTLgvys.js";import{g as ye,d as je,a as Ne,C as w,f as v,s as we}from"./currency-ByZW2OTY.js";import{S as W}from"./StripeCheckoutDialog-dfUivYlm.js";import{g as B,i as ve,c as _e}from"./stripe-GsQ57ucg.js";import{u as ke}from"./useServerFn-CkNDMP0X.js";import{c as Se,r as Ce}from"./auth-middleware-pmW4nbpj.js";import"./index-PMoOVIPN.js";const Pe=["individual","team","business"],$e=["gbp","usd","eur"],Ee=[];for(const o of Pe)for(const r of $e)Ee.push(`${o}_monthly_${r}_v2`,`${o}_annual_${r}`);const Be=Se({method:"POST"}).middleware([Ce]).handler(le("55fc488f04e7ac4ebbf3c61fdd31ccdc1852e56892a314f4c787ca977c64c162"));function Te(o){return o.seatLimit==="unlimited"?"Unlimited seats":o.seatLimit===1?"1 seat":`Up to ${o.seatLimit} seats`}function De(o){return o.monthlyFreeCredits>0?`${o.monthlyFreeCredits} free credits per month`:"Custom monthly allocation"}function Me(){const{user:o,org:r,orgLoading:f,personalCredits:_,refreshPersonalCredits:T}=me(),z=!!r&&r.role==="member",[D,k]=i.useState(null),[I,ee]=i.useState([]),[n,y]=i.useState("USD"),[Ie,te]=i.useState({}),[re,F]=i.useState(null),[se,R]=i.useState(null),[u,A]=i.useState(null),[S,L]=i.useState(null),[O,U]=i.useState([]),[q,M]=i.useState(!1),[x,ae]=i.useState("annual"),[j,Y]=i.useState(null),[G,H]=i.useState(null),[C,P]=i.useState(null),[K,$]=i.useState(null),[X,N]=i.useState(!1),Z=ke(Be);i.useEffect(()=>{const t=ye();if(t){y(t);return}y(je());let s=!1;return Ne().then(d=>{s||y(d)}),()=>{s=!0}},[]);const J=t=>{we(t),y(t)};i.useEffect(()=>{g.from("credit_pack_prices_public").select("pack_id, currency, price").eq("currency",n).then(({data:t})=>{const s={};(t??[]).forEach(d=>{d.pack_id&&(s[d.pack_id]=Number(d.price??0))}),te(s)})},[n]),i.useEffect(()=>{if(o){if(f){k(null);return}k(r?r.creditBalance:_),(async()=>{let t=r?.id??null,s=r?r.role==="owner"||r.role==="admin":!1;if(!t){const{data:m}=await g.from("organizations").select("id").eq("owner_id",o.id).maybeSingle();m?.id&&(t=m.id,s=!0)}if(!t){A(null),L(null),Y(null),H(null),U([]);return}const{data:d}=await g.from("organizations").select("tier, subscription_status").eq("id",t).maybeSingle();A(d?.tier??null),L(d?.subscription_status??null);const{data:a}=await g.from("org_subscriptions").select("price_id, current_period_end, created_at, pending_price_id, pending_change_at").eq("org_id",t).eq("environment",B()).order("created_at",{ascending:!1}).limit(1).maybeSingle(),l=a?.price_id?E(a.price_id):null;if(Y(l?.interval??(a?"monthly":null)),H(a?.current_period_end??null),P(a?.pending_price_id??null),$(a?.pending_change_at??null),s){const{data:m}=await g.rpc("org_credit_lot_summary",{_org_id:t});U(m??[])}})(),Promise.all([g.from("payments").select("id, amount, credits_added, created_at").eq("user_id",o.id),g.from("scans").select("id, target_handle, created_at").eq("user_id",o.id),g.from("personal_credit_ledger").select("id, change, reason, created_at").eq("user_id",o.id)]).then(([t,s,d])=>{const a=(t.data??[]).map(c=>({kind:"purchase",id:c.id,created_at:c.created_at,credits_added:c.credits_added,amount:Number(c.amount)})),l=(s.data??[]).map(c=>({kind:"scan",id:c.id,created_at:c.created_at,target_handle:c.target_handle})),m=(d?.data??[]).map(c=>({kind:"personal",id:c.id,created_at:c.created_at,change:c.change,reason:c.reason})),h=[...a,...l,...m].sort((c,ce)=>new Date(ce.created_at).getTime()-new Date(c.created_at).getTime());ee(h)})}},[o,r,f,_]);const Q=!r||r.role==="owner",p=S==="active"||S==="past_due",ne=!r||r.role==="owner",ie=async()=>{M(!0);try{const t=await _e({returnUrl:`${window.location.origin}/billing`});window.open(t,"_blank","noopener,noreferrer")}catch(t){b.error(t?.message??"Could not open billing portal")}finally{M(!1)}},de=async t=>{const s=E(t),d={individual:1,team:2,business:3};if(!(s&&u&&(d[s.tier]<d[u]||s.tier===u&&s.interval==="monthly"&&j==="annual")&&!window.confirm("This change takes effect at your next renewal. You keep your current plan and credits until then. Continue?"))){N(!0);try{const l=await Z({data:{action:"change",targetPriceId:t,environment:B(),returnUrl:`${window.location.origin}/billing`}});if(l?.error)throw new Error(l.error);l.kind==="upgrade"?window.open(l.url,"_blank","noopener,noreferrer"):(P(l.pendingPriceId),$(l.changeAt),b.success("Plan change booked for your renewal date"))}catch(l){b.error(l?.message??"Could not change plan")}finally{N(!1)}}},oe=async()=>{N(!0);try{const t=await Z({data:{action:"cancel_pending",environment:B()}});if(t?.error)throw new Error(t.error);P(null),$(null),b.success("You'll stay on your current plan")}catch(t){b.error(t?.message??"Could not cancel the change")}finally{N(!1)}};return e.jsx("div",{className:"mx-auto max-w-5xl px-4 py-8 sm:px-6 sm:py-12",children:z?e.jsxs("div",{className:"text-center py-16",children:[e.jsx("div",{className:"text-xs uppercase tracking-[0.4em] text-gold",children:"Restricted"}),e.jsx("h1",{className:"font-display text-3xl mt-2",children:"Billing is managed by your organisation owner"}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-4",children:["Only the owner of ",r?.name??"your organisation"," can view billing, change the plan, or purchase credits."]})]}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"text-xs uppercase tracking-[0.4em] text-gold",children:"Account"}),e.jsx("h1",{className:"font-display text-4xl mt-1",children:"Billing"}),e.jsx("div",{className:"gold-rule my-8 max-w-[80px]"}),e.jsxs("div",{className:"flex flex-col items-start gap-5 border border-gold/40 bg-surface p-5 sm:p-8 md:flex-row md:items-center md:justify-between",children:[e.jsxs("div",{children:[e.jsx("div",{className:"text-xs uppercase tracking-widest text-muted-foreground",children:f?"Balance":r?"Organisation balance":"Current balance"}),e.jsx("div",{className:"mt-1 font-display text-5xl text-gold sm:text-6xl",children:f||D===null?e.jsx("span",{className:"text-muted-foreground animate-pulse",children:"â¦"}):D}),e.jsx("div",{className:"text-sm text-muted-foreground mt-1",children:f?"Resolving accountâ¦":r?`${r.name} · shared credit pool`:"credits available · 1 scan = 1 credit"})]}),e.jsxs("div",{className:"text-left text-xs uppercase tracking-widest text-muted-foreground md:text-right",children:["Purchased top-up credits never expire.",e.jsx("br",{}),"Monthly plan credits reset each billing cycle."]})]}),!f&&r&&e.jsxs("div",{className:"mt-4 flex flex-col items-start gap-5 border border-border bg-surface p-5 sm:p-6 md:flex-row md:items-center md:justify-between md:gap-6",children:[e.jsxs("div",{children:[e.jsx("div",{className:"text-xs uppercase tracking-widest text-gold",children:"Your personal credits â only you can use these"}),e.jsx("div",{className:"font-display text-4xl text-offwhite mt-1",children:_}),e.jsx("div",{className:"text-sm text-muted-foreground mt-1",children:"Reserved pay-as-you-go credits. They never expire and your teammates cannot spend them."})]}),e.jsxs("div",{className:"text-left text-xs uppercase tracking-widest text-muted-foreground md:text-right",children:["Used only when the organisation",e.jsx("br",{}),"pool is empty, and only with",e.jsx("br",{}),"your confirmation."]})]}),!Q&&e.jsxs("div",{className:"mt-8 border border-border bg-surface p-5 text-sm text-muted-foreground",children:[e.jsx("span",{className:"text-gold uppercase tracking-widest text-xs mr-2",children:"Note"}),"Only the organisation owner can purchase credits. Ask the owner of"," ",e.jsx("span",{className:"text-offwhite",children:r?.name})," to top up the shared pool."]}),r&&(r.role==="owner"||r.role==="admin")&&O.length>0&&e.jsxs("div",{className:"mt-8 border border-border bg-surface p-6",children:[e.jsx("div",{className:"text-xs uppercase tracking-[0.4em] text-gold mb-3",children:"Credit breakdown"}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"text-left text-xs uppercase tracking-widest text-muted-foreground border-b border-border",children:[e.jsx("th",{className:"py-2 font-normal",children:"Type"}),e.jsx("th",{className:"py-2 font-normal",children:"Remaining"}),e.jsx("th",{className:"py-2 font-normal",children:"Expires"})]})}),e.jsx("tbody",{children:O.map((t,s)=>e.jsxs("tr",{className:"border-b border-border last:border-0",children:[e.jsx("td",{className:"py-2 text-offwhite capitalize",children:t.type==="free_monthly"?"Free monthly":t.type==="paid_pack"?"Purchased":t.type}),e.jsx("td",{className:"py-2 text-gold",children:t.remaining_amount}),e.jsx("td",{className:"py-2 text-muted-foreground",children:t.expires_at?new Date(t.expires_at).toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"}):"Never"})]},s))})]})}),e.jsx("p",{className:"text-xs text-muted-foreground mt-3",children:"Monthly plan credits reset each billing cycle â unused credits do not roll forward. Purchased top-up credits never expire. Lower-expiry credits are spent first."})]}),ne&&e.jsxs(e.Fragment,{children:[e.jsx("h2",{className:"font-display text-2xl mt-16 mb-2",children:"Subscription"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-6",children:p?e.jsxs(e.Fragment,{children:["Current plan: ",e.jsx("span",{className:"text-offwhite uppercase tracking-widest text-xs",children:u}),j&&e.jsx("span",{className:"ml-3 text-muted-foreground text-xs uppercase tracking-widest",children:j==="annual"?"Billed annually":"Billed monthly"}),G&&e.jsxs("span",{className:"ml-3 text-muted-foreground text-xs",children:["Renews"," ",new Date(G).toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"})]}),S==="past_due"&&e.jsx("span",{className:"ml-3 text-destructive uppercase tracking-widest text-xs",children:"Payment past due"}),e.jsx("button",{type:"button",onClick:ie,disabled:q,className:"ml-4 underline text-gold text-xs uppercase tracking-widest disabled:opacity-50",children:q?"Openingâ¦":"Manage subscription"})]}):"Choose a plan to unlock monthly credits and discounted top-ups. Team and Business also unlock shared seats."}),p&&C&&e.jsxs("div",{className:"mb-6 border border-gold/60 px-4 py-3 text-sm flex flex-wrap items-center gap-3",children:[e.jsxs("span",{children:["Switching to"," ",e.jsx("span",{className:"uppercase tracking-widest text-xs text-offwhite",children:(()=>{const t=E(C);return t?`${t.tier}
1 · ${t.interval==="annual"?"annual":"monthly"}`:C})()}),K&&` on ${new Date(K).toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"})}`]}),e.jsx("button",{type:"button",onClick:oe,disabled:X,className:"underline text-gold text-xs uppercase tracking-widest disabled:opacity-50",children:"Keep current plan"})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3 mb-3",children:[e.jsx("span",{className:"text-xs uppercase tracking-widest text-muted-foreground",children:"Currency"}),e.jsx("div",{className:"flex border border-border",children:["USD","GBP","EUR"].map(t=>e.jsxs("button",{onClick:()=>J(t),className:`px-3 py-1.5 text-xs uppercase tracking-widest transition-colors ${n===t?"bg-gold text-primary-foreground":"text-muted-foreground hover:text-offwhite"}`,children:[t," ",w[t]]},t))})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3 mb-6",children:[e.jsx("span",{className:"text-xs uppercase tracking-widest text-muted-foreground",children:"Term"}),e.jsx("div",{className:"flex border border-border",children:[["annual","Annual"],["monthly","Monthly"]].map(([t,s])=>e.jsx("button",{onClick:()=>ae(t),className:`px-3 py-1.5 text-xs uppercase tracking-widest transition-colors ${x===t?"bg-gold text-primary-foreground":"text-muted-foreground hover:text-offwhite"}`,children:s},t))}),x==="annual"&&e.jsx("span",{className:"text-[10px] uppercase tracking-[0.3em] text-gold",children:"2 months free"})]}),e.jsx("div",{className:"grid md:grid-cols-2 lg:grid-cols-4 gap-4",children:ue.map(t=>{const s=p&&u===t.id&&(j??"monthly")===x,d=xe(t,n,x),a=pe(t,n,x);return e.jsxs("div",{className:`border bg-surface p-6 flex flex-col ${s?"border-gold":"border-border"}`,children:[s&&e.jsx("div",{className:"text-[10px] uppercase tracking-[0.3em] text-gold mb-2",children:"Current plan"}),e.jsx("div",{className:"font-display text-3xl text-offwhite",children:t.name}),e.jsx("div",{className:"font-display text-2xl text-gold mt-2",children:t.id==="corporate"?"Custom":d!==void 0?`${v(d,n)} / mo`:"â"}),t.id!=="corporate"&&a!==void 0&&e.jsxs("div",{className:"text-[10px] text-muted-foreground font-sans normal-case tracking-normal mt-1",children:[v(a,n)," per credit"]}),e.jsxs("ul",{className:"text-sm text-muted-foreground mt-4 space-y-1 flex-1",children:[e.jsx("li",{children:Te(t)}),e.jsx("li",{children:De(t)}),e.jsx("li",{className:"mt-2 text-xs",children:t.blurb})]}),t.id==="corporate"?e.jsx("a",{href:"mailto:[email protected]?subject=Argus%20X%20Corporate%20enquiry",className:"mt-6 py-3 text-center border border-gold text-gold uppercase tracking-widest text-sm hover:bg-gold hover:text-primary-foreground transition-colors",children:"Contact sales"}):s?e.jsx("button",{disabled:!0,className:"mt-6 py-3 uppercase tracking-widest text-sm border border-border text-muted-foreground cursor-not-allowed",children:"Current plan"}):e.jsx("button",{disabled:X,onClick:()=>p?de(V(t.id,n,x)):R(V(t.id,n,x)),className:"mt-6 py-3 uppercase tracking-widest text-sm bg-gold text-primary-foreground hover:opacity-90 transition-opacity disabled:opacity-50",children:p?"Switch plan":"Subscribe"})]},t.id)})}),e.jsx("p",{className:"text-xs text-muted-foreground mt-4 uppercase tracking-widest",children:x==="annual"?`Billed once per year in ${n}. Plan credits are released monthly and reset each month â unused monthly credits do not roll forward. Purchased top-up credits never expire.`:`Billed monthly in ${n}. Credits are granted at the start of each billing period.`})]}),Q&&e.jsxs(e.Fragment,{children:[e.jsx("h2",{className:"font-display text-2xl mt-16 mb-6",children:r?n==="GBP"?"Purchase Credits for Organisation":"Purchase Credits for Organization":"Purchase Credits"}),e.jsxs("div",{className:"flex items-center gap-3 mb-6",children:[e.jsx("span",{className:"text-xs uppercase tracking-widest text-muted-foreground",children:"Currency"}),e.jsx("div",{className:"flex border border-border",children:["USD","GBP","EUR"].map(t=>e.jsxs("button",{onClick:()=>J(t),className:`px-3 py-1.5 text-xs uppercase tracking-widest transition-colors ${n===t?"bg-gold text-primary-foreground":"text-muted-foreground hover:text-offwhite"}`,children:[t," ",w[t]]},t))})]}),(()=>{const t=p&&u&&u!=="corporate"?u:"payg",s=he(t),d=t==="payg"?"Pay-as-you-go":ge(t).name;return e.jsxs(e.Fragment,{children:[!p&&e.jsx("p",{className:"text-xs text-muted-foreground mb-4",children:"Showing pay-as-you-go pricing. Subscribe to a plan below to unlock discounted top-ups."}),e.jsx("div",{className:"grid md:grid-cols-3 gap-4",children:fe.map((a,l)=>{const m=a===5,h=s[a][n];return e.jsxs("div",{className:`border bg-surface p-6 flex flex-col ${m?"border-gold":"border-border"}`,children:[m&&e.jsx("div",{className:"text-[10px] uppercase tracking-[0.3em] text-gold mb-2",children:"Recommended"}),e.jsxs("div",{className:"font-display text-5xl text-offwhite mt-2",children:[a,e.jsxs("span",{className:"text-base text-muted-foreground ml-2",children:["credit",a>1?"s":""]})]}),e.jsx("div",{className:"font-display text-2xl text-gold mt-2",children:h!==void 0?v(h,n):`${w[n]}â`}),a>1&&h!==void 0&&e.jsxs("div",{className:"text-xs text-muted-foreground mt-1",children:[v(h/a,n)," per credit"]}),e.jsx("p",{className:"text-sm text-muted-foreground mt-3 flex-1",children:p?`${d} plan rate.`:" "}),e.jsx("button",{disabled:h===void 0,onClick:()=>F(be(t,a,n)),className:`mt-6 py-3 uppercase tracking-widest text-sm transition-opacity ${m?"bg-gold text-primary-foreground hover:opacity-90":"border border-gold text-gold hover:bg-gold hover:text-primary-foreground"} disabled:opacity-40 disabled:cursor-not-allowed`,children:h===void 0?"Unavailable":"Buy now"})]},a)})})]})})(),e.jsxs("p",{className:"text-xs text-muted-foreground mt-4 uppercase tracking-widest",children:["Prices shown in ",n," (",w[n],").",ve()&&" · Test mode â use 4242 4242 4242 4242"]})]}),I.length>0&&e.jsxs(e.Fragment,{children:[e.jsx("h2",{className:"font-display text-2xl mt-16 mb-6",children:"History"}),e.jsx("div",{className:"overflow-x-auto border border-border bg-surface",children:e.jsxs("table",{className:"w-full min-w-[680px]",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-border text-xs uppercase tracking-widest text-muted-foreground",children:[e.jsx("th",{className:"text-left p-4 font-normal",children:"Date"}),e.jsx("th",{className:"text-left p-4 font-normal",children:"Activity"}),e.jsx("th",{className:"text-left p-4 font-normal",children:"Details"}),e.jsx("th",{className:"text-left p-4 font-normal",children:"Credits"}),e.jsx("th",{className:"text-right p-4 font-normal",children:"Amount"})]})}),e.jsx("tbody",{children:I.map(t=>e.jsxs("tr",{className:"border-b border-border last:border-0",children:[e.jsx("td",{className:"p-4 text-sm",children:new Date(t.created_at).toLocaleDateString()}),t.kind==="purchase"?e.jsxs(e.Fragment,{children:[e.jsx("td",{className:"p-4 text-sm",children:"Credit purchase"}),e.jsx("td",{className:"p-4 text-sm text-muted-foreground",children:"â"}),e.jsxs("td",{className:"p-4 text-sm text-gold",children:["+",t.credits_added]}),e.jsxs("td",{className:"p-4 text-right text-sm",children:["$",t.amount.toFixed(2)]})]}):t.kind==="scan"?e.jsxs(e.Fragment,{children:[e.jsx("td",{className:"p-4 text-sm",children:"Scan"}),e.jsxs("td",{className:"p-4 text-sm text-muted-foreground",children:["@",t.target_handle]}),e.jsx("td",{className:"p-4 text-sm text-destructive",children:"â1"}),e.jsx("td",{className:"p-4 text-right text-sm text-muted-foreground",children:"â"})]}):e.jsxs(e.Fragment,{children:[e.jsxs("td",{className:"p-4 text-sm",children:["Personal credit",e.jsx("span",{className:"ml-2 text-[10px] uppercase tracking-widest text-gold",children:"Personal"})]}),e.jsx("td",{className:"p-4 text-sm text-muted-foreground",children:t.reason}),e.jsx("td",{className:`p-4 text-sm ${t.change<0?"text-destructive":"text-gold"}`,children:t.change>0?`+${t.change}`:t.change}),e.jsx("td",{className:"p-4 text-right text-sm text-muted-foreground",children:"â"})]})]},`${t.kind}-${t.id}`))})]})})]}),e.jsx(W,{priceId:re,onClose:()=>{F(null),T()}}),e.jsx(W,{priceId:se,mode:"subscription",onClose:()=>{R(null),T()}})]})})}export{Me as component};
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.