import v from"path";import{I18N_DIR_NAME as c,L10N_DIR_NAME as p,RESERVED_DIR_NAMES as m,VERSIONED_FOLDER_REGEXP_MATCH as P}from"../../../constants/common.js";import{parsePathVersions as l}from"../../../../utils/path/parse-path-versions.js";import{reporter as u}from"../../../tools/notifiers/reporter.js";import{logger as V}from"../../../tools/notifiers/logger.js";const _=async(r,{cache:i,fs:s})=>{const e=new Map,t=await s.scan();await y(t);for(const{relativePath:n}of t){if(v.posix.basename(n)==="versions.yaml"){await g(n,e,i);continue}const o=l(n);if(o==null)continue;const a=e.get(o.versionFolderPath)||{versionedFiles:new Map,versions:[]},f=a.versionedFiles.get(o.filePathInVersion)||new Set,d={version:o.versionName};f.add(d.version),!a.hasVersionsConfig&&a.versions.findIndex(h=>h.version===o.versionName)===-1&&a.versions.push(d),a.versionedFiles.set(o.filePathInVersion,f),e.set(o.versionFolderPath,a)}return F(e)};async function g(r,i,s){let e=v.posix.dirname(r);e==="."?e="/":e=e.endsWith("/")?e:e+"/",e.startsWith(c+"/")&&(e=e.slice(c.length+1)),e.startsWith(p+"/")&&(e=e.slice(p.length+1));const n=(await s.load(r,"yaml")).data;n.versions&&!Array.isArray(n.versions)?(await u.panicOnBuild(`Property 'versions' must be an array at %rp. Got '${typeof n.versions}'.`,r),n.versions=[]):(!n.versions||n.versions.length===0)&&V.warn("Property 'versions' is empty at %rp. This is likely a mistake.",r);const o=i.get(e)||{versionedFiles:new Map,versions:[]};o.hasVersionsConfig=!0,o.versions=n.versions||[],n.default&&(o.defaultVersion=n.default),i.set(e,o)}async function y(r){const i=new Set;for(const{relativePath:s}of r){const e=l(s);if(!(e?.versionFolderPath&&m.includes(e?.versionFolderPath))){if(e?.versionFolderPath==null){(s.match(P)?.length||0)>1&&await u.panicOnBuild(`Nested versions are not supported. Detected nested versions in path: "${s}".`);continue}i.has(e.versionFolderPath)||i.add(e.versionFolderPath)}}}function F(r){for(let[i,s]of r){const e=s.hasVersionsConfig?s.versions:s.versions.sort((n,o)=>n.version.localeCompare(o.version)),t=s.defaultVersion||e[e.length-1]?.version;r.set(i,{...s,versions:e,defaultVersion:t})}return r}function N(r,i){const s=l(r);return s==null?null:i.get(s.versionFolderPath)?.defaultVersion}export{N as getDefaultVersionByPath,_ as versionsConfigLoader};
