"use strict";var m=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var O=Object.prototype.hasOwnProperty;var W=(u,t)=>{for(var r in t)m(u,r,{get:t[r],enumerable:!0})},E=(u,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of k(t))!O.call(u,e)&&e!==r&&m(u,e,{get:()=>t[e],enumerable:!(s=v(t,e))||s.enumerable});return u};var N=u=>E(m({},"__esModule",{value:!0}),u);var j={};W(j,{Middleware:()=>P,default:()=>b});module.exports=N(j);var l=require("next/server"),P=class{fetch;rules;authPaths;onError;constructor(t){this.fetch=t.fetch,this.rules=t.rules,this.authPaths=t.authPaths,this.onError=t.onError}extractParams(t,r){let s=t.split("/"),e=r.split("/");return s.reduce((a,i,n)=>{if(i.startsWith("[")&&i.endsWith("]")){let o=i.slice(1,-1);return{...a,[o]:e[n]}}return a},{})}isWildcard(t){return t.endsWith("/*")}match(t,r){let s=t.split("/"),e=r.split("/");return s.every((a,i)=>a==="*"||a.startsWith("[")&&a.endsWith("]")?!0:a===e[i])}sortPaths(t){return t.sort((r,s)=>this.isWildcard(r)&&!this.isWildcard(s)?1:-1)}async apply(t,r,s){let a=Object.keys(this.rules).filter(n=>this.match(n,r)),i=this.sortPaths(a);for(let n of i){let o=this.rules[n]||[],x=this.extractParams(n,r),d=!1,h,y=async()=>{d=!0},g=(f,c)=>{if(h=f,c){if("params"in c){let p=c.params;h=f.split("/").map(R=>{if(R.startsWith("[")&&R.endsWith("]")){let w=R.slice(1,-1);return p[w]}return R}).join("/")}if(c.query){let p=new URLSearchParams(c.query);h+=`?${p.toString()}`}c.hash&&(h+=`#${c.hash}`)}};for(let f of o)if(await f({data:s,params:x,next:y,redirect:g,path:new URL(t.url).pathname}),h)break;return!d&&!h?l.NextResponse.next():h&&h!==new URL(t.url).pathname?l.NextResponse.redirect(new URL(h,t.url)):l.NextResponse.next()}}async handle(t,r){let s=new URL(t.url).pathname;if(this.authPaths.some(e=>{let a=e.split("/"),i=s.split("/");return a.every((n,o)=>n==="*"||n.startsWith("[")&&n.endsWith("]")?!0:n===i[o])}))try{let e=await this.fetch(t);if(!e)throw"Missing data from `fetch`";return await this.apply(t,s,e)}catch{if(this.onError)return await this.onError(t)}else return l.NextResponse.next()}},b=P;0&&(module.exports={Middleware}); //# sourceMappingURL=index.cjs.map