import { de as ensurePluginRuntimeInitialized, dL as optionalAuth, c as createError, bZ as PolicyRegistry, j as getQuery, f8 as PolicyDeniedError, bX as RouteRegistry, f9 as pluginSlug, q as readBody, d as defineEventHandler } from '../../../nitro/nitro.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

function normalizePath(path) {
  if (!path || path === "/") {
    return "/";
  }
  const trimmed = path.replace(/^\/+|\/+$/g, "");
  return trimmed ? `/${trimmed}` : "/";
}
function splitSegments(path) {
  return normalizePath(path).replace(/^\//, "").split("/").filter(Boolean);
}
function decodeRouteSegment(segment) {
  try {
    return decodeURIComponent(segment);
  } catch {
    return null;
  }
}
function isUnsafeRouteSegment(segment) {
  const decoded = decodeRouteSegment(segment);
  if (decoded === null) {
    return true;
  }
  return decoded === "." || decoded === ".." || decoded.includes("/") || decoded.includes("\\");
}
function matchPluginRoutePath(routePath, requestPath) {
  var _a;
  const routeSegments = splitSegments(routePath);
  const requestSegments = splitSegments(requestPath);
  if (routeSegments.length === 0 && requestSegments.length === 0) {
    return {};
  }
  const params = {};
  for (let index = 0; index < routeSegments.length; index += 1) {
    const routeSegment = routeSegments[index];
    const requestSegment = requestSegments[index];
    if (routeSegment === "*") {
      const rest = requestSegments.slice(index);
      if (rest.some(isUnsafeRouteSegment)) {
        return null;
      }
      params["*"] = rest.map((segment) => {
        var _a2;
        return (_a2 = decodeRouteSegment(segment)) != null ? _a2 : segment;
      }).join("/");
      return params;
    }
    if (requestSegment === void 0) {
      return null;
    }
    if (routeSegment.startsWith(":")) {
      if (isUnsafeRouteSegment(requestSegment)) {
        return null;
      }
      params[routeSegment.slice(1)] = (_a = decodeRouteSegment(requestSegment)) != null ? _a : requestSegment;
      continue;
    }
    if (routeSegment !== requestSegment) {
      return null;
    }
  }
  if (requestSegments.length !== routeSegments.length) {
    return null;
  }
  return params;
}
function buildHeaders(event) {
  const headers = {};
  for (const [key, value] of Object.entries(event.node.req.headers)) {
    headers[key] = value;
  }
  return headers;
}
function buildPolicyContext(event, method, params) {
  return {
    path: event.path || event.node.req.url || "/",
    method,
    headers: buildHeaders(event),
    query: getQuery(event),
    params,
    adminUser: event.context.adminUser || null,
    user: event.context.user || null
  };
}
function readPluginRouteBody(event, method) {
  if (!["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
    return void 0;
  }
  return readBody(event);
}
function resolvePluginRoute(method, routeParam) {
  const normalizedMethod = method.toUpperCase();
  const segments = (routeParam != null ? routeParam : "").split("/").filter(Boolean);
  const [slug, ...rest] = segments;
  if (!slug) {
    return null;
  }
  const pluginPath = rest.length > 0 ? `/${rest.join("/")}` : "/";
  for (const route of RouteRegistry.getAll()) {
    if (route.method !== normalizedMethod) {
      continue;
    }
    if (pluginSlug(route.pluginName) !== slug) {
      continue;
    }
    const params = matchPluginRoutePath(route.path, pluginPath);
    if (params) {
      return { route, params, pluginPath };
    }
  }
  return null;
}
async function dispatchPluginRoute(event) {
  var _a, _b;
  await ensurePluginRuntimeInitialized();
  await optionalAuth(event);
  const method = (event.node.req.method || "GET").toUpperCase();
  const routeParam = (_a = event.context.params) == null ? void 0 : _a.route;
  const resolved = resolvePluginRoute(method, routeParam);
  if (!resolved) {
    throw createError({
      statusCode: 404,
      statusMessage: "Plugin route not found"
    });
  }
  const { route, params } = resolved;
  const httpMethod = method;
  try {
    if ((_b = route.options.policies) == null ? void 0 : _b.length) {
      await PolicyRegistry.evaluate(
        route.options.policies,
        buildPolicyContext(event, httpMethod, params)
      );
    }
    const body = await readPluginRouteBody(event, httpMethod);
    return route.handler({
      path: event.path || event.node.req.url || "/",
      method: httpMethod,
      params,
      query: getQuery(event),
      headers: buildHeaders(event),
      body,
      event
    });
  } catch (error) {
    if (error instanceof PolicyDeniedError) {
      throw createError({
        statusCode: error.statusCode,
        statusMessage: error.message
      });
    }
    throw error;
  }
}

const ____route_ = defineEventHandler((event) => {
  return dispatchPluginRoute(event);
});

export { ____route_ as default };
//# sourceMappingURL=_...route_.mjs.map
