import{getHigherRole as c}from"./rbac.js";function d(e,r){if(!e)return;if(!p(e))return e;const t={reunite:{},content:f(e)},n=i(e,r);for(const o of n)t.reunite[o.teamName]=o.projectRole,t.content={...t.content,...Object.fromEntries((e.teamFolders||[]).flatMap(a=>{const l={...e.teamFoldersBaseRoles||{authenticated:"READ"},[o.teamName]:o.projectRole,"redocly.owners":"ADMIN"},m=a.replaceAll("{teamPathSegment}",o.teamPathSegment),s=m+"/**";return[[m,l],[s,l]]}))};return{reunite:{...e.reunite,...t.reunite},content:{...P(e.content||{},t.content)}}}function p(e){return!!(e.teamNamePatterns&&e.teamFolders)}function u(e,r){for(const t of e.teamNamePatterns||[]){if(!t.includes("{teamPathSegment}")&&!t.includes("{projectRole}"))continue;const n="^"+t.replace("{teamPathSegment}","(?<teamPathSegment>[a-zA-Z0-9_.-]+)").replace("{projectRole}","(?<projectRole>admin|maintain|triage|write|read|none)")+"$",a=new RegExp(n,"i").exec(r)?.groups;if(a?.projectRole||a?.teamPathSegment)return{teamName:r,projectRole:a.projectRole?.toUpperCase(),teamPathSegment:a.teamPathSegment?.toLowerCase()}}return null}function g(e,r){for(const t of e?.teamFolders||[]){if(!t.includes("{teamPathSegment}"))continue;const n=`^${t.replace("{teamPathSegment}","(?<teamPathSegment>[a-zA-Z0-9_.-]+)")}($|/)`,o=new RegExp(n,"i");for(const a of r){const l=o.exec(a)?.groups;if(l?.teamPathSegment)return{teamPathSegment:l.teamPathSegment.toLowerCase()}}}return null}function i(e,r){const t=r.map(o=>u(e,o));let n=null;for(const o of t)o?.projectRole&&!o.teamPathSegment&&(n=n?c(n,o.projectRole):o.projectRole);return t.map(o=>!o?.teamPathSegment||!o.projectRole&&!n?null:{...o,projectRole:o?.projectRole??n}).filter(Boolean)}function f(e){const r={};if(e.teamFoldersBaseRoles)for(const t of e.teamFolders){const n=t.replaceAll("{teamPathSegment}","**");r[n]=e.teamFoldersBaseRoles}return r}function P(e,r){const t={...e};for(const[n,o]of Object.entries(r))t[n]?t[n]={...t[n],...o}:t[n]=o;return t}export{p as canExpandConfig,d as expandRbacConfig,f as getTeamFolderDefaults,g as parseTeamFoldersTemplate,u as parseTeamNameTemplate,i as parseTeams};
