import{c as F}from"./chunk-RIT5WMHA.mjs";import{b as C}from"./chunk-O53OUY2B.mjs";import{a as y}from"./chunk-ICS3PEIS.mjs";import{a as z}from"./chunk-CAOKJGEV.mjs";import{j as b}from"./chunk-QPV2G662.mjs";import{b as d,c as w}from"./chunk-AUIVUOKV.mjs";import{a as S}from"./chunk-3PFH3U2T.mjs";import{execSync as k}from"child_process";import n from"fs/promises";var N=new Map,I=async({ours:o,theirs:a,base:f,format:t,filePath:e,config:s,normalizedConfig:g,logger:m,autoStage:u=!1})=>{var i;let r=[],[l]=await Promise.all([F({ours:o,theirs:a,base:f,filePath:e,conflicts:r,path:"",ctx:{config:g,strategies:(i=g.customStrategies)!=null?i:{},_strategyCache:N}}),b(e,s.backupDir)]);if(g.debug&&m.debug(e,JSON.stringify({merged:l,conflicts:r},null,2)),r.length===0){let c=await S(t,l);if(await n.writeFile(e,c,"utf8"),u)try{k(`git add ${e}`)}catch(p){m.warn(e,`Failed to stage file: ${p}`)}return{success:!0,conflicts:[]}}else{let c=await z(l,o,a,t);return await Promise.all([n.writeFile(e,c,"utf8"),s.writeConflictSidecar?n.writeFile(`${e}.conflict.json`,JSON.stringify(r,null,2)):null]),{success:!1,conflicts:r}}},q=async(o,a,f,t={})=>{let e=await y(t.loggerConfig,t.debug),s=await C(t);s.debug&&e.info("git-merge",`Merging files: ours=${o}, base=${a}, theirs=${f}`);let[g,m,u]=await Promise.all([n.readFile(o,"utf8"),n.readFile(a,"utf8").catch(()=>"{}"),n.readFile(f,"utf8")]),r=d({...s,filename:""}),[l,i]=await w(g,r),[c,p]=await Promise.all([m,u].map(T=>w(T,[i]).then(([$])=>$))),x=typeof i=="string"?i:i.name,{success:M}=await I({ours:l,theirs:p,base:c,format:x,filePath:o,config:t,normalizedConfig:s,logger:e,autoStage:!1});await e.flush(),process.exit(M?0:1)};export{I as a,q as b};
