1import{b as C,a as T,N as k,a2 as S,af as u,s as i}from"./index-tQjrzw8f.js";import{r as D}from"./vendor-react-DTP8y_xJ.js";const F=()=>{const{profile:a}=C(),{toast:l}=T();k(),S();const[g,n]=D.useState(!1),{data:r,isLoading:_}=u({queryKey:["ai-credits",a==null?void 0:a.org_id],queryFn:async()=>{if(!(a!=null&&a.org_id))return null;const{data:e,error:t}=await i.from("ai_credits").select("*").eq("org_id",a.org_id).single();if(t&&t.code!=="PGRST116")throw t;return e},enabled:!!(a!=null&&a.org_id)}),{data:y=[],isLoading:h}=u({queryKey:["ai-credit-transactions",a==null?void 0:a.org_id],queryFn:async()=>{if(!(a!=null&&a.org_id))return[];const{data:e,error:t}=await i.from("ai_credit_transactions").select("*").eq("org_id",a.org_id).order("created_at",{ascending:!1}).limit(50);if(t)throw t;return e},enabled:!!(a!=null&&a.org_id)}),m=async e=>{try{const{data:t,error:s}=await i.functions.invoke("purchase-credits",{body:{packageId:e}});if(s)throw s;t!=null&&t.url&&window.open(t.url,"_blank")}catch(t){l({title:"Purchase Failed",description:t.message||"Failed to initiate purchase",variant:"destructive"})}},w=e=>((r==null?void 0:r.balance)||0)>=e,o=(r==null?void 0:r.balance)||0;return{credits:r,transactions:y,balance:o,isLoadingCredits:_,isLoadingTransactions:h,purchaseCreditsViaStripe:m,hasSufficientCredits:w,checkCreditsAndGate:(e=1)=>o<e?(n(!0),!1):!0,checkDailyLimit:async()=>{if(!(a!=null&&a.org_id))return{allowed:!1,message:"User not authenticated"};const{data:e}=await i.from("subscriptions").select("plan").eq("org_id",a.org_id).single(),t=(e==null?void 0:e.plan)||"free",{data:s}=await i.from("plan_limits").select("ai_credits_daily").eq("plan",t).single();if(!(s!=null&&s.ai_credits_daily))return{allowed:!0};const c=new Date().toISOString().split("T")[0],{data:f}=await i.from("ai_credit_transactions").select("amount").eq("org_id",a.org_id).eq("transaction_type","usage").gte("created_at",`${c}T00:00:00`).lte("created_at",`${c}T23:59:59`),d=Math.abs((f||[]).reduce((b,q)=>b+q.amount,0));return d>=s.ai_credits_daily?{allowed:!1,message:`Daily AI credit limit reached (${d}/${s.ai_credits_daily}). Resets at midnight.`}:{allowed:!0}},showNoCreditsDialog:g,setShowNoCreditsDialog:n}};export{F as u};
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.