import{Source as d}from"@redocly/openapi-core";import g from"node:fs/promises";import m from"node:path";import{withPathPrefix as j,getPathPrefix as h,combineUrls as q}from"@redocly/theme/core/utils";import{getRoutesByLines as w,getPointerPosition as v}from"../../plugins/openapi-docs/ast-utils.js";import{OPENAPI_SHARED_DATA_PREFIX as P}from"../../plugins/openapi-docs/constants.js";import{promiseMapLimit as B,readSharedData as y}from"../../utils/index.js";import{normalizeRouteSlug as D,removeTrailingSlash as F}from"../../../shared/utils.js";function E(o){return async t=>{const s=await t.req.json(),r=decodeURI(F(s.split("?")[0])),[e]=r.split("#"),a=h(),i=D(e.slice(a.length)),n=o.getRouteBySlug(i);if(n&&o.getRouteByFsPath(n.fsPath)?.metadata?.type==="openapi"){const c=await y(`${P}${n.fsPath}`,o.outdir),p=Object.keys(c.routesMapping).find(f=>q(n.baseSlug,c.routesMapping[f])===r);if(!c.sourcePath)return t.json({route:n},200);let u;if(p){const f=m.join(o.contentDir,n.fsPath),b=await g.readFile(f,"utf-8"),S=new d(f,b);u=v(p,S)}return t.json({route:{fsPath:n.fsPath,pointer:p,position:u}},200)}return t.json({route:n},200)}}function L(o){return async t=>{const s=await t.req.json(),r=await B(s,5,async e=>R(e,o));return t.json({routes:r},200)}}function T(o){return async t=>{const s=await t.req.json(),r=await R(s,o);return t.json(r,200)}}function _(o){return async t=>{const s=(t.req.query("lines")||"").split(",").map(i=>parseInt(i)),r=t.req.query("file");if(!r)return t.json("No data",400);const e=await y(`${P}${r}`,o.outdir),a=m.join(o.contentDir,r);try{const i=await g.readFile(a,"utf-8");if(!e)return t.json("No data",400);const n=new d(a,i),l=w(s,n,e.routesMapping,e.baseSlug);return t.json(l,200)}catch{return t.json("No data",400)}}}async function R(o,t){const[s,r]=o.split(":");let e=t.getRouteByFsPath(s);if(e?.metadata?.type==="openapi"){const n=await y(`${P}${s}`,t.outdir);if(n?.sourcePath){const l=m.join(t.contentDir,e.fsPath),c=await g.readFile(l,"utf-8"),p=new d(l,c),u=w([parseInt(r,10)],p,n.routesMapping,e.baseSlug);if(u.length)return{route:{slug:h()?j(u[0]):u[0],filePath:o}}}}const a=e?.slug?h()?j(e?.slug):e?.slug:"";return{route:{...e,pageName:e?await e?.getNavText?.():"",slug:a,filePath:o}}}export{_ as getRoutesByLineHandler,T as resolvePathHandler,L as resolvePathsHandler,E as resolveSlugHandler};
