"use strict";
/**
 * Lightweight HTTP router with chi-style `{param}` segments.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
exports.pathRequiresAuth = pathRequiresAuth;
function parsePattern(pattern) {
    const trimmed = pattern.replace(/\/+$/, "") || "/";
    const segs = trimmed === "/" ? [""] : trimmed.split("/");
    return segs.map((seg) => {
        if (seg.startsWith("{") && seg.endsWith("}")) {
            return { kind: "param", name: seg.slice(1, -1) };
        }
        return { kind: "lit", value: seg };
    });
}
function matchParts(parts, path) {
    const normalized = path.replace(/\/+$/, "") || "/";
    const segs = normalized === "/" ? [""] : normalized.split("/");
    if (segs.length !== parts.length) {
        return null;
    }
    const params = {};
    for (let i = 0; i < parts.length; i++) {
        const part = parts[i];
        const seg = segs[i];
        if (part.kind === "lit") {
            if (part.value !== seg) {
                return null;
            }
        }
        else {
            params[part.name] = decodeURIComponent(seg);
        }
    }
    return params;
}
function pathRequiresAuth(method, path) {
    return (path.includes("/settings") ||
        path.startsWith("/getting-started") ||
        path.startsWith("/api/user") ||
        path.startsWith("/orgs/new") ||
        path.startsWith("/invites/") ||
        (method !== "GET" &&
            (path.includes("/owners") ||
                path.includes("/members") ||
                path.includes("/teams") ||
                path.includes("/transfer") ||
                path.startsWith("/api/v1/orgs") ||
                path.startsWith("/orgs/"))) ||
        path === "/api/v1/publish" ||
        path === "/api/v1/trusted-publishing/token" ||
        path.startsWith("/admin") ||
        path.includes("/yank") ||
        path.includes("/unyank") ||
        path.includes("/unpublish"));
}
class Router {
    routes = [];
    notFoundHandler = null;
    get routeCount() {
        return this.routes.length;
    }
    add(method, pattern, handler) {
        this.routes.push({
            method,
            pattern,
            parts: parsePattern(pattern),
            handler,
            requireAuth: pathRequiresAuth(method, pattern),
        });
    }
    match(method, path) {
        const m = method.toUpperCase();
        for (const route of this.routes) {
            if (route.method !== m) {
                continue;
            }
            const params = matchParts(route.parts, path);
            if (params) {
                return { params, handler: route.handler, requireAuth: route.requireAuth };
            }
        }
        return null;
    }
}
exports.Router = Router;
//# sourceMappingURL=router.js.map