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