#!/usr/bin/env node
import{a as N}from"./chunk-JBABAK44.js";import{a as S,b as E}from"./chunk-GTNBSMZR.js";import"./chunk-KGOUQS5A.js";var v=class{diff(e,t,n={}){let s;typeof n=="function"?(s=n,n={}):"callback"in n&&(s=n.callback);let u=this.castInput(e,n),f=this.castInput(t,n),i=this.removeEmpty(this.tokenize(u,n)),a=this.removeEmpty(this.tokenize(f,n));return this.diffWithOptionsObj(i,a,n,s)}diffWithOptionsObj(e,t,n,s){var u;let f=r=>{if(r=this.postProcess(r,n),s){setTimeout(function(){s(r)},0);return}else return r},i=t.length,a=e.length,d=1,o=i+a;n.maxEditLength!=null&&(o=Math.min(o,n.maxEditLength));let g=(u=n.timeout)!==null&&u!==void 0?u:1/0,w=Date.now()+g,m=[{oldPos:-1,lastComponent:void 0}],P=this.extractCommon(m[0],t,e,0,n);if(m[0].oldPos+1>=a&&P+1>=i)return f(this.buildValues(m[0].lastComponent,t,e));let C=-1/0,x=1/0,L=()=>{for(let r=Math.max(C,-d);r<=Math.min(x,d);r+=2){let c,h=m[r-1],p=m[r+1];h&&(m[r-1]=void 0);let y=!1;if(p){let I=p.oldPos-r;y=p&&0<=I&&I<i}let b=h&&h.oldPos+1<a;if(!y&&!b){m[r]=void 0;continue}if(!b||y&&h.oldPos<p.oldPos?c=this.addToPath(p,!0,!1,0,n):c=this.addToPath(h,!1,!0,1,n),P=this.extractCommon(c,t,e,r,n),c.oldPos+1>=a&&P+1>=i)return f(this.buildValues(c.lastComponent,t,e))||!0;m[r]=c,c.oldPos+1>=a&&(x=Math.min(x,r-1)),P+1>=i&&(C=Math.max(C,r+1))}d++};if(s)(function r(){setTimeout(function(){if(d>o||Date.now()>w)return s(void 0);L()||r()},0)})();else for(;d<=o&&Date.now()<=w;){let r=L();if(r)return r}}addToPath(e,t,n,s,u){let f=e.lastComponent;return f&&!u.oneChangePerToken&&f.added===t&&f.removed===n?{oldPos:e.oldPos+s,lastComponent:{count:f.count+1,added:t,removed:n,previousComponent:f.previousComponent}}:{oldPos:e.oldPos+s,lastComponent:{count:1,added:t,removed:n,previousComponent:f}}}extractCommon(e,t,n,s,u){let f=t.length,i=n.length,a=e.oldPos,d=a-s,o=0;for(;d+1<f&&a+1<i&&this.equals(n[a+1],t[d+1],u);)d++,a++,o++,u.oneChangePerToken&&(e.lastComponent={count:1,previousComponent:e.lastComponent,added:!1,removed:!1});return o&&!u.oneChangePerToken&&(e.lastComponent={count:o,previousComponent:e.lastComponent,added:!1,removed:!1}),e.oldPos=a,d}equals(e,t,n){return n.comparator?n.comparator(e,t):e===t||!!n.ignoreCase&&e.toLowerCase()===t.toLowerCase()}removeEmpty(e){let t=[];for(let n=0;n<e.length;n++)e[n]&&t.push(e[n]);return t}castInput(e,t){return e}tokenize(e,t){return Array.from(e)}join(e){return e.join("")}postProcess(e,t){return e}get useLongestToken(){return!1}buildValues(e,t,n){let s=[],u;for(;e;)s.push(e),u=e.previousComponent,delete e.previousComponent,e=u;s.reverse();let f=s.length,i=0,a=0,d=0;for(;i<f;i++){let o=s[i];if(o.removed)o.value=this.join(n.slice(d,d+o.count)),d+=o.count;else{if(!o.added&&this.useLongestToken){let g=t.slice(a,a+o.count);g=g.map(function(w,m){let P=n[d+m];return P.length>w.length?P:w}),o.value=this.join(g)}else o.value=this.join(t.slice(a,a+o.count));a+=o.count,o.added||(d+=o.count)}}return s}};var T=class extends v{constructor(){super(...arguments),this.tokenize=W}equals(e,t,n){return n.ignoreWhitespace?((!n.newlineIsToken||!e.includes(`
`))&&(e=e.trim()),(!n.newlineIsToken||!t.includes(`
`))&&(t=t.trim())):n.ignoreNewlineAtEof&&!n.newlineIsToken&&(e.endsWith(`
`)&&(e=e.slice(0,-1)),t.endsWith(`
`)&&(t=t.slice(0,-1))),super.equals(e,t,n)}},O=new T;function D(l,e,t){return O.diff(l,e,t)}function W(l,e){e.stripTrailingCr&&(l=l.replace(/\r\n/g,`
`));let t=[],n=l.split(/(\n|\r\n)/);n[n.length-1]||n.pop();for(let s=0;s<n.length;s++){let u=n[s];s%2&&!e.newlineIsToken?t[t.length-1]+=u:t.push(u)}return t}function j(l,e,t,n,s,u,f){let i;f?typeof f=="function"?i={callback:f}:i=f:i={},typeof i.context>"u"&&(i.context=4);let a=i.context;if(i.newlineIsToken)throw new Error("newlineIsToken may not be used with patch-generation functions, only with diffing functions");if(i.callback){let{callback:o}=i;D(t,n,Object.assign(Object.assign({},i),{callback:g=>{let w=d(g);o(w)}}))}else return d(D(t,n,i));function d(o){if(!o)return;o.push({value:"",lines:[]});function g(r){return r.map(function(c){return" "+c})}let w=[],m=0,P=0,C=[],x=1,L=1;for(let r=0;r<o.length;r++){let c=o[r],h=c.lines||A(c.value);if(c.lines=h,c.added||c.removed){if(!m){let p=o[r-1];m=x,P=L,p&&(C=a>0?g(p.lines.slice(-a)):[],m-=C.length,P-=C.length)}for(let p of h)C.push((c.added?"+":"-")+p);c.added?L+=h.length:x+=h.length}else{if(m)if(h.length<=a*2&&r<o.length-2)for(let p of g(h))C.push(p);else{let p=Math.min(h.length,a);for(let b of g(h.slice(0,p)))C.push(b);let y={oldStart:m,oldLines:x-m+p,newStart:P,newLines:L-P+p,lines:C};w.push(y),m=0,P=0,C=[]}x+=h.length,L+=h.length}}for(let r of w)for(let c=0;c<r.lines.length;c++)r.lines[c].endsWith(`
`)?r.lines[c]=r.lines[c].slice(0,-1):(r.lines.splice(c+1,0,"\\ No newline at end of file"),c++);return{oldFileName:l,newFileName:e,oldHeader:s,newHeader:u,hunks:w}}}function k(l){if(Array.isArray(l))return l.map(k).join(`
`);let e=[];l.oldFileName==l.newFileName&&e.push("Index: "+l.oldFileName),e.push("==================================================================="),e.push("--- "+l.oldFileName+(typeof l.oldHeader>"u"?"":"	"+l.oldHeader)),e.push("+++ "+l.newFileName+(typeof l.newHeader>"u"?"":"	"+l.newHeader));for(let t=0;t<l.hunks.length;t++){let n=l.hunks[t];n.oldLines===0&&(n.oldStart-=1),n.newLines===0&&(n.newStart-=1),e.push("@@ -"+n.oldStart+","+n.oldLines+" +"+n.newStart+","+n.newLines+" @@");for(let s of n.lines)e.push(s)}return e.join(`
`)+`
`}function F(l,e,t,n,s,u,f){if(typeof f=="function"&&(f={callback:f}),f?.callback){let{callback:i}=f;j(l,e,t,n,s,u,Object.assign(Object.assign({},f),{callback:a=>{i(a?k(a):void 0)}}))}else{let i=j(l,e,t,n,s,u,f);return i?k(i):void 0}}function A(l){let e=l.endsWith(`
`),t=l.split(`
`).map(n=>n+`
`);return e?t.pop():t.push(t.pop().slice(0,-1)),t}var z={name:"diff",summary:"compare files line by line",usage:"diff [OPTION]... FILE1 FILE2",options:["-u, --unified     output unified diff format (default)","-q, --brief       report only whether files differ","-s, --report-identical-files  report when files are the same","-i, --ignore-case  ignore case differences","    --help        display this help and exit"]},H={unified:{short:"u",long:"unified",type:"boolean"},brief:{short:"q",long:"brief",type:"boolean"},reportSame:{short:"s",long:"report-identical-files",type:"boolean"},ignoreCase:{short:"i",long:"ignore-case",type:"boolean"}},K={name:"diff",async execute(l,e){if(E(l))return S(z);let t=N("diff",l,H);if(!t.ok)return t.error;let n=t.result.flags.brief,s=t.result.flags.reportSame,u=t.result.flags.ignoreCase,f=t.result.positional;if(t.result.flags.unified,f.length<2)return{stdout:"",stderr:`diff: missing operand
`,exitCode:2};let i,a,[d,o]=f;try{i=d==="-"?e.stdin:await e.fs.readFile(e.fs.resolvePath(e.cwd,d))}catch{return{stdout:"",stderr:`diff: ${d}: No such file or directory
`,exitCode:2}}try{a=o==="-"?e.stdin:await e.fs.readFile(e.fs.resolvePath(e.cwd,o))}catch{return{stdout:"",stderr:`diff: ${o}: No such file or directory
`,exitCode:2}}let g=i,w=a;return u&&(g=g.toLowerCase(),w=w.toLowerCase()),g===w?s?{stdout:`Files ${d} and ${o} are identical
`,stderr:"",exitCode:0}:{stdout:"",stderr:"",exitCode:0}:n?{stdout:`Files ${d} and ${o} differ
`,stderr:"",exitCode:1}:{stdout:F(d,o,i,a,"","",{context:3}),stderr:"",exitCode:1}}};export{K as diffCommand};
