#!/usr/bin/env node
import{a as j,b as y}from"./chunk-63XSH3IT.js";import{a as g,b as v,c as F}from"./chunk-GTNBSMZR.js";var N={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 I(i,s){return s?i.split(s):i.split(/[ \t]+/).filter(n=>n.length>0)}function L(i,s,n,f){let e=I(i,s),r=e[n-1]??"";return f&&(r=r.toLowerCase()),{fields:e,joinKey:r,original:i}}function m(i,s,n){let f=n.separator??" ";if(n.outputFormat){let l=[];for(let{file:c,field:p}of n.outputFormat){let a=c===1?i:s;a&&p===0?l.push(a.joinKey):a&&a.fields[p-1]!==void 0?l.push(a.fields[p-1]):l.push(n.emptyString)}return l.join(f)}let e=[],r=i?.joinKey??s?.joinKey??"";if(e.push(r),i)for(let l=0;l<i.fields.length;l++)l!==n.field1-1&&e.push(i.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 U(i){let s=i.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 E={name:"join",execute:async(i,s)=>{if(v(i))return g(N);let n={field1:1,field2:1,separator:null,printUnpairable:new Set,onlyUnpairable:new Set,emptyString:"",outputFormat:null,ignoreCase:!1},f=[],e=0;for(;e<i.length;){let t=i[e];if(t==="-1"&&e+1<i.length){let o=Number.parseInt(i[e+1],10);if(Number.isNaN(o)||o<1)return{exitCode:1,stdout:"",stderr:`join: invalid field number: '${i[e+1]}'
`};n.field1=o,e+=2}else if(t==="-2"&&e+1<i.length){let o=Number.parseInt(i[e+1],10);if(Number.isNaN(o)||o<1)return{exitCode:1,stdout:"",stderr:`join: invalid field number: '${i[e+1]}'
`};n.field2=o,e+=2}else if((t==="-t"||t==="--field-separator")&&e+1<i.length)n.separator=i[e+1],e+=2;else if(t.startsWith("-t")&&t.length>2)n.separator=t.slice(2),e++;else if(t==="-a"&&e+1<i.length){let o=Number.parseInt(i[e+1],10);if(o!==1&&o!==2)return{exitCode:1,stdout:"",stderr:`join: invalid file number: '${i[e+1]}'
`};n.printUnpairable.add(o),e+=2}else if(t.match(/^-a[12]$/))n.printUnpairable.add(Number.parseInt(t[2],10)),e++;else if(t==="-v"&&e+1<i.length){let o=Number.parseInt(i[e+1],10);if(o!==1&&o!==2)return{exitCode:1,stdout:"",stderr:`join: invalid file number: '${i[e+1]}'
`};n.onlyUnpairable.add(o),e+=2}else if(t.match(/^-v[12]$/))n.onlyUnpairable.add(Number.parseInt(t[2],10)),e++;else if(t==="-e"&&e+1<i.length)n.emptyString=i[e+1],e+=2;else if(t==="-o"&&e+1<i.length){let o=U(i[e+1]);if(!o)return{exitCode:1,stdout:"",stderr:`join: invalid field spec: '${i[e+1]}'
`};n.outputFormat=o,e+=2}else if(t==="-i"||t==="--ignore-case")n.ignoreCase=!0,e++;else if(t==="--"){f.push(...i.slice(e+1));break}else{if(t.startsWith("-")&&t!=="-")return F("join",t);f.push(t),e++}}if(f.length!==2)return{exitCode:1,stdout:"",stderr:f.length<2?`join: missing file operand
`:`join: extra operand
`};let r=[];for(let t of f)if(t==="-")r.push(j(s.stdin)??"");else{let o=s.fs.resolvePath(s.cwd,t),u=await s.fs.readFile(o);if(u===null)return{exitCode:1,stdout:"",stderr:`join: ${t}: No such file or directory
`};r.push(u)}let l=(t,o)=>{let u=t.split(`
`);return t.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),p=l(r[1],n.field2),a=new Map;for(let t of p){let o=a.get(t.joinKey);o?o.push(t):a.set(t.joinKey,[t])}let d=[],b=new Set;for(let t of c){let o=a.get(t.joinKey);if(o&&o.length>0){if(b.add(t.joinKey),n.onlyUnpairable.size===0)for(let u of o)d.push(m(t,u,n))}else(n.printUnpairable.has(1)||n.onlyUnpairable.has(1))&&d.push(m(t,null,n))}if(n.printUnpairable.has(2)||n.onlyUnpairable.has(2))for(let t of p)b.has(t.joinKey)||d.push(m(null,t,n));return{exitCode:0,stdout:y(d.length>0?`${d.join(`
`)}
`:""),stderr:"",stdoutEncoding:"binary"}}},H={name:"join",flags:[{flag:"-1",type:"value",valueHint:"number"},{flag:"-2",type:"value",valueHint:"number"},{flag:"-t",type:"value",valueHint:"delimiter"},{flag:"-a",type:"value",valueHint:"number"},{flag:"-v",type:"value",valueHint:"number"},{flag:"-e",type:"value",valueHint:"string"},{flag:"-o",type:"value",valueHint:"format"},{flag:"-i",type:"boolean"}],needsArgs:!0,minArgs:2};export{E as a,H as b};
