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_SLUGS as _}from"../../../constants/catalog-entities.js";import{CATALOG_ENTITIES_FILES_REGEX 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{promiseMapLimit as $}from"../../utils/async/promise-map-limit.js";import{getRoutesByLines as R,getPointerPosition as q}from"../../plugins/openapi-docs/ast-utils.js";import{readSharedData as j}from"../../utils/index.js";function X(o){return async t=>{const s=await t.req.json(),n=decodeURI(T(s.split("?")[0])),[e]=n.split("#"),u=h(),a=U(e.slice(u.length)),l=a.startsWith(y)?y:a,r=o.getRouteBySlug(l);if(r&&o.getRouteByFsPath(r.fsPath)?.metadata?.type==="openapi"){const c=await j(`${P}${r.fsPath}`,o.outdir),i=Object.keys(c.routesMapping).find(p=>E(r.baseSlug,c.routesMapping[p])===n);if(!c.sourcePath)return t.json({route:r},200);let f;if(i){const p=d.join(o.contentDir,r.fsPath),b=await g.readFile(p,"utf-8"),L=new m(p,b);f=q(i,L)}return t.json({route:{fsPath:r.fsPath,pointer:i,position:f}},200)}return t.json({route:r},200)}}function k(o){return async t=>{const s=await t.req.json(),n=await $(s,5,async e=>A(e,o));return t.json({routes:n},200)}}function J(o){return async t=>{const s=await t.req.json(),n=await A(s,o);return t.json(n,200)}}function K(o){return async t=>{const s=(t.req.query("lines")||"").split(",").map(a=>parseInt(a)),n=t.req.query("file");if(!n)return t.json("No data",400);const e=await j(`${P}${n}`,o.outdir),u=d.join(o.contentDir,n);try{const a=await g.readFile(u,"utf-8");if(!e)return t.json("No data",400);const l=new m(u,a),r=R(s,l,e.routesMapping,e.baseSlug);return t.json(r,200)}catch{return t.json("No data",400)}}}async function A(o,t){const[s,n]=o.split(":");if(I.test(s))return{route:{slug:`${y}/${_.ALL}`}};let e=t.getRouteByFsPath(s);if(e?.metadata?.type==="openapi"){const l=await j(`${P}${s}`,t.outdir);if(l?.sourcePath){const r=d.join(t.contentDir,e.fsPath),w=await g.readFile(r,"utf-8"),c=new m(r,w),i=R([parseInt(n,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,J as resolvePathHandler,k as resolvePathsHandler,X as resolveSlugHandler};
