module.exports=[524741,e=>{"use strict";var t=e.i(739943);function r(t){e.A(322396).then(({logUsage:e})=>{e(t).catch(e=>{console.error("[rateLimitMiddleware] logUsage error:",e.message)})}).catch(e=>{console.error("[rateLimitMiddleware] import usageRecordsRepo error:",e.message)})}async function a(e,r){let a=r.rtpMinute??100;if(a<=0)return null;let n=await (0,t.getAdapter)(),i=new Date(Date.now()-6e4).toISOString(),o=n.get(`SELECT COUNT(*) as cnt FROM usageHistory uh
     INNER JOIN apiKeys ak ON uh.apiKey = ak.key
     WHERE ak.userId = ? AND uh.timestamp >= ?`,[e,i]);return(o?.cnt||0)>=a?{allowed:!1,reason:"Rate limit exceeded (requests per minute)"}:null}async function n(e,r){let a=r.rtpDay??1e5;if(a<=0)return null;let n=await (0,t.getAdapter)(),i=new Date,o=new Date(i.getFullYear(),i.getMonth(),i.getDate()).toISOString(),s=n.get(`SELECT COUNT(*) as cnt FROM usageHistory uh
     INNER JOIN apiKeys ak ON uh.apiKey = ak.key
     WHERE ak.userId = ? AND uh.timestamp >= ?`,[e,o]);return(s?.cnt||0)>=a?{allowed:!1,reason:"Rate limit exceeded (requests per day)"}:null}async function i(t,r){let a=r.tokenLimitMonthly??1e6;if(a<=0)return null;let{getMonthlyTokens:n}=await e.A(535635);return await n(t)>=a?{allowed:!1,reason:"Token limit exceeded (monthly)"}:null}async function o(t,r){let a=r.creditLimit??5;if(a<=0)return null;let{getMonthlyCost:n}=await e.A(535635);return await n(t)>=a?{allowed:!1,reason:"Credit/quota exceeded (monthly)"}:null}async function s(e){let r=(await (0,t.getAdapter)()).get("SELECT expiresAt FROM userPackages WHERE userId = ?",[e]);return r?.expiresAt&&new Date(r.expiresAt)<new Date?{allowed:!1,reason:"Package expired"}:null}async function l(r){if(!r)return{allowed:!0};try{let l=await (0,t.getAdapter)(),u=await l.get("SELECT id, userId FROM apiKeys WHERE key = ?",[r]);if(!u?.userId)return{allowed:!0};let d=u.userId,c=u.id,{findById:p}=await e.A(539729),w=await p(d);if(w?.role==="admin")return{allowed:!0,userId:d,apiKeyId:c};let{getEffectiveLimits:m}=await e.A(535635),y=await m(d);if(!y)return{allowed:!0,userId:d,apiKeyId:c};for(let e of[s,a,n,i,o]){let t=await e(d,y);if(t)return{...t,userId:d,apiKeyId:c}}return{allowed:!0,userId:d,apiKeyId:c}}catch(e){return console.error("[rateLimitMiddleware] checkRateLimits error:",e.message),{allowed:!0}}}async function u(t,a={}){let n=await l(t);if(n.allowed)return r({userId:n.userId,apiKeyId:n.apiKeyId,model:a.model||"unknown",inputTokens:a.inputTokens||0,outputTokens:a.outputTokens||0,totalTokens:a.totalTokens||0,cost:a.cost||0,creditUsed:a.creditUsed||0,status:"success",endpoint:a.endpoint||"/v1/chat/completions"}),null;r({userId:n.userId,apiKeyId:n.apiKeyId,model:a.model||"unknown",status:"rate_limited",errorMessage:n.reason,endpoint:a.endpoint||"/v1/chat/completions"});let{NextResponse:i}=await e.A(830068);return i.json({error:n.reason||"Rate limit exceeded"},{status:429,headers:{"Retry-After":"60"}})}e.s(["enforceRateLimits",0,u])},535635,e=>{e.v(e=>Promise.resolve().then(()=>e(618427)))},322396,e=>{e.v(t=>Promise.all(["server/chunks/src_lib_db_repos_usageRecordsRepo_0549fvr.js"].map(t=>e.l(t))).then(()=>t(224873)))},539729,e=>{e.v(e=>Promise.resolve().then(()=>e(543758)))},830068,e=>{e.v(e=>Promise.resolve().then(()=>e(955207)))}];

//# sourceMappingURL=_0svw-dn._.js.map