import{a as b,b as y,c as N}from"./chunk-74CEPOFO.js";import"./chunk-DXB73IDG.js";var I={name:"join",summary:"join lines of two files on a common field",usage:"join [OPTION]... FILE1 FILE2",description:"For each pair of input lines with identical join fields, write a line to standard output. The default join field is the first, delimited by blanks.",options:["-1 FIELD     Join on this FIELD of file 1 (default: 1)","-2 FIELD     Join on this FIELD of file 2 (default: 1)","-t CHAR      Use CHAR as input and output field separator","-a FILENUM   Also print unpairable lines from file FILENUM (1 or 2)","-v FILENUM   Like -a but only output unpairable lines","-e STRING    Replace missing fields with STRING","-o FORMAT    Output format (comma-separated list of FILENUM.FIELD)","-i           Ignore case when comparing fields"],examples:["join file1 file2               # Join on first field","join -1 2 -2 1 file1 file2     # Join file1 col 2 with file2 col 1","join -t ',' file1.csv file2.csv  # Join CSV files","join -a 1 file1 file2          # Left outer join"]};function F(t,s){return s?t.split(s):t.split(/[ \t]+/).filter(n=>n.length>0)}function L(t,s,n,f){let e=F(t,s),r=e[n-1]??"";return f&&(r=r.toLowerCase()),{fields:e,joinKey:r,original:t}}function m(t,s,n){let f=n.separator??" ";if(n.outputFormat){let l=[];for(let{file:c,field:d}of n.outputFormat){let a=c===1?t:s;a&&d===0?l.push(a.joinKey):a&&a.fields[d-1]!==void 0?l.push(a.fields[d-1]):l.push(n.emptyString)}return l.join(f)}let e=[],r=t?.joinKey??s?.joinKey??"";if(e.push(r),t)for(let l=0;l<t.fields.length;l++)l!==n.field1-1&&e.push(t.fields[l]);if(s)for(let l=0;l<s.fields.length;l++)l!==n.field2-1&&e.push(s.fields[l]);return e.join(f)}function w(t){let s=t.split(","),n=[];for(let f of s){let e=f.trim().match(/^(\d+)\.(\d+)$/);if(!e)return null;let r=Number.parseInt(e[1],10),l=Number.parseInt(e[2],10);if(r!==1&&r!==2)return null;n.push({file:r,field:l})}return n}var C={name:"join",execute:async(t,s)=>{if(y(t))return b(I);let n={field1:1,field2:1,separator:null,printUnpairable:new Set,onlyUnpairable:new Set,emptyString:"",outputFormat:null,ignoreCase:!1},f=[],e=0;for(;e<t.length;){let i=t[e];if(i==="-1"&&e+1<t.length){let o=Number.parseInt(t[e+1],10);if(Number.isNaN(o)||o<1)return{exitCode:1,stdout:"",stderr:`join: invalid field number: '${t[e+1]}'
`};n.field1=o,e+=2}else if(i==="-2"&&e+1<t.length){let o=Number.parseInt(t[e+1],10);if(Number.isNaN(o)||o<1)return{exitCode:1,stdout:"",stderr:`join: invalid field number: '${t[e+1]}'
`};n.field2=o,e+=2}else if((i==="-t"||i==="--field-separator")&&e+1<t.length)n.separator=t[e+1],e+=2;else if(i.startsWith("-t")&&i.length>2)n.separator=i.slice(2),e++;else if(i==="-a"&&e+1<t.length){let o=Number.parseInt(t[e+1],10);if(o!==1&&o!==2)return{exitCode:1,stdout:"",stderr:`join: invalid file number: '${t[e+1]}'
`};n.printUnpairable.add(o),e+=2}else if(i.match(/^-a[12]$/))n.printUnpairable.add(Number.parseInt(i[2],10)),e++;else if(i==="-v"&&e+1<t.length){let o=Number.parseInt(t[e+1],10);if(o!==1&&o!==2)return{exitCode:1,stdout:"",stderr:`join: invalid file number: '${t[e+1]}'
`};n.onlyUnpairable.add(o),e+=2}else if(i.match(/^-v[12]$/))n.onlyUnpairable.add(Number.parseInt(i[2],10)),e++;else if(i==="-e"&&e+1<t.length)n.emptyString=t[e+1],e+=2;else if(i==="-o"&&e+1<t.length){let o=w(t[e+1]);if(!o)return{exitCode:1,stdout:"",stderr:`join: invalid field spec: '${t[e+1]}'
`};n.outputFormat=o,e+=2}else if(i==="-i"||i==="--ignore-case")n.ignoreCase=!0,e++;else if(i==="--"){f.push(...t.slice(e+1));break}else{if(i.startsWith("-")&&i!=="-")return N("join",i);f.push(i),e++}}if(f.length!==2)return{exitCode:1,stdout:"",stderr:f.length<2?`join: missing file operand
`:`join: extra operand
`};let r=[];for(let i of f)if(i==="-")r.push(s.stdin??"");else{let o=s.fs.resolvePath(s.cwd,i),u=await s.fs.readFile(o);if(u===null)return{exitCode:1,stdout:"",stderr:`join: ${i}: No such file or directory
`};r.push(u)}let l=(i,o)=>{let u=i.split(`
`);return i.endsWith(`
`)&&u[u.length-1]===""&&u.pop(),u.filter(h=>h.length>0).map(h=>L(h,n.separator,o,n.ignoreCase))},c=l(r[0],n.field1),d=l(r[1],n.field2),a=new Map;for(let i of d){let o=a.get(i.joinKey);o?o.push(i):a.set(i.joinKey,[i])}let p=[],j=new Set;for(let i of c){let o=a.get(i.joinKey);if(o&&o.length>0){if(j.add(i.joinKey),n.onlyUnpairable.size===0)for(let u of o)p.push(m(i,u,n))}else(n.printUnpairable.has(1)||n.onlyUnpairable.has(1))&&p.push(m(i,null,n))}if(n.printUnpairable.has(2)||n.onlyUnpairable.has(2))for(let i of d)j.has(i.joinKey)||p.push(m(null,i,n));return{exitCode:0,stdout:p.length>0?`${p.join(`
`)}
`:"",stderr:""}}};export{C as join};
