import f from"path";import{combineUrls as S}from"@redocly/theme/core/utils";import{VERSION_SEPARATOR as c}from"../../../constants/common.js";import{removeTrailingSlash as d,slash as b}from"../../../../shared/utils.js";import p from"../../../utils/reporter/logger.js";import{getDefaultVersionByPath as w}from"./versions-config-loader.js";import{GithubSlugger as F,slug as D}from"../../../utils/index.js";import{parseBaseName as N}from"../../utils.js";const A=async(s,{fs:e,cache:i})=>{const g=p.startTiming(),r=(await i.load("versions-config","versions-config")).data,t=new Map,n=new Map,o=new Set,l=new F;n.set(".","/"),n.set("/","/");const u=(await e.scan()).sort((a,m)=>m.relativePath.localeCompare(a.relativePath));for(const{relativePath:a}of u){const m=b(a).replace(new RegExp("^(@i18n|@l10n)\\/"),""),h=w(a,r);V(m,{defaultVersion:h,fileSlugs:t,dirSlugs:n,allFileSlugsList:o,githubSlugger:l})}return p.verboseTime(g,"Calculated slugs. Number of file paths processed: "+u.length),{fileSlugs:t,dirSlugs:n}};function V(s,e){const i=e.fileSlugs.get(s);if(i)return i;let g=f.posix.dirname(s);const r=v(g,e)||"",{baseName:t,isIndexFile:n}=N(s),o=e.dirSlugs.has(f.posix.join(g,t));let l;if(!n&&o){const u=D(S(r,t,"/"));e.allFileSlugsList.has(u)||(l=u)}l||(l=n&&e.dirSlugs.get(g)||e.githubSlugger.slug(S(r,t))),l=d(l),e.fileSlugs.set(s,l),e.allFileSlugsList.add(l)}function v(s,e){if(e.dirSlugs.has(s))return e.dirSlugs.get(s);const i=d(f.posix.normalize(s)).split("/");let g="/",r="";for(const t of i){r=f.posix.join(r,t);const n=E(t,e.defaultVersion);let o=e.dirSlugs.get(r)||e.githubSlugger.slug(S("/",g,n));o.endsWith("/")||(o=o+"/"),e.dirSlugs.set(r,o),g=o}return e.dirSlugs.get(s)}function E(s,e){if(!s.startsWith(c))return s;const i=s.substring(c.length);return i===e?"":i}export{A as contentSlugsLoader};
