import*as p from"path";import{bold as g}from"colorette";import{DEFAULT_THEME_NAME as b}from"../../constants/common.js";import{logger as a}from"../../server/tools/notifiers/logger.js";import{getGlobMatcher as y}from"./glob.js";import{getThemeFiles as u,copyComponent as d,getEjectedFiles as x,copyComponentsFromList as C}from"./themeOperations.js";function F(o,r,n){return o.filter(c=>{const{dir:e,name:t}=p.parse(c);return r===p.join(e,t)||n(c)})}async function E({pathToThemeFolder:o,pathToPortalCustomThemeFolder:r,componentToEject:n,theme:c=b},e){const t=u(o,{onlyFilePatterns:e.onlyFiles,ignoreFilePatterns:e.ignoreFiles}),j=u(r),m=x(j,t);if(!n)return C({componentList:t,ejectedComponents:m,pathToThemeFolder:o,pathToPortalCustomThemeFolder:r,options:e});const $=y(n);let i=F(t,n,$);if(i.length===0){const s=`components/${n}`,f=y(s);i=F(t,s,f)}if(i.length>1)return C({componentList:i,ejectedComponents:m,pathToThemeFolder:o,pathToPortalCustomThemeFolder:r,options:e});const[l]=i;if(!l){const s=`The requested component ${g(n.toString())} does not exist in the theme ${g(c)}.`;a.warn(s),e&&e.onError?e.onError(s):a.info(`Below are the available components for ejecting:

${t.map(f=>`${f}
`).join("")}`);return}return d({componentToCopy:l,ejectedFiles:m,copyFrom:o,copyTo:r,options:e})}export{E as default};
