#!/usr/bin/env node
import{a as S,b as V}from"./chunk-TA5OOF5J.js";import{d as O}from"./chunk-V7ZOPVQS.js";import{a as R,b as T}from"./chunk-MO4RPBN2.js";import{a as F}from"./chunk-YU6OGPZR.js";import{a as M}from"./chunk-S7KZBLGH.js";import{a as E}from"./chunk-63XSH3IT.js";import{k as q}from"./chunk-DHC3ILJI.js";import{a as I}from"./chunk-RLNOQILG.js";import{a as A,b as L,c as J}from"./chunk-GTNBSMZR.js";function W(t){switch(t){case"\b":return"\\b";case"\f":return"\\f";case`
`:return"\\n";case"\r":return"\\r";case"	":return"\\t";default:return`\\u${t.charCodeAt(0).toString(16).padStart(4,"0")}`}}function _(t){let n="",s=!1,l=!1;for(let a=0;a<t.length;a++){let r=t[a];if(l){n+=r,l=!1;continue}if(r==="\\"){n+=r,l=!0;continue}if(r==='"'){n+=r,s=!s;continue}if(s&&r.charCodeAt(0)<=31){n+=W(r);continue}n+=r}return n}function N(t,n,s){return JSON.parse(_(t.slice(n,s)))}function B(t){let n=[],s=0,l=t.length;for(;s<l;){for(;s<l&&/\s/.test(t[s]);)s++;if(s>=l)break;let a=s,r=t[s];if(r==="{"||r==="["){let f=r,u=r==="{"?"}":"]",c=1,p=!1,h=!1;for(s++;s<l&&c>0;){let g=t[s];h?h=!1:g==="\\"?h=!0:g==='"'?p=!p:p||(g===f?c++:g===u&&c--),s++}if(c!==0)throw new Error(`Unexpected end of JSON input at position ${s} (unclosed ${f})`);n.push(O(N(t,a,s)))}else if(r==='"'){let f=!1;for(s++;s<l;){let u=t[s];if(f)f=!1;else if(u==="\\")f=!0;else if(u==='"'){s++;break}s++}n.push(O(N(t,a,s)))}else if(r==="-"||r>="0"&&r<="9"){for(;s<l&&/[\d.eE+-]/.test(t[s]);)s++;n.push(O(N(t,a,s)))}else if(t.slice(s,s+4)==="true")n.push(!0),s+=4;else if(t.slice(s,s+5)==="false")n.push(!1),s+=5;else if(t.slice(s,s+4)==="null")n.push(null),s+=4;else{let f=t.slice(s,s+10);throw new Error(`Invalid JSON at position ${a}: unexpected '${f.split(/\s/)[0]}'`)}}return n}var X={name:"jq",summary:"command-line JSON processor",usage:"jq [OPTIONS] FILTER [FILE]",options:["-R, --raw-input   read input as raw strings, not JSON","-r, --raw-output  output strings without quotes","-c, --compact     compact output (no pretty printing)","-e, --exit-status set exit status based on output","-s, --slurp       read entire input into array","-n, --null-input  don't read any input","-j, --join-output don't print newlines after each output","-a, --ascii       force ASCII output","-S, --sort-keys   sort object keys","-C, --color       colorize output (ignored)","-M, --monochrome  monochrome output (ignored)","    --arg name value bind $name to string value","    --tab         use tabs for indentation","    --help        display this help and exit"]};function x(t,n,s,l,a,r=0){if(t===null||t===void 0)return"null";if(typeof t=="boolean")return String(t);if(typeof t=="number")return Number.isFinite(t)?String(t):"null";if(typeof t=="string")return s?t:JSON.stringify(t);let f=a?"	":"  ";if(Array.isArray(t))return t.length===0?"[]":n?`[${t.map(c=>x(c,!0,!1,l,a)).join(",")}]`:`[
${t.map(c=>f.repeat(r+1)+x(c,!1,!1,l,a,r+1)).join(`,
`)}
${f.repeat(r)}]`;if(typeof t=="object"){let u=Object.keys(t);return l&&(u=u.sort()),u.length===0?"{}":n?`{${u.map(p=>`${JSON.stringify(p)}:${x(t[p],!0,!1,l,a)}`).join(",")}}`:`{
${u.map(p=>{let h=x(t[p],!1,!1,l,a,r+1);return`${f.repeat(r+1)}${JSON.stringify(p)}: ${h}`}).join(`,
`)}
${f.repeat(r)}}`}return String(t)}var re={name:"jq",async execute(t,n){R(n.requireDefenseContext,"jq","execution entry");let s=(o,e)=>T(n.requireDefenseContext,"jq",o,e);if(L(t))return A(X);let l=!1,a=!1,r=!1,f=!1,u=!1,c=!1,p=!1,h=!1,g=!1,k=new Map,D=".",z=!1,j=[];for(let o=0;o<t.length;o++){let e=t[o];if(e==="-R"||e==="--raw-input")a=!0;else if(e==="-r"||e==="--raw-output")l=!0;else if(e==="-c"||e==="--compact-output")r=!0;else if(e==="-e"||e==="--exit-status")f=!0;else if(e==="-s"||e==="--slurp")u=!0;else if(e==="-n"||e==="--null-input")c=!0;else if(e==="-j"||e==="--join-output")p=!0;else if(!(e==="-a"||e==="--ascii")){if(e==="-S"||e==="--sort-keys")h=!0;else if(!(e==="-C"||e==="--color")){if(!(e==="-M"||e==="--monochrome"))if(e==="--tab")g=!0;else if(e==="--arg"){let i=t[o+1],C=t[o+2];if(i===void 0||C===void 0)return{stdout:"",stderr:`jq: --arg requires two arguments
`,exitCode:2};k.set(`$${i}`,C),o+=2}else if(e==="-")j.push("-");else{if(e.startsWith("--"))return J("jq",e);if(e.startsWith("-")){for(let i of e.slice(1))if(i==="R")a=!0;else if(i==="r")l=!0;else if(i==="c")r=!0;else if(i==="e")f=!0;else if(i==="s")u=!0;else if(i==="n")c=!0;else if(i==="j")p=!0;else if(i!=="a"){if(i==="S")h=!0;else if(i!=="C"){if(i!=="M")return J("jq",`-${i}`)}}}else z?j.push(e):(D=e,z=!0)}}}}let y=[];if(!c)if(j.length===0||j.length===1&&j[0]==="-")y.push({source:"stdin",content:E(n.stdin)});else{let o=await s("file read",()=>M(n,j,{cmdName:"jq",stopOnError:!0}));if(o.exitCode!==0)return{stdout:"",stderr:o.stderr,exitCode:2};y=o.files.map(e=>({source:e.filename||"stdin",content:E(e.content)}))}try{let o=V(D),e=[],i={limits:n.limits?{maxIterations:n.limits.maxJqIterations}:void 0,env:n.env,vars:k,coverage:n.coverage,requireDefenseContext:n.requireDefenseContext};if(c)e=S(null,o,i);else if(a&&u){let m=y.map(({content:d})=>d).join("");e=S(m,o,i)}else if(a)for(let{content:m}of y){let d=m.split(`
`);d.length>0&&d.at(-1)===""&&d.pop();for(let w of d)e.push(...S(w,o,i))}else if(u){let m=[];for(let{content:d}of y){let w=d.trim();w&&m.push(...B(w))}e=S(m,o,i)}else for(let{content:m}of y){let d=m.trim();if(!d)continue;let w=B(d);for(let U of w)e.push(...S(U,o,i))}let C=e.map(m=>x(m,r,l,h,g)),H=p?"":`
`,$=C.join(H),b=n.limits?.maxStringLength;if(b!==void 0&&b>0&&$.length>b)throw new q(`jq: output size limit exceeded (${b} bytes)`,"string_length");let P=f&&(e.length===0||e.every(m=>m==null||m===!1))?1:0;return{stdout:$?p?$:`${$}
`:"",stderr:"",exitCode:P}}catch(o){if(o instanceof F)throw o;if(o instanceof q)return{stdout:"",stderr:`jq: ${I(o.message)}
`,exitCode:q.EXIT_CODE};let e=I(o.message);return e.includes("Unknown function")?{stdout:"",stderr:`jq: error: ${e}
`,exitCode:3}:{stdout:"",stderr:`jq: parse error: ${e}
`,exitCode:5}}}},oe={name:"jq",flags:[{flag:"-R",type:"boolean"},{flag:"-r",type:"boolean"},{flag:"-c",type:"boolean"},{flag:"-e",type:"boolean"},{flag:"-s",type:"boolean"},{flag:"-n",type:"boolean"},{flag:"-j",type:"boolean"},{flag:"-S",type:"boolean"},{flag:"--tab",type:"boolean"}],stdinType:"text",needsArgs:!0};export{re as a,oe as b};
