import*as c from"path";import{lstatSync as m,readdirSync as y,copyFileSync as F,mkdirSync as h}from"fs";import{FsErrors as g}from"../../constants/common.js";import{logger as a}from"../../server/tools/notifiers/logger.js";import{getGlobMatcher as l}from"./glob.js";import{askUserToConfirm as x,askUserToSelectComponents as j}from"./ejectPrompt.js";function f(r){return y(r).flatMap(i=>{const t=c.join(r,i);return m(t).isDirectory()?f(t):t})}function A(r,i=c.resolve("")){const t=(n,o,e)=>{o.forEach(s=>{n[s]={...n[s]||{}},n=n[s]}),n["."]=Array.isArray(n["."])?[...n["."],e]:[e]};return r.reduce((n,o)=>{if(m(c.join(i,o)).isFile()){const{dir:e,base:s}=c.parse(o),d=e?e.split(c.sep).filter(Boolean):[];t(n,d,s)}return n},{})}function u(r,i){return c.normalize(r).replace(c.normalize(i),"").replace(/^[\/\\]+/,"")}function C(r,i={}){let t=[];const{ignoreFilePatterns:n,onlyFilePatterns:o}=i;try{if(t=f(r).map(e=>u(e,r)),o){const e=l(o);t=t.filter(s=>e(s))}if(n){const e=l(n);return t.filter(s=>!e(s))}return t}catch(e){if(e.code===g.NotExist)return t;throw e}}function T(r,i){return r.filter(t=>i.includes(t))}function v(r,i,t){const n=u(t,i),o=c.resolve(i,n),e=c.resolve(r,n);return h(c.dirname(e),{recursive:!0}),F(o,e),e}function p(r,i,t,n){const o=a.startTiming(),e=v(t,i,r),s=`\u{1F4E6} Component ${r} has been ejected into %rp`;n?.onSuccess?.(s),a.infoTime(o,s,e)}async function S({componentToCopy:r,ejectedFiles:i=[],copyFrom:t,copyTo:n,options:o={}}){if(i.includes(r)&&!o.force){const e=`File [ ${r} ] will be overwritten. Are you sure ?`;return o&&o.onOverride?o.onOverride(e):await x(e)?p(r,t,n):a.info(`Skipped [ ${r} ] component ejection.`)}return p(r,t,n,o)}async function $({componentList:r,ejectedComponents:i,pathToThemeFolder:t,pathToPortalCustomThemeFolder:n,options:o}){const e=await j(r,t);for(const s of e)await S({componentToCopy:s,ejectedFiles:i,copyFrom:t,copyTo:n,options:o})}export{S as copyComponent,p as copyComponentAndReport,$ as copyComponentsFromList,v as copyThemeComponent,T as getEjectedFiles,C as getThemeFiles,u as stripComponentThemePath,A as toFileTree};
