import l from"fs/promises";import a from"path";var b=Symbol("MERGE_DROP"),F=".merge-backups",C=0,D=1,E=2,M=3,k=e=>e.includes("<<<<<<<")&&e.includes("=======")&&e.includes(">>>>>>>"),_=async({root:e=process.cwd(),include:i,exclude:r,matcher:s,includeNonConflicted:c,debug:t,backupDir:o=F})=>{for(let n of[...i,...r]){if(n.startsWith("!"))throw new Error(`Negation not allowed in include/exclude: ${n}`);n.includes("\\")&&console.warn(`Use '/' as path separator: ${n}`)}r.push(o);let f=n=>{let g=n.replace(/\\/g,"/");return s.isMatch(g,i)&&!s.isMatch(g,r)},d=S(i,o?[...r,o]:r,s),u=[],w=async n=>{let g=await l.readdir(n,{withFileTypes:!0});for(let h of g){let p=a.join(n,h.name),y=a.relative(e,p).replace(/\\+/g,"/");if(h.isDirectory())!/node_modules|\.git/.test(h.name)&&!d(y)&&await w(p);else if(f(y))try{let m=await l.readFile(p,"utf8");c||k(m)?u.push({filePath:y,content:m}):t&&console.info(`Skipped (no conflicts): ${p}`)}catch{console.warn(`Skipped (unreadable): ${p}`)}}};return await w(e),u},S=(e,i,r)=>{if(e.length>0&&e.every(t=>!t.includes("/")&&!t.includes("**")))return()=>!1;let s=new Set,c=new Set;for(let t of e)if(t.includes("/")){let o=t.split("/").slice(0,-1).join("/");o&&s.add(o)}s.has("**")&&s.clear();for(let t of i)if(t.endsWith("/**")){let o=t.slice(0,-3);o&&c.add(o)}return t=>r.isMatch(t,[...c])||s.size>0&&!r.isMatch(t,[...s])},$=async(e,i=F)=>{let r=a.relative(process.cwd(),e),s=a.join(i,r);return await l.mkdir(a.dirname(s),{recursive:!0}),await l.copyFile(e,s),s},v=async(e=".merge-backups")=>{let i=async(r,s="")=>{try{let c=await l.readdir(r,{withFileTypes:!0});for(let t of c){let o=a.join(r,t.name),f=a.join(s,t.name),d=a.join(process.cwd(),f);if(t.isDirectory())await i(o,f);else try{await l.mkdir(a.dirname(d),{recursive:!0}),await l.copyFile(o,d)}catch(u){console.warn(`Failed to restore ${o.replace(/[\r\n\t]/g,"")}: ${u}`)}}}catch(c){console.warn(`Failed to read backup directory ${r}: ${c}`)}};await i(e)};export{b as a,F as b,C as c,D as d,E as e,M as f,k as g,_ as h,S as i,$ as j,v as k};
