#!/usr/bin/env node
import{createRequire} from"node:module";const require=createRequire(import.meta.url);
import{a as w,b as H}from"./chunk-IW7RU6DE.js";import{d as J}from"./chunk-MLUOPG3W.js";import{a as V,b as B}from"./chunk-LNNWMRCB.js";import{a as L}from"./chunk-HL4ZS7TX.js";import{a as T}from"./chunk-H7JTIXAO.js";import{a as I}from"./chunk-VZK4FHWJ.js";import{k as E}from"./chunk-PTT46D3Y.js";import{a as k}from"./chunk-PBOVSFTJ.js";import{a as M,b as R,c as N}from"./chunk-MUFNRCMY.js";function _(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 X(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+=_(r);continue}n+=r}return n}function D(t,n,s){return JSON.parse(X(t.slice(n,s)))}function P(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,c=r==="{"?"}":"]",p=1,m=!1,d=!1;for(s++;s<l&&p>0;){let j=t[s];d?d=!1:j==="\\"?d=!0:j==='"'?m=!m:m||(j===f?p++:j===c&&p--),s++}if(p!==0)throw new Error(`Unexpected end of JSON input at position ${s} (unclosed ${f})`);n.push(J(D(t,a,s)))}else if(r==='"'){let f=!1;for(s++;s<l;){let c=t[s];if(f)f=!1;else if(c==="\\")f=!0;else if(c==='"'){s++;break}s++}n.push(J(D(t,a,s)))}else if(r==="-"||r>="0"&&r<="9"){for(;s<l&&/[\d.eE+-]/.test(t[s]);)s++;n.push(J(D(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 G={name:"jq",summary:"command-line JSON processor",usage:"jq [OPTIONS] FILTER [FILE...]",options:["-R, --raw-input   read each line as string instead of JSON","-r, --raw-output  output strings without quotes","-c, --compact-output  compact instead of pretty-printed output","-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 C(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(p=>C(p,!0,!1,l,a)).join(",")}]`:`[
${t.map(p=>f.repeat(r+1)+C(p,!1,!1,l,a,r+1)).join(`,
`)}
${f.repeat(r)}]`;if(typeof t=="object"){let c=Object.keys(t);return l&&(c=c.sort()),c.length===0?"{}":n?`{${c.map(m=>`${JSON.stringify(m)}:${C(t[m],!0,!1,l,a)}`).join(",")}}`:`{
${c.map(m=>{let d=C(t[m],!1,!1,l,a,r+1);return`${f.repeat(r+1)}${JSON.stringify(m)}: ${d}`}).join(`,
`)}
${f.repeat(r)}}`}return String(t)}var oe={name:"jq",async execute(t,n){V(n.requireDefenseContext,"jq","execution entry");let s=(o,e)=>B(n.requireDefenseContext,"jq",o,e);if(R(t))return M(G);let l=!1,a=!1,r=!1,f=!1,c=!1,p=!1,m=!1,d=!1,j=!1,z=new Map,F=".",A=!1,x=[];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")c=!0;else if(e==="-n"||e==="--null-input")p=!0;else if(e==="-j"||e==="--join-output")m=!0;else if(!(e==="-a"||e==="--ascii")){if(e==="-S"||e==="--sort-keys")d=!0;else if(!(e==="-C"||e==="--color")){if(!(e==="-M"||e==="--monochrome"))if(e==="--tab")j=!0;else if(e==="--arg"){let i=t[o+1],$=t[o+2];if(i===void 0||$===void 0)return{stdout:"",stderr:`jq: --arg requires two arguments
`,exitCode:2};z.set(`$${i}`,$),o+=2}else if(e==="-")x.push("-");else{if(e.startsWith("--"))return N("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")c=!0;else if(i==="n")p=!0;else if(i==="j")m=!0;else if(i!=="a"){if(i==="S")d=!0;else if(i!=="C"){if(i!=="M")return N("jq",`-${i}`)}}}else A?x.push(e):(F=e,A=!0)}}}}let S=[];if(!p)if(x.length===0||x.length===1&&x[0]==="-")S.push({source:"stdin",content:I(n.stdin)});else{let o=await s("file read",()=>T(n,x,{cmdName:"jq",stopOnError:!0}));if(o.exitCode!==0)return{stdout:"",stderr:o.stderr,exitCode:2};S=o.files.map(e=>({source:e.filename||"stdin",content:I(e.content)}))}try{let o=H(F),e=[],i={limits:n.limits?{maxIterations:n.limits.maxJqIterations}:void 0,env:n.env,vars:z,coverage:n.coverage,requireDefenseContext:n.requireDefenseContext};if(p)e=w(null,o,i);else if(a&&c){let u=S.map(({content:g})=>g).join("");e=w(u,o,i)}else if(a){let u="";for(let{content:g}of S){let h=u+g,y=0,O=h.indexOf(`
`,y);for(;O!==-1;)e.push(...w(h.slice(y,O),o,i)),y=O+1,O=h.indexOf(`
`,y);u=h.slice(y)}u!==""&&e.push(...w(u,o,i))}else if(c){let u=[];for(let{content:g}of S){let h=g.trim();h&&u.push(...P(h))}e=w(u,o,i)}else for(let{content:u}of S){let g=u.trim();if(!g)continue;let h=P(g);for(let y of h)e.push(...w(y,o,i))}let $=e.map(u=>C(u,r,l,d,j)),U=m?"":`
`,b=$.join(U),q=n.limits?.maxStringLength;if(q!==void 0&&q>0&&b.length>q)throw new E(`jq: output size limit exceeded (${q} bytes)`,"string_length");let W=f&&(e.length===0||e.every(u=>u==null||u===!1))?1:0;return{stdout:b?m?b:`${b}
`:"",stderr:"",exitCode:W}}catch(o){if(o instanceof L)throw o;if(o instanceof E)return{stdout:"",stderr:`jq: ${k(o.message)}
`,exitCode:E.EXIT_CODE};let e=k(o.message);return e.includes("Unknown function")?{stdout:"",stderr:`jq: error: ${e}
`,exitCode:3}:{stdout:"",stderr:`jq: parse error: ${e}
`,exitCode:5}}}},ie={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{oe as a,ie as b};
