import{a as b,b as g,c as m}from"./chunk-74CEPOFO.js";import"./chunk-DXB73IDG.js";var k={name:"unexpand",summary:"convert spaces to tabs",usage:"unexpand [OPTION]... [FILE]...",description:"Convert blanks in each FILE to TABs, writing to standard output. If no FILE is specified, standard input is read.",options:["-t N        Use N spaces per tab (default: 8)","-t LIST     Use comma-separated list of tab stops","-a          Convert all sequences of blanks (not just leading)"],examples:["unexpand file.txt           # Convert leading spaces to tabs","unexpand -a file.txt        # Convert all space sequences","unexpand -t 4 file.txt      # Use 4-space tabs"]};function d(s){let i=s.split(",").map(o=>o.trim()),e=[];for(let o of i){let n=parseInt(o,10);if(Number.isNaN(n)||n<1)return null;e.push(n)}for(let o=1;o<e.length;o++)if(e[o]<=e[o-1])return null;return e}function v(s,i){if(i.length===1){let e=i[0];return s+(e-s%e)}for(let e of i)if(e>s)return e;if(i.length>=2){let e=i[i.length-1]-i[i.length-2],o=i[i.length-1],n=Math.floor((s-o)/e)+1;return o+n*e}return-1}function L(s,i){let{tabStops:e,allBlanks:o}=i,n="",a=0,t="",l=0,u=!0,f=()=>{if(t.length===0)return;let r=l+t.length;if(!o&&!u){n+=t,t="";return}let p=l,c="";for(;p<r;){let h=v(p,e);if(h<=r&&h>p)c+="	",p=h;else break}let x=r-p;x>0&&(c+=" ".repeat(x)),n+=c,t=""};for(let r of s)r===" "?(t.length===0&&(l=a),t+=r,a++):r==="	"?(f(),n+=r,a=v(a,e)):(f(),n+=r,a++,u=!1);return f(),n}function C(s,i){if(s==="")return"";let e=s.split(`
`),o=s.endsWith(`
`)&&e[e.length-1]==="";return o&&e.pop(),e.map(a=>L(a,i)).join(`
`)+(o?`
`:"")}var w={name:"unexpand",execute:async(s,i)=>{if(g(s))return b(k);let e={tabStops:[8],allBlanks:!1},o=[],n=0;for(;n<s.length;){let t=s[n];if(t==="-t"&&n+1<s.length){let l=d(s[n+1]);if(!l)return{exitCode:1,stdout:"",stderr:`unexpand: invalid tab size: '${s[n+1]}'
`};e.tabStops=l,n+=2}else if(t.startsWith("-t")&&t.length>2){let l=d(t.slice(2));if(!l)return{exitCode:1,stdout:"",stderr:`unexpand: invalid tab size: '${t.slice(2)}'
`};e.tabStops=l,n++}else if(t==="--tabs"&&n+1<s.length){let l=d(s[n+1]);if(!l)return{exitCode:1,stdout:"",stderr:`unexpand: invalid tab size: '${s[n+1]}'
`};e.tabStops=l,n+=2}else if(t.startsWith("--tabs=")){let l=d(t.slice(7));if(!l)return{exitCode:1,stdout:"",stderr:`unexpand: invalid tab size: '${t.slice(7)}'
`};e.tabStops=l,n++}else if(t==="-a"||t==="--all")e.allBlanks=!0,n++;else if(t==="--"){o.push(...s.slice(n+1));break}else{if(t.startsWith("-")&&t!=="-")return m("unexpand",t);o.push(t),n++}}let a="";if(o.length===0){let t=i.stdin??"";a=C(t,e)}else for(let t of o){let l=i.fs.resolvePath(i.cwd,t),u=await i.fs.readFile(l);if(u===null)return{exitCode:1,stdout:a,stderr:`unexpand: ${t}: No such file or directory
`};a+=C(u,e)}return{exitCode:0,stdout:a,stderr:""}}};export{w as unexpand};
