const corePaths = [
    ["GET", "/api/bootstrap"],
    ["GET", "/api/health"],
    ["GET", "/api/snapshot"],
    ["GET", "/api/version"],
    ["POST", "/api/update"],
    ["GET", "/api/tasks"],
    ["GET", "/api/progress"],
    ["GET", "/api/trace"],
    ["GET", "/api/metrics"],
    ["GET", "/api/metrics/history"],
    ["GET", "/api/metrics/observability"],
    ["GET", "/api/active-sessions"],
    ["GET", "/api/adapters/health"],
    ["GET", "/api/adapters/conformance"],
    ["GET", "/api/diagnostics"],
    ["POST", "/api/diagnostics/voice/refresh"],
    ["GET", "/api/diagnostics/bundle"],
    ["GET", "/api/control-options"],
    ["GET", "/api/locks"],
    ["POST", "/api/locks"],
    ["DELETE", "/api/locks"],
    ["GET", "/api/auth/status"],
    ["POST", "/api/auth/login"],
    ["POST", "/api/auth/logout"],
];
const sessionPaths = [
    ["GET", "/api/sessions"],
    ["GET", "/api/sessions/detail"],
    ["POST", "/api/sessions/name"],
    ["POST", "/api/agent"],
    ["POST", "/api/sessions/new"],
    ["GET", "/api/sessions/worktrees"],
    ["POST", "/api/sessions/worktrees/fork"],
    ["POST", "/api/sessions/worktrees/compare"],
    ["POST", "/api/sessions/worktrees/integrate"],
    ["POST", "/api/sessions/worktrees/integrate/preview"],
    ["POST", "/api/sessions/worktrees/integrate/patch"],
    ["POST", "/api/sessions/worktrees/cleanup"],
    ["POST", "/api/sessions/switch"],
    ["POST", "/api/sessions/attach"],
    ["GET", "/api/models"],
    ["POST", "/api/session/model"],
    ["POST", "/api/session/reasoning"],
    ["POST", "/api/session/fast"],
    ["POST", "/api/session/launch"],
    ["POST", "/api/prompt"],
    ["POST", "/api/prompt/upload"],
    ["POST", "/api/abort"],
    ["POST", "/api/stop"],
    ["POST", "/api/handback"],
    ["POST", "/api/retry"],
    ["POST", "/api/sync"],
];
const queuePaths = [
    ["GET", "/api/queue"],
    ["POST", "/api/queue"],
    ["GET", "/api/queue/plans"],
    ["POST", "/api/queue/plans"],
];
const chatPaths = [
    ["GET", "/api/chat/history"],
    ["DELETE", "/api/chat/history"],
    ["GET", "/api/chat/attachment"],
    ["GET", "/api/chat/mirror"],
    ["POST", "/api/chat/mirror"],
];
const artifactPaths = [
    ["GET", "/api/artifacts"],
    ["GET", "/api/artifacts/usage"],
    ["POST", "/api/artifacts/cleanup/preview"],
    ["POST", "/api/artifacts/cleanup/run"],
    ["GET", "/api/artifacts/preview"],
    ["GET", "/api/artifacts/diff"],
    ["DELETE", "/api/artifacts"],
    ["POST", "/api/artifacts/bulk"],
    ["GET", "/api/artifacts/zip"],
    ["GET", "/api/artifacts/file"],
];
const pluginPaths = [
    ["GET", "/api/plugins"],
    ["POST", "/api/plugins"],
    ["GET", "/api/plugins/catalog"],
    ["POST", "/api/plugins/analyze"],
    ["POST", "/api/plugins/validate"],
    ["POST", "/api/plugins/scaffold"],
];
export const PEER_WEB_ROUTES = [
    ...corePaths.map(([method, path]) => exact(method, path, "core")),
    exact("GET", "/api/peers", "peers"),
    pattern("POST", /^\/api\/peers\/([^/]+)\/(?:rotate|sync-invite)$/, "peers"),
    exact("GET", "/api/agent-updates", "agentUpdates"),
    exact("POST", "/api/agent-update", "agentUpdates"),
    pattern("*", /^\/api\/agent-update\/([^/]+)\/(?:log|input|cancel)$/, "agentUpdates"),
    exact("GET", "/api/jobs", "jobs"),
    pattern("*", /^\/api\/jobs\/([^/]+)\/(?:log|action)$/, "jobs"),
    exact("GET", "/api/templates", "workflows"),
    exact("POST", "/api/templates", "workflows"),
    exact("POST", "/api/templates/import", "workflows"),
    pattern("*", /^\/api\/templates\/([^/]+)\/(?:versions|diff|export)(?:\/([^/]+))?(?:\/(rollback|run|preview|export))?$/, "workflows"),
    pattern("*", /^\/api\/templates\/([^/]+)(?:\/(run|preview))?$/, "workflows"),
    exact("GET", "/api/workflows", "workflows"),
    exact("POST", "/api/workflows", "workflows"),
    exact("POST", "/api/workflows/import", "workflows"),
    pattern("*", /^\/api\/workflow-runs\/([^/]+)(?:\/(cancel|rerun-failed|report))?$/, "workflows"),
    pattern("*", /^\/api\/workflows\/([^/]+)\/(?:versions|diff|export)(?:\/([^/]+))?(?:\/(rollback|run|preview|export))?$/, "workflows"),
    pattern("*", /^\/api\/workflows\/([^/]+)\/(dry-run|triggers)(?:\/([^/]+))?$/, "workflows"),
    pattern("POST", /^\/api\/workflow-triggers\/([^/]+)\/run$/, "workflows"),
    pattern("*", /^\/api\/workflows\/([^/]+)(?:\/(run|preview))?$/, "workflows"),
    ...sessionPaths.map(([method, path]) => exact(method, path, "sessions")),
    pattern("POST", /^\/api\/approvals\/([^/]+)\/respond$/, "sessions"),
    pattern("*", /^\/api\/sessions\/worktrees\/integrations\/([^/]+)\/finalize$/, "sessions"),
    pattern("*", /^\/api\/sessions\/worktrees\/([^/]+)(?:\/(diff|update|commit))?$/, "sessions"),
    ...queuePaths.map(([method, path]) => exact(method, path, "queue")),
    pattern("*", /^\/api\/queue\/plans\/([^/]+)(?:\/(move|approve|enqueue))?$/, "queue"),
    ...chatPaths.map(([method, path]) => exact(method, path, "chat")),
    exact("GET", "/api/activity", "activity"),
    ...artifactPaths.map(([method, path]) => exact(method, path, "artifacts")),
    ...pluginPaths.map(([method, path]) => exact(method, path, "plugins")),
    pattern("*", /^\/api\/plugins\/([^/]+)(?:\/(enable|disable|settings|log|manifest|update-check|update|rollback|invoke|command|panel|artifact-handler|diagnostics|collector))?$/, "plugins"),
    pattern("*", /^\/api\/plugins\/([^/]+)\/(events|jobs)(?:\/([^/]+)(?:\/(cancel))?)?$/, "plugins"),
    exact("GET", "/api/logs", "operations"),
    exact("POST", "/api/logs/clear", "operations"),
    exact("POST", "/api/runtime/restart", "operations"),
];
export const PEER_WEB_ROUTE_CONTRACT_PATHS = new Set([
    ...PEER_WEB_ROUTES.map((route) => route.path).filter((path) => Boolean(path)),
    "/api/agent-update/:id/log",
    "/api/agent-update/:id/input",
    "/api/agent-update/:id/cancel",
    "/api/jobs/:id/log",
    "/api/jobs/:id/action",
    "/api/templates/:id",
    "/api/templates/import",
    "/api/templates/:id/versions",
    "/api/templates/:id/diff",
    "/api/templates/:id/export",
    "/api/templates/:id/versions/:version/export",
    "/api/templates/:id/versions/:version/rollback",
    "/api/templates/:id/versions/:version/run",
    "/api/templates/:id/versions/:version/preview",
    "/api/templates/:id/run",
    "/api/templates/:id/preview",
    "/api/workflow-runs/:id",
    "/api/workflow-runs/:id/cancel",
    "/api/workflow-runs/:id/rerun-failed",
    "/api/workflow-runs/:id/report",
    "/api/workflows/:id",
    "/api/workflows/import",
    "/api/workflows/:id/versions",
    "/api/workflows/:id/diff",
    "/api/workflows/:id/export",
    "/api/workflows/:id/versions/:version/export",
    "/api/workflows/:id/versions/:version/rollback",
    "/api/workflows/:id/versions/:version/run",
    "/api/workflows/:id/versions/:version/preview",
    "/api/workflows/:id/dry-run",
    "/api/workflows/:id/triggers",
    "/api/workflows/:id/triggers/:triggerId",
    "/api/workflows/:id/run",
    "/api/workflows/:id/preview",
    "/api/workflow-triggers/:token/run",
    "/api/peers/:id/rotate",
    "/api/peers/:id/sync-invite",
    "/api/sessions/worktrees/:id",
    "/api/sessions/worktrees/integrations/:id/finalize",
    "/api/sessions/worktrees/:id/diff",
    "/api/sessions/worktrees/:id/update",
    "/api/sessions/worktrees/:id/commit",
    "/api/approvals/:id/respond",
    "/api/plugins",
    "/api/plugins/catalog",
    "/api/plugins/analyze",
    "/api/plugins/validate",
    "/api/plugins/scaffold",
    "/api/plugins/:id",
    "/api/plugins/:id/enable",
    "/api/plugins/:id/disable",
    "/api/plugins/:id/settings",
    "/api/plugins/:id/log",
    "/api/plugins/:id/events",
    "/api/plugins/:id/manifest",
    "/api/plugins/:id/update-check",
    "/api/plugins/:id/update",
    "/api/plugins/:id/jobs",
    "/api/plugins/:id/jobs/:jobId",
    "/api/plugins/:id/jobs/:jobId/cancel",
    "/api/plugins/:id/rollback",
    "/api/plugins/:id/invoke",
    "/api/plugins/:id/command",
    "/api/plugins/:id/panel",
    "/api/plugins/:id/artifact-handler",
    "/api/plugins/:id/diagnostics",
    "/api/plugins/:id/collector",
    "/api/queue/plans/:id",
    "/api/queue/plans/:id/move",
    "/api/queue/plans/:id/approve",
    "/api/queue/plans/:id/enqueue",
]);
export function matchPeerWebRoute(method, path) {
    for (const definition of PEER_WEB_ROUTES) {
        if (definition.method !== "*" && definition.method !== method) {
            continue;
        }
        if (definition.path && definition.path === path) {
            return { definition, params: [] };
        }
        if (definition.pattern) {
            const match = path.match(definition.pattern);
            if (match) {
                return { definition, params: match.slice(1).map((value) => value ? decodeURIComponent(value) : "") };
            }
        }
    }
    return null;
}
function exact(method, path, group) {
    return { method, path, group };
}
function pattern(method, pattern, group) {
    return { method, pattern, group };
}
