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