import{n as e,r as t,t as n}from"./chunk-74CEPOFO-V0JXh91S.mjs";var r={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(e,t){return t?e.split(t):e.split(/[ \t]+/).filter(e=>e.length>0)}function a(e,t,n,r){let a=i(e,t),o=a[n-1]??``;return r&&(o=o.toLowerCase()),{fields:a,joinKey:o,original:e}}function o(e,t,n){let r=n.separator??` `;if(n.outputFormat){let i=[];for(let{file:r,field:a}of n.outputFormat){let o=r===1?e:t;o&&a===0?i.push(o.joinKey):o&&o.fields[a-1]!==void 0?i.push(o.fields[a-1]):i.push(n.emptyString)}return i.join(r)}let i=[],a=e?.joinKey??t?.joinKey??``;if(i.push(a),e)for(let t=0;t<e.fields.length;t++)t!==n.field1-1&&i.push(e.fields[t]);if(t)for(let e=0;e<t.fields.length;e++)e!==n.field2-1&&i.push(t.fields[e]);return i.join(r)}function s(e){let t=e.split(`,`),n=[];for(let e of t){let t=e.trim().match(/^(\d+)\.(\d+)$/);if(!t)return null;let r=Number.parseInt(t[1],10),i=Number.parseInt(t[2],10);if(r!==1&&r!==2)return null;n.push({file:r,field:i})}return n}var c={name:`join`,execute:async(i,c)=>{if(n(i))return t(r);let l={field1:1,field2:1,separator:null,printUnpairable:new Set,onlyUnpairable:new Set,emptyString:``,outputFormat:null,ignoreCase:!1},u=[],d=0;for(;d<i.length;){let t=i[d];if(t===`-1`&&d+1<i.length){let e=Number.parseInt(i[d+1],10);if(Number.isNaN(e)||e<1)return{exitCode:1,stdout:``,stderr:`join: invalid field number: '${i[d+1]}'
`};l.field1=e,d+=2}else if(t===`-2`&&d+1<i.length){let e=Number.parseInt(i[d+1],10);if(Number.isNaN(e)||e<1)return{exitCode:1,stdout:``,stderr:`join: invalid field number: '${i[d+1]}'
`};l.field2=e,d+=2}else if((t===`-t`||t===`--field-separator`)&&d+1<i.length)l.separator=i[d+1],d+=2;else if(t.startsWith(`-t`)&&t.length>2)l.separator=t.slice(2),d++;else if(t===`-a`&&d+1<i.length){let e=Number.parseInt(i[d+1],10);if(e!==1&&e!==2)return{exitCode:1,stdout:``,stderr:`join: invalid file number: '${i[d+1]}'
`};l.printUnpairable.add(e),d+=2}else if(t.match(/^-a[12]$/))l.printUnpairable.add(Number.parseInt(t[2],10)),d++;else if(t===`-v`&&d+1<i.length){let e=Number.parseInt(i[d+1],10);if(e!==1&&e!==2)return{exitCode:1,stdout:``,stderr:`join: invalid file number: '${i[d+1]}'
`};l.onlyUnpairable.add(e),d+=2}else if(t.match(/^-v[12]$/))l.onlyUnpairable.add(Number.parseInt(t[2],10)),d++;else if(t===`-e`&&d+1<i.length)l.emptyString=i[d+1],d+=2;else if(t===`-o`&&d+1<i.length){let e=s(i[d+1]);if(!e)return{exitCode:1,stdout:``,stderr:`join: invalid field spec: '${i[d+1]}'
`};l.outputFormat=e,d+=2}else if(t===`-i`||t===`--ignore-case`)l.ignoreCase=!0,d++;else if(t===`--`){u.push(...i.slice(d+1));break}else{if(t.startsWith(`-`)&&t!==`-`)return e(`join`,t);u.push(t),d++}}if(u.length!==2)return{exitCode:1,stdout:``,stderr:u.length<2?`join: missing file operand
`:`join: extra operand
`};let f=[];for(let e of u)if(e===`-`)f.push(c.stdin??``);else{let t=c.fs.resolvePath(c.cwd,e),n=await c.fs.readFile(t);if(n===null)return{exitCode:1,stdout:``,stderr:`join: ${e}: No such file or directory
`};f.push(n)}let p=(e,t)=>{let n=e.split(`
`);return e.endsWith(`
`)&&n[n.length-1]===``&&n.pop(),n.filter(e=>e.length>0).map(e=>a(e,l.separator,t,l.ignoreCase))},m=p(f[0],l.field1),h=p(f[1],l.field2),g=new Map;for(let e of h){let t=g.get(e.joinKey);t?t.push(e):g.set(e.joinKey,[e])}let _=[],v=new Set;for(let e of m){let t=g.get(e.joinKey);if(t&&t.length>0){if(v.add(e.joinKey),l.onlyUnpairable.size===0)for(let n of t)_.push(o(e,n,l))}else (l.printUnpairable.has(1)||l.onlyUnpairable.has(1))&&_.push(o(e,null,l))}if(l.printUnpairable.has(2)||l.onlyUnpairable.has(2))for(let e of h)v.has(e.joinKey)||_.push(o(null,e,l));return{exitCode:0,stdout:_.length>0?`${_.join(`
`)}
`:``,stderr:``}}},l={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{c as n,l as t};
//# sourceMappingURL=chunk-A7ICOL4V-D5TsVU6v.mjs.map