#!/usr/bin/env node
import{a as b,b as z}from"./chunk-V7VIJQD6.js";import{a as D,b as F}from"./chunk-WO4B7WUT.js";import{a as N}from"./chunk-EO5AH4U7.js";import{a as O}from"./chunk-SYG3IW7P.js";import{a as k}from"./chunk-5WFYIUU2.js";import{k as $}from"./chunk-6KZRLMG3.js";import{a as J,b as I,c as C}from"./chunk-GTNBSMZR.js";function L(s){let r=[],t=0,l=s.length;for(;t<l;){for(;t<l&&/\s/.test(s[t]);)t++;if(t>=l)break;let u=t,n=s[t];if(n==="{"||n==="["){let i=n,a=n==="{"?"}":"]",c=1,p=!1,d=!1;for(t++;t<l&&c>0;){let h=s[t];d?d=!1:h==="\\"?d=!0:h==='"'?p=!p:p||(h===i?c++:h===a&&c--),t++}if(c!==0)throw new Error(`Unexpected end of JSON input at position ${t} (unclosed ${i})`);r.push(JSON.parse(s.slice(u,t)))}else if(n==='"'){let i=!1;for(t++;t<l;){let a=s[t];if(i)i=!1;else if(a==="\\")i=!0;else if(a==='"'){t++;break}t++}r.push(JSON.parse(s.slice(u,t)))}else if(n==="-"||n>="0"&&n<="9"){for(;t<l&&/[\d.eE+-]/.test(s[t]);)t++;r.push(JSON.parse(s.slice(u,t)))}else if(s.slice(t,t+4)==="true")r.push(!0),t+=4;else if(s.slice(t,t+5)==="false")r.push(!1),t+=5;else if(s.slice(t,t+4)==="null")r.push(null),t+=4;else{let i=s.slice(t,t+10);throw new Error(`Invalid JSON at position ${u}: unexpected '${i.split(/\s/)[0]}'`)}}return r}var W={name:"jq",summary:"command-line JSON processor",usage:"jq [OPTIONS] FILTER [FILE]",options:["-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)","    --tab         use tabs for indentation","    --help        display this help and exit"]};function j(s,r,t,l,u,n=0){if(s===null||s===void 0)return"null";if(typeof s=="boolean")return String(s);if(typeof s=="number")return Number.isFinite(s)?String(s):"null";if(typeof s=="string")return t?s:JSON.stringify(s);let i=u?"	":"  ";if(Array.isArray(s))return s.length===0?"[]":r?`[${s.map(c=>j(c,!0,!1,l,u)).join(",")}]`:`[
${s.map(c=>i.repeat(n+1)+j(c,!1,!1,l,u,n+1)).join(`,
`)}
${i.repeat(n)}]`;if(typeof s=="object"){let a=Object.keys(s);return l&&(a=a.sort()),a.length===0?"{}":r?`{${a.map(p=>`${JSON.stringify(p)}:${j(s[p],!0,!1,l,u)}`).join(",")}}`:`{
${a.map(p=>{let d=j(s[p],!1,!1,l,u,n+1);return`${i.repeat(n+1)}${JSON.stringify(p)}: ${d}`}).join(`,
`)}
${i.repeat(n)}}`}return String(s)}var G={name:"jq",async execute(s,r){D(r.requireDefenseContext,"jq","execution entry");let t=(o,e)=>F(r.requireDefenseContext,"jq",o,e);if(I(s))return J(W);let l=!1,u=!1,n=!1,i=!1,a=!1,c=!1,p=!1,d=!1,h=".",E=!1,g=[];for(let o=0;o<s.length;o++){let e=s[o];if(e==="-r"||e==="--raw-output")l=!0;else if(e==="-c"||e==="--compact-output")u=!0;else if(e==="-e"||e==="--exit-status")n=!0;else if(e==="-s"||e==="--slurp")i=!0;else if(e==="-n"||e==="--null-input")a=!0;else if(e==="-j"||e==="--join-output")c=!0;else if(!(e==="-a"||e==="--ascii")){if(e==="-S"||e==="--sort-keys")p=!0;else if(!(e==="-C"||e==="--color")){if(!(e==="-M"||e==="--monochrome"))if(e==="--tab")d=!0;else if(e==="-")g.push("-");else{if(e.startsWith("--"))return C("jq",e);if(e.startsWith("-")){for(let f of e.slice(1))if(f==="r")l=!0;else if(f==="c")u=!0;else if(f==="e")n=!0;else if(f==="s")i=!0;else if(f==="n")a=!0;else if(f==="j")c=!0;else if(f!=="a"){if(f==="S")p=!0;else if(f!=="C"){if(f!=="M")return C("jq",`-${f}`)}}}else E?g.push(e):(h=e,E=!0)}}}}let y=[];if(!a)if(g.length===0||g.length===1&&g[0]==="-")y.push({source:"stdin",content:r.stdin});else{let o=await t("file read",()=>k(r,g,{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.content}))}try{let o=z(h),e=[],f={limits:r.limits?{maxIterations:r.limits.maxJqIterations}:void 0,env:r.env,coverage:r.coverage,requireDefenseContext:r.requireDefenseContext};if(a)e=b(null,o,f);else if(i){let m=[];for(let{content:x}of y){let q=x.trim();q&&m.push(...L(q))}e=b(m,o,f)}else for(let{content:m}of y){let x=m.trim();if(!x)continue;let q=L(x);for(let H of q)e.push(...b(H,o,f))}let A=e.map(m=>j(m,u,l,p,d)),M=c?"":`
`,S=A.join(M),w=r.limits?.maxStringLength;if(w!==void 0&&w>0&&S.length>w)throw new $(`jq: output size limit exceeded (${w} bytes)`,"string_length");let V=n&&(e.length===0||e.every(m=>m==null||m===!1))?1:0;return{stdout:S?c?S:`${S}
`:"",stderr:"",exitCode:V}}catch(o){if(o instanceof N)throw o;if(o instanceof $)return{stdout:"",stderr:`jq: ${O(o.message)}
`,exitCode:$.EXIT_CODE};let e=O(o.message);return e.includes("Unknown function")?{stdout:"",stderr:`jq: error: ${e}
`,exitCode:3}:{stdout:"",stderr:`jq: parse error: ${e}
`,exitCode:5}}}},Q={name:"jq",flags:[{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:"json",needsArgs:!0};export{G as a,Q as b};
