import{createRequire} from"node:module";const require=createRequire(import.meta.url); import{a as g,b as A,c as T,d as at,e as E,g as O,h as G}from"./chunk-4WKZNNJK.js";import{a as X,c as lt}from"./chunk-DLL7UR66.js";import{a as yt}from"./chunk-KAPG5ONA.js";import{a as R}from"./chunk-7CSMHFIJ.js";import{k as B}from"./chunk-SAI2SPQQ.js";function W(t,r,e,n,c,o,u,p,s,f){switch(r){case"sort":return Array.isArray(t)?[[...t].sort(u)]:[null];case"sort_by":return!Array.isArray(t)||e.length===0?[null]:[[...t].sort((h,a)=>{let y=c(h,e[0],n)[0],l=c(a,e[0],n)[0];return u(y,l)})];case"bsearch":{if(!Array.isArray(t)){let h=t===null?"null":typeof t=="object"?"object":typeof t;throw new Error(`${h} (${JSON.stringify(t)}) cannot be searched from`)}return e.length===0?[null]:c(t,e[0],n).map(h=>{let a=0,y=t.length;for(;a>>1;u(t[l],h)<0?a=l+1:y=l}return au(a.key,y.key)),[h.map(a=>a.item)]}case"group_by":{if(!Array.isArray(t)||e.length===0)return[null];let i=new Map;for(let h of t){let a=JSON.stringify(c(h,e[0],n)[0]);i.has(a)||i.set(a,[]),i.get(a)?.push(h)}return[[...i.values()]]}case"max":return Array.isArray(t)&&t.length>0?[t.reduce((i,h)=>u(i,h)>0?i:h)]:[null];case"max_by":return!Array.isArray(t)||t.length===0||e.length===0?[null]:[t.reduce((i,h)=>{let a=c(i,e[0],n)[0],y=c(h,e[0],n)[0];return u(a,y)>0?i:h})];case"min":return Array.isArray(t)&&t.length>0?[t.reduce((i,h)=>u(i,h)<0?i:h)]:[null];case"min_by":return!Array.isArray(t)||t.length===0||e.length===0?[null]:[t.reduce((i,h)=>{let a=c(i,e[0],n)[0],y=c(h,e[0],n)[0];return u(a,y)<0?i:h})];case"add":{let i=h=>{let a=h.filter(y=>y!==null);return a.length===0?null:a.every(y=>typeof y=="number")?a.reduce((y,l)=>y+l,0):a.every(y=>typeof y=="string")?a.join(""):a.every(y=>Array.isArray(y))?a.flat():a.every(y=>y&&typeof y=="object"&&!Array.isArray(y))?lt(...a):null};if(e.length>=1){let h=c(t,e[0],n);return[i(h)]}return Array.isArray(t)?[i(t)]:[null]}case"any":{if(e.length>=2){try{let i=o(t,e[0],n);for(let h of i)if(c(h,e[1],n).some(p))return[!0]}catch(i){if(i instanceof f)throw i}return[!1]}return e.length===1?Array.isArray(t)?[t.some(i=>p(c(i,e[0],n)[0]))]:[!1]:Array.isArray(t)?[t.some(p)]:[!1]}case"all":{if(e.length>=2){try{let i=o(t,e[0],n);for(let h of i)if(!c(h,e[1],n).some(p))return[!1]}catch(i){if(i instanceof f)throw i}return[!0]}return e.length===1?Array.isArray(t)?[t.every(i=>p(c(i,e[0],n)[0]))]:[!0]:Array.isArray(t)?[t.every(p)]:[!0]}case"select":return e.length===0?[t]:c(t,e[0],n).some(p)?[t]:[];case"map":return e.length===0||!Array.isArray(t)?[null]:[t.flatMap(h=>c(h,e[0],n))];case"map_values":{if(e.length===0)return[null];if(Array.isArray(t))return[t.flatMap(i=>c(i,e[0],n))];if(t&&typeof t=="object"){let i=Object.create(null);for(let[h,a]of Object.entries(t)){if(!g(h))continue;let y=c(a,e[0],n);y.length>0&&A(i,h,y[0])}return[i]}return[null]}case"has":{if(e.length===0)return[!1];let h=c(t,e[0],n)[0];return Array.isArray(t)&&typeof h=="number"?[h>=0&&h=0&&t0)try{let s=o(t,e[0],n);return s.length>0?[s[0]]:[]}catch(s){if(s instanceof p)throw s;return[]}return Array.isArray(t)&&t.length>0?[t[0]]:[null];case"last":if(e.length>0){let s=c(t,e[0],n);return s.length>0?[s[s.length-1]]:[]}return Array.isArray(t)&&t.length>0?[t[t.length-1]]:[null];case"nth":{if(e.length<1)return[null];let s=c(t,e[0],n);if(e.length>1){for(let i of s)if(i<0)throw new Error("nth doesn't support negative indices");let f;try{f=o(t,e[1],n)}catch(i){if(i instanceof p)throw i;f=[]}return s.flatMap(i=>{let h=i;return h{let i=f;if(i<0)throw new Error("nth doesn't support negative indices");return il.length>=s,i=c(t,e[0],n);if(e.length===1){let l=[];for(let m of i){let b=m;for(let w=0;w0){for(let C=w;Ck;C+=N)if(y.push(C),f(y))return y}}return y}case"limit":return e.length<2?[]:c(t,e[0],n).flatMap(f=>{let i=f;if(i<0)throw new Error("limit doesn't support negative count");if(i===0)return[];let h;try{h=o(t,e[1],n)}catch(a){if(a instanceof p)throw a;h=[]}return h.slice(0,i)});case"isempty":{if(e.length<1)return[!0];try{return[o(t,e[0],n).length===0]}catch(s){if(s instanceof p)throw s;return[!0]}}case"isvalid":{if(e.length<1)return[!0];try{return[c(t,e[0],n).length>0]}catch(s){if(s instanceof p)throw s;return[!1]}}case"skip":return e.length<2?[]:c(t,e[0],n).flatMap(f=>{let i=f;if(i<0)throw new Error("skip doesn't support negative count");return c(t,e[1],n).slice(i)});case"until":{if(e.length<2)return[t];let s=t,f=n.limits.maxIterations;for(let i=0;i=i)throw new p(`jq while: too many iterations (${i}), increase executionLimits.maxJqIterations`,"iterations");return s}case"repeat":{if(e.length===0)return[t];let s=[],f=t,i=n.limits.maxIterations;for(let h=0;h=i)throw new p(`jq repeat: too many iterations (${i}), increase executionLimits.maxJqIterations`,"iterations");return s}default:return null}}function Z(t,r,e,n,c){switch(r){case"now":return[Date.now()/1e3];case"gmtime":{if(typeof t!="number")return[null];let o=new Date(t*1e3),u=o.getUTCFullYear(),p=o.getUTCMonth(),s=o.getUTCDate(),f=o.getUTCHours(),i=o.getUTCMinutes(),h=o.getUTCSeconds(),a=o.getUTCDay(),y=Date.UTC(u,0,1),l=Math.floor((o.getTime()-y)/(1440*60*1e3));return[[u,p,s,f,i,h,a,l]]}case"mktime":{if(!Array.isArray(t))throw new Error("mktime requires parsed datetime inputs");let[o,u,p,s=0,f=0,i=0]=t;if(typeof o!="number"||typeof u!="number")throw new Error("mktime requires parsed datetime inputs");let h=Date.UTC(o,u,p??1,s??0,f??0,i??0);return[Math.floor(h/1e3)]}case"strftime":{if(e.length===0)return[null];let u=c(t,e[0],n)[0];if(typeof u!="string")throw new Error("strftime/1 requires a string format");let p;if(typeof t=="number")p=new Date(t*1e3);else if(Array.isArray(t)){let[a,y,l,m=0,b=0,w=0]=t;if(typeof a!="number"||typeof y!="number")throw new Error("strftime/1 requires parsed datetime inputs");p=new Date(Date.UTC(a,y,l??1,m??0,b??0,w??0))}else throw new Error("strftime/1 requires parsed datetime inputs");let s=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],f=["January","February","March","April","May","June","July","August","September","October","November","December"],i=(a,y=2)=>String(a).padStart(y,"0");return[u.replace(/%Y/g,String(p.getUTCFullYear())).replace(/%m/g,i(p.getUTCMonth()+1)).replace(/%d/g,i(p.getUTCDate())).replace(/%H/g,i(p.getUTCHours())).replace(/%M/g,i(p.getUTCMinutes())).replace(/%S/g,i(p.getUTCSeconds())).replace(/%A/g,s[p.getUTCDay()]).replace(/%B/g,f[p.getUTCMonth()]).replace(/%Z/g,"UTC").replace(/%%/g,"%")]}case"strptime":{if(e.length===0)return[null];if(typeof t!="string")throw new Error("strptime/1 requires a string input");let u=c(t,e[0],n)[0];if(typeof u!="string")throw new Error("strptime/1 requires a string format");if(u==="%Y-%m-%dT%H:%M:%SZ"){let s=t.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/);if(s){let[,f,i,h,a,y,l]=s.map(Number),m=new Date(Date.UTC(f,i-1,h,a,y,l)),b=m.getUTCDay(),w=Date.UTC(f,0,1),k=Math.floor((m.getTime()-w)/(1440*60*1e3));return[[f,i-1,h,a,y,l,b,k]]}}let p=new Date(t);if(!Number.isNaN(p.getTime())){let s=p.getUTCFullYear(),f=p.getUTCMonth(),i=p.getUTCDate(),h=p.getUTCHours(),a=p.getUTCMinutes(),y=p.getUTCSeconds(),l=p.getUTCDay(),m=Date.UTC(s,0,1),b=Math.floor((p.getTime()-m)/(1440*60*1e3));return[[s,f,i,h,a,y,l,b]]}throw new Error(`Cannot parse date: ${t}`)}case"fromdate":{if(typeof t!="string")throw new Error("fromdate requires a string input");let o=new Date(t);if(Number.isNaN(o.getTime()))throw new Error(`date "${t}" does not match format "%Y-%m-%dT%H:%M:%SZ"`);return[Math.floor(o.getTime()/1e3)]}case"todate":{if(typeof t!="number")throw new Error("todate requires a number input");return[new Date(t*1e3).toISOString().replace(/\.\d{3}Z$/,"Z")]}default:return null}}function S(t){return t!==!1&&t!==null}function I(t,r){return JSON.stringify(t)===JSON.stringify(r)}function U(t,r){return typeof t=="number"&&typeof r=="number"?t-r:typeof t=="string"&&typeof r=="string"?t.localeCompare(r):0}function v(t,r){let e=O(t);for(let n of Object.keys(r)){if(!g(n))continue;let c=T(e,n)?E(e[n]):null,o=E(r[n]);c&&o?A(e,n,v(c,o)):A(e,n,r[n])}return e}function D(t,r=3e3){let e=0,n=t;for(;ep===null?0:typeof p=="boolean"?1:typeof p=="number"?2:typeof p=="string"?3:Array.isArray(p)?4:typeof p=="object"?5:6,n=e(t),c=e(r);if(n!==c)return n-c;if(typeof t=="number"&&typeof r=="number")return t-r;if(typeof t=="string"&&typeof r=="string")return t.localeCompare(r);if(typeof t=="boolean"&&typeof r=="boolean")return(t?1:0)-(r?1:0);if(Array.isArray(t)&&Array.isArray(r)){for(let p=0;pt.some(o=>F(o,c)));let e=E(t),n=E(r);return e&&n?Object.keys(n).every(c=>T(e,c)&&F(e[c],n[c])):!1}var Ot=2e3;function tt(t,r,e){switch(r){case"@base64":return typeof t=="string"?typeof Buffer<"u"?[Buffer.from(t,"utf-8").toString("base64")]:[btoa(t)]:[null];case"@base64d":return typeof t=="string"?typeof Buffer<"u"?[Buffer.from(t,"base64").toString("utf-8")]:[atob(t)]:[null];case"@uri":return typeof t=="string"?[encodeURIComponent(t).replace(/!/g,"%21").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29").replace(/\*/g,"%2A")]:[null];case"@urid":return typeof t=="string"?[decodeURIComponent(t)]:[null];case"@csv":return Array.isArray(t)?[t.map(c=>{if(c===null)return"";if(typeof c=="boolean")return c?"true":"false";if(typeof c=="number")return String(c);let o=String(c);return o.includes(",")||o.includes('"')||o.includes(` `)||o.includes("\r")?`"${o.replace(/"/g,'""')}"`:o}).join(",")]:[null];case"@tsv":return Array.isArray(t)?[t.map(n=>String(n??"").replace(/\t/g,"\\t").replace(/\n/g,"\\n")).join(" ")]:[null];case"@json":{let n=e??Ot;return D(t,n+1)>n?[null]:[JSON.stringify(t)]}case"@html":return typeof t=="string"?[t.replace(/&/g,"&").replace(//g,">").replace(/'/g,"'").replace(/"/g,""")]:[null];case"@sh":return typeof t=="string"?[`'${t.replace(/'/g,"'\\''")}'`]:[null];case"@text":return typeof t=="string"?[t]:t==null?[""]:[String(t)];default:return null}}function et(t,r,e,n,c,o){switch(r){case"index":return e.length===0?[null]:c(t,e[0],n).map(p=>{if(typeof t=="string"&&typeof p=="string"){if(p===""&&t==="")return null;let s=t.indexOf(p);return s>=0?s:null}if(Array.isArray(t)){if(Array.isArray(p)){for(let f=0;f<=t.length-p.length;f++){let i=!0;for(let h=0;ho(f,p));return s>=0?s:null}return null});case"rindex":return e.length===0?[null]:c(t,e[0],n).map(p=>{if(typeof t=="string"&&typeof p=="string"){let s=t.lastIndexOf(p);return s>=0?s:null}if(Array.isArray(t)){if(Array.isArray(p)){for(let s=t.length-p.length;s>=0;s--){let f=!0;for(let i=0;i=0;s--)if(o(t[s],p))return s;return null}return null});case"indices":return e.length===0?[[]]:c(t,e[0],n).map(p=>{let s=[];if(typeof t=="string"&&typeof p=="string"){let f=t.indexOf(p);for(;f!==-1;)s.push(f),f=t.indexOf(p,f+1)}else if(Array.isArray(t))if(Array.isArray(p)){let f=p.length;if(f===0)for(let i=0;i<=t.length;i++)s.push(i);else for(let i=0;i<=t.length-f;i++){let h=!0;for(let a=0;a{if(y.push(m),Array.isArray(m))for(let b of m)l(b);else if(m&&typeof m=="object")for(let b of Object.keys(m))l(m[b])};return l(t),y}let s=[],f=e.length>=2?e[1]:null,i=1e4,h=0,a=y=>{if(h++>i||f&&!c(y,f,n).some(o))return;s.push(y);let l=c(y,e[0],n);for(let m of l)m!=null&&a(m)};return a(t),s}case"recurse_down":return p(t,"recurse",e,n);case"walk":{if(e.length===0)return[t];let s=new WeakSet,f=i=>{if(i&&typeof i=="object"){if(s.has(i))return i;s.add(i)}let h;if(Array.isArray(i))h=i.map(f);else if(i&&typeof i=="object"){let y=Object.create(null);for(let[l,m]of Object.entries(i))g(l)&&A(y,l,f(m));h=y}else h=i;return c(h,e[0],n)[0]};return[f(t)]}case"transpose":{if(!Array.isArray(t))return[null];if(t.length===0)return[[]];let s=Math.max(...t.map(i=>Array.isArray(i)?i.length:0)),f=[];for(let i=0;iArray.isArray(h)?h[i]:null));return[f]}case"combinations":{if(e.length>0){let h=c(t,e[0],n)[0];if(!Array.isArray(t)||h<0)return[];if(h===0)return[[]];let a=[],y=(l,m)=>{if(m===h){a.push([...l]);return}for(let b of t)l.push(b),y(l,m+1),l.pop()};return y([],0),a}if(!Array.isArray(t))return[];if(t.length===0)return[[]];for(let i of t)if(!Array.isArray(i))return[];let s=[],f=(i,h)=>{if(i===t.length){s.push([...h]);return}let a=t[i];for(let y of a)h.push(y),f(i+1,h),h.pop()};return f(0,[]),s}case"parent":{if(n.root===void 0||n.currentPath===void 0)return[];let s=n.currentPath;if(s.length===0)return[];let f=e.length>0?c(t,e[0],n)[0]:1;if(f>=0){if(f>s.length)return[];let i=s.slice(0,s.length-f);return[u(n.root,i)]}else{let i=-f-1;if(i>=s.length)return[t];let h=s.slice(0,i);return[u(n.root,h)]}}case"parents":{if(n.root===void 0||n.currentPath===void 0)return[[]];let s=n.currentPath,f=[];for(let i=s.length-1;i>=0;i--)f.push(u(n.root,s.slice(0,i)));return[f]}case"root":return n.root!==void 0?[n.root]:[];default:return null}}var Nt=2e3;function st(t,r,e,n,c){switch(r){case"keys":return Array.isArray(t)?[t.map((o,u)=>u)]:t&&typeof t=="object"?[Object.keys(t).sort()]:[null];case"keys_unsorted":return Array.isArray(t)?[t.map((o,u)=>u)]:t&&typeof t=="object"?[Object.keys(t)]:[null];case"length":return typeof t=="string"?[t.length]:Array.isArray(t)?[t.length]:t&&typeof t=="object"?[Object.keys(t).length]:t===null?[0]:typeof t=="number"?[Math.abs(t)]:[null];case"utf8bytelength":{if(typeof t=="string")return[new TextEncoder().encode(t).length];let o=t===null?"null":Array.isArray(t)?"array":typeof t,u=o==="array"||o==="object"?JSON.stringify(t):String(t);throw new Error(`${o} (${u}) only strings have UTF-8 byte length`)}case"to_entries":{let o=E(t);return o?[Object.entries(o).map(([u,p])=>({key:u,value:p}))]:[null]}case"from_entries":if(Array.isArray(t)){let o=Object.create(null);for(let u of t){let p=E(u);if(p){let s=p.key??p.Key??p.name??p.Name??p.k,f=p.value??p.Value??p.v;if(s!==void 0){let i=String(s);g(i)&&A(o,i,f)}}}return[o]}return[null];case"with_entries":{if(e.length===0)return[t];let o=E(t);if(o){let p=Object.entries(o).map(([f,i])=>({key:f,value:i})).flatMap(f=>c(f,e[0],n)),s=Object.create(null);for(let f of p){let i=E(f);if(i){let h=i.key??i.name??i.k,a=i.value??i.v;if(h!==void 0){let y=String(h);g(y)&&A(s,y,a)}}}return[s]}return[null]}case"reverse":return Array.isArray(t)?[[...t].reverse()]:typeof t=="string"?[t.split("").reverse().join("")]:[null];case"flatten":return Array.isArray(t)?(e.length>0?c(t,e[0],n):[Number.POSITIVE_INFINITY]).map(u=>{let p=u;if(p<0)throw new Error("flatten depth must not be negative");return t.flat(p)}):[null];case"unique":if(Array.isArray(t)){let o=new Set,u=[];for(let p of t){let s=JSON.stringify(p);o.has(s)||(o.add(s),u.push(p))}return[u]}return[null];case"tojson":case"tojsonstream":{let o=n.limits.maxDepth??Nt;return D(t,o+1)>o?[null]:[JSON.stringify(t)]}case"fromjson":{if(typeof t=="string"){let o=t.trim().toLowerCase();if(o==="nan")return[Number.NaN];if(o==="inf"||o==="infinity")return[Number.POSITIVE_INFINITY];if(o==="-inf"||o==="-infinity")return[Number.NEGATIVE_INFINITY];try{return[at(JSON.parse(t))]}catch{throw new Error(`Invalid JSON: ${t}`)}}return[t]}case"tostring":return typeof t=="string"?[t]:[JSON.stringify(t)];case"tonumber":if(typeof t=="number")return[t];if(typeof t=="string"){let o=Number(t);if(Number.isNaN(o))throw new Error(`${JSON.stringify(t)} cannot be parsed as a number`);return[o]}throw new Error(`${typeof t} cannot be parsed as a number`);case"toboolean":{if(typeof t=="boolean")return[t];if(typeof t=="string"){if(t==="true")return[!0];if(t==="false")return[!1];throw new Error(`string (${JSON.stringify(t)}) cannot be parsed as a boolean`)}let o=t===null?"null":Array.isArray(t)?"array":typeof t,u=o==="array"||o==="object"?JSON.stringify(t):String(t);throw new Error(`${o} (${u}) cannot be parsed as a boolean`)}case"tostream":{let o=[],u=(p,s)=>{if(p===null||typeof p!="object")o.push([s,p]);else if(Array.isArray(p))if(p.length===0)o.push([s,[]]);else for(let f=0;fo&&u.push([f.slice(o)]);continue}if(s.length===2&&Array.isArray(s[0])){let f=s[0],i=s[1];f.length>o&&u.push([f.slice(o),i])}}return u}default:return null}}function it(t,r,e,n,c,o,u,p,s,f){switch(r){case"getpath":{if(e.length===0)return[null];let i=c(t,e[0],n),h=[];for(let a of i){let y=a,l=t;for(let m of y){if(l==null){l=null;break}if(Array.isArray(l)&&typeof m=="number")l=l[m];else if(typeof m=="string"){let b=E(l);if(!b||!Object.hasOwn(b,m)){l=null;break}l=b[m]}else{l=null;break}}h.push(l)}return h}case"setpath":{if(e.length<2)return[null];let h=c(t,e[0],n)[0],y=c(t,e[1],n)[0];return[u(t,h,y)]}case"delpaths":{if(e.length===0)return[t];let h=c(t,e[0],n)[0],a=t;for(let y of h.sort((l,m)=>m.length-l.length))a=p(a,y);return[a]}case"path":{if(e.length===0)return[[]];let i=[];return f(t,e[0],n,[],i),i}case"del":return e.length===0?[t]:[s(t,e[0],n)];case"pick":{if(e.length===0)return[null];let i=[];for(let a of e)f(t,a,n,[],i);let h=null;for(let a of i){for(let l of a)if(typeof l=="number"&&l<0)throw new Error("Out of bounds negative array index");let y=t;for(let l of a){if(y==null)break;if(Array.isArray(y)&&typeof l=="number")y=y[l];else if(typeof l=="string"){let m=E(y);if(!m||!Object.hasOwn(m,l)){y=null;break}y=m[l]}else{y=null;break}}h=u(h,a,y)}return[h]}case"paths":{let i=[],h=(a,y)=>{if(a&&typeof a=="object")if(Array.isArray(a))for(let l=0;l0?i.filter(a=>{let y=t;for(let m of a)if(Array.isArray(y)&&typeof m=="number")y=y[m];else if(typeof m=="string"){let b=E(y);if(!b||!Object.hasOwn(b,m))return!1;y=b[m]}else return!1;return c(y,e[0],n).some(o)}):i}case"leaf_paths":{let i=[],h=(a,y)=>{if(a===null||typeof a!="object")i.push(y);else if(Array.isArray(a))for(let l=0;lJSON.stringify(f)));for(let f of u)if(s.has(JSON.stringify(f)))return[!0];return[!1]}case"INDEX":{if(e.length===0)return[Object.create(null)];if(e.length===1){let s=c(t,e[0],n),f=Object.create(null);for(let i of s){let h=String(i);g(h)&&A(f,h,i)}return[f]}if(e.length===2){let s=c(t,e[0],n),f=Object.create(null);for(let i of s){let h=c(i,e[1],n);if(h.length>0){let a=String(h[0]);g(a)&&A(f,a,i)}}return[f]}let u=c(t,e[0],n),p=Object.create(null);for(let s of u){let f=c(s,e[1],n),i=c(s,e[2],n);if(f.length>0&&i.length>0){let h=String(f[0]);g(h)&&A(p,h,i[0])}}return[p]}case"JOIN":{if(e.length<2)return[null];let u=E(c(t,e[0],n)[0]);if(!u)return[null];if(!Array.isArray(t))return[null];let p=[];for(let s of t){let f=c(s,e[1],n),i=f.length>0?String(f[0]):"",h=T(u,i)?u[i]:null;p.push([s,h])}return[p]}default:return null}}function ft(t,r,e,n,c){switch(r){case"join":{if(!Array.isArray(t))return[null];let o=e.length>0?c(t,e[0],n):[""];for(let u of t)if(Array.isArray(u)||u!==null&&typeof u=="object")throw new Error("cannot join: contains arrays or objects");return o.map(u=>t.map(p=>p===null?"":typeof p=="string"?p:String(p)).join(String(u)))}case"split":{if(typeof t!="string"||e.length===0)return[null];let o=c(t,e[0],n),u=String(o[0]);return[t.split(u)]}case"splits":{if(typeof t!="string"||e.length===0)return[];let o=c(t,e[0],n),u=String(o[0]);try{let p=e.length>1?String(c(t,e[1],n)[0]):"g";return R(u,p.includes("g")?p:`${p}g`).split(t)}catch{return[]}}case"scan":{if(typeof t!="string"||e.length===0)return[];let o=c(t,e[0],n),u=String(o[0]);try{let p=e.length>1?String(c(t,e[1],n)[0]):"";return[...R(u,p.includes("g")?p:`${p}g`).matchAll(t)].map(i=>i.length>1?i.slice(1):i[0])}catch{return[]}}case"test":{if(typeof t!="string"||e.length===0)return[!1];let o=c(t,e[0],n),u=String(o[0]);try{let p=e.length>1?String(c(t,e[1],n)[0]):"";return[R(u,p).test(t)]}catch{return[!1]}}case"match":{if(typeof t!="string"||e.length===0)return[null];let o=c(t,e[0],n),u=String(o[0]);try{let p=e.length>1?String(c(t,e[1],n)[0]):"",f=R(u,`${p}d`).exec(t);if(!f)return[];let i=f.indices;return[{offset:f.index,length:f[0].length,string:f[0],captures:f.slice(1).map((h,a)=>({offset:i?.[a+1]?.[0]??null,length:h?.length??0,string:h??"",name:null}))}]}catch{return[null]}}case"capture":{if(typeof t!="string"||e.length===0)return[null];let o=c(t,e[0],n),u=String(o[0]);try{let p=e.length>1?String(c(t,e[1],n)[0]):"",f=R(u,p).match(t);return!f||!f.groups?[Object.create(null)]:[f.groups]}catch{return[null]}}case"sub":{if(typeof t!="string"||e.length<2)return[null];let o=c(t,e[0],n),u=c(t,e[1],n),p=String(o[0]),s=String(u[0]);try{let f=e.length>2?String(c(t,e[2],n)[0]):"";return[R(p,f).replace(t,s)]}catch{return[t]}}case"gsub":{if(typeof t!="string"||e.length<2)return[null];let o=c(t,e[0],n),u=c(t,e[1],n),p=String(o[0]),s=String(u[0]);try{let f=e.length>2?String(c(t,e[2],n)[0]):"g",i=f.includes("g")?f:`${f}g`;return[R(p,i).replace(t,s)]}catch{return[t]}}case"ascii_downcase":return typeof t=="string"?[t.replace(/[A-Z]/g,o=>String.fromCharCode(o.charCodeAt(0)+32))]:[null];case"ascii_upcase":return typeof t=="string"?[t.replace(/[a-z]/g,o=>String.fromCharCode(o.charCodeAt(0)-32))]:[null];case"ltrimstr":{if(typeof t!="string"||e.length===0)return[t];let o=c(t,e[0],n),u=String(o[0]);return[t.startsWith(u)?t.slice(u.length):t]}case"rtrimstr":{if(typeof t!="string"||e.length===0)return[t];let o=c(t,e[0],n),u=String(o[0]);return u===""?[t]:[t.endsWith(u)?t.slice(0,-u.length):t]}case"trimstr":{if(typeof t!="string"||e.length===0)return[t];let o=c(t,e[0],n),u=String(o[0]);if(u==="")return[t];let p=t;return p.startsWith(u)&&(p=p.slice(u.length)),p.endsWith(u)&&(p=p.slice(0,-u.length)),[p]}case"trim":if(typeof t=="string")return[t.trim()];throw new Error("trim input must be a string");case"ltrim":if(typeof t=="string")return[t.trimStart()];throw new Error("trim input must be a string");case"rtrim":if(typeof t=="string")return[t.trimEnd()];throw new Error("trim input must be a string");case"startswith":{if(typeof t!="string"||e.length===0)return[!1];let o=c(t,e[0],n);return[t.startsWith(String(o[0]))]}case"endswith":{if(typeof t!="string"||e.length===0)return[!1];let o=c(t,e[0],n);return[t.endsWith(String(o[0]))]}case"ascii":return typeof t=="string"&&t.length>0?[t.charCodeAt(0)]:[null];case"explode":return typeof t=="string"?[Array.from(t).map(o=>o.codePointAt(0))]:[null];case"implode":if(!Array.isArray(t))throw new Error("implode input must be an array");return[t.map(p=>{if(typeof p=="string")throw new Error(`string (${JSON.stringify(p)}) can't be imploded, unicode codepoint needs to be numeric`);if(typeof p!="number"||Number.isNaN(p))throw new Error("number (null) can't be imploded, unicode codepoint needs to be numeric");let s=Math.trunc(p);return s<0||s>1114111||s>=55296&&s<=57343?String.fromCodePoint(65533):String.fromCodePoint(s)}).join("")];default:return null}}function ct(t,r){switch(r){case"type":return t===null?["null"]:Array.isArray(t)?["array"]:typeof t=="boolean"?["boolean"]:typeof t=="number"?["number"]:typeof t=="string"?["string"]:typeof t=="object"?["object"]:["null"];case"infinite":return[Number.POSITIVE_INFINITY];case"nan":return[Number.NaN];case"isinfinite":return[typeof t=="number"&&!Number.isFinite(t)];case"isnan":return[typeof t=="number"&&Number.isNaN(t)];case"isnormal":return[typeof t=="number"&&Number.isFinite(t)&&t!==0];case"isfinite":return[typeof t=="number"&&Number.isFinite(t)];case"numbers":return typeof t=="number"?[t]:[];case"strings":return typeof t=="string"?[t]:[];case"booleans":return typeof t=="boolean"?[t]:[];case"nulls":return t===null?[t]:[];case"arrays":return Array.isArray(t)?[t]:[];case"objects":return t&&typeof t=="object"&&!Array.isArray(t)?[t]:[];case"iterables":return Array.isArray(t)||t&&typeof t=="object"&&!Array.isArray(t)?[t]:[];case"scalars":return!Array.isArray(t)&&!(t&&typeof t=="object")?[t]:[];case"values":return t===null?[]:[t];case"not":return t===!1||t===null?[!0]:[!1];case"null":return[null];case"true":return[!0];case"false":return[!1];case"empty":return[];default:return null}}function K(t,r,e){if(r.length===0)return e;let[n,...c]=r;if(typeof n=="number"){if(t&&typeof t=="object"&&!Array.isArray(t))throw new Error("Cannot index object with number");if(n>536870911)throw new Error("Array index too large");if(n<0)throw new Error("Out of bounds negative array index");let f=Array.isArray(t)?[...t]:[];for(;f.length<=n;)f.push(null);return f[n]=K(f[n],c,e),f}if(Array.isArray(t))throw new Error("Cannot index array with string");if(!g(n))return t??Object.create(null);let o=E(t),u=o?O(o):Object.create(null),p=Object.hasOwn(u,n)?u[n]:void 0;return A(u,n,K(p,c,e)),u}function V(t,r){if(r.length===0)return null;if(r.length===1){let c=r[0];if(Array.isArray(t)&&typeof c=="number"){let o=[...t];return o.splice(c,1),o}if(t&&typeof t=="object"&&!Array.isArray(t)){let o=String(c);if(!g(o))return t;let u=O(t);return delete u[o],u}return t}let[e,...n]=r;if(Array.isArray(t)&&typeof e=="number"){let c=[...t];return c[e]=V(c[e],n),c}if(t&&typeof t=="object"&&!Array.isArray(t)){let c=String(e);if(!g(c))return t;let o=O(t);return Object.hasOwn(o,c)&&A(o,c,V(o[c],n)),o}return t}var P=class t extends Error{label;partialResults;constructor(r,e=[]){super(`break ${r}`),this.label=r,this.partialResults=e,this.name="BreakError"}withPrependedResults(r){return new t(this.label,[...r,...this.partialResults])}},H=class extends Error{value;constructor(r){super(typeof r=="string"?r:JSON.stringify(r)),this.value=r,this.name="JqError"}},St=1e4,bt=2e3,Ct=new Map([["floor",Math.floor],["ceil",Math.ceil],["round",Math.round],["sqrt",Math.sqrt],["log",Math.log],["log10",Math.log10],["log2",Math.log2],["exp",Math.exp],["sin",Math.sin],["cos",Math.cos],["tan",Math.tan],["asin",Math.asin],["acos",Math.acos],["atan",Math.atan],["sinh",Math.sinh],["cosh",Math.cosh],["tanh",Math.tanh],["asinh",Math.asinh],["acosh",Math.acosh],["atanh",Math.atanh],["cbrt",Math.cbrt],["expm1",Math.expm1],["log1p",Math.log1p],["trunc",Math.trunc]]);function Tt(t){return{vars:new Map,limits:{maxIterations:t?.limits?.maxIterations??St,maxDepth:t?.limits?.maxDepth??bt},env:t?.env,coverage:t?.coverage,requireDefenseContext:t?.requireDefenseContext,defenseContextChecked:!1}}function q(t,r,e){let n=new Map(t.vars);return n.set(r,e),{vars:n,limits:t.limits,env:t.env,requireDefenseContext:t.requireDefenseContext,defenseContextChecked:t.defenseContextChecked,root:t.root,currentPath:t.currentPath,funcs:t.funcs,labels:t.labels,coverage:t.coverage}}function L(t,r,e){switch(r.type){case"var":return q(t,r.name,e);case"array":{if(!Array.isArray(e))return null;let n=t;for(let c=0;c0&&r.args[0].type==="Literal"){let n=r.args[0].value;typeof n=="number"&&(e=n)}if(e>=0)return t.slice(0,Math.max(0,t.length-e));{let n=-e-1;return t.slice(0,Math.min(n,t.length))}}if(r.name==="root")return[]}if(r.type==="Field"){let e=M(r);if(e!==null)return[...t,...e]}if(r.type==="Index"&&r.index.type==="Literal"){let e=M(r);if(e!==null)return[...t,...e]}if(r.type==="Pipe"){let e=pt(t,r.left);return e===null?null:pt(e,r.right)}return r.type==="Identity"?t:null}function mt(t,r,e){if(r.type==="Comma"){let n=[];try{n.push(...d(t,r.left,e))}catch(c){if(c instanceof B)throw c;if(n.length>0)return n;throw new Error("evaluation failed")}try{n.push(...d(t,r.right,e))}catch(c){if(c instanceof B)throw c;return n}return n}return d(t,r,e)}function d(t,r,e){let n=e&&"vars"in e?e:Tt(e);switch(n.defenseContextChecked||(yt(n.requireDefenseContext,"query-engine","evaluation"),n={...n,defenseContextChecked:!0}),n.root===void 0&&(n={...n,root:t,currentPath:[]}),n.coverage?.hit(`jq:node:${r.type}`),r.type){case"Identity":return[t];case"Field":return(r.base?d(t,r.base,n):[t]).flatMap(o=>{let u=E(o);if(u){if(!Object.hasOwn(u,r.name))return[null];let s=u[r.name];return[s===void 0?null:s]}if(o===null)return[null];let p=Array.isArray(o)?"array":typeof o;throw new Error(`Cannot index ${p} with string "${r.name}"`)});case"Index":return(r.base?d(t,r.base,n):[t]).flatMap(o=>d(o,r.index,n).flatMap(p=>{if(typeof p=="number"&&Array.isArray(o)){if(Number.isNaN(p))return[null];let s=Math.trunc(p),f=s<0?o.length+s:s;return f>=0&&f{if(o===null)return[null];if(!Array.isArray(o)&&typeof o!="string")throw new Error(`Cannot slice ${typeof o} (${JSON.stringify(o)})`);let u=o.length,p=r.start?d(t,r.start,n):[0],s=r.end?d(t,r.end,n):[u];return p.flatMap(f=>s.map(i=>{let h=f,a=i,y=Number.isNaN(h)?0:Number.isInteger(h)?h:Math.floor(h),l=Number.isNaN(a)?u:Number.isInteger(a)?a:Math.ceil(a),m=dt(y,u),b=dt(l,u);return Array.isArray(o),o.slice(m,b)}))});case"Iterate":return(r.base?d(t,r.base,n):[t]).flatMap(o=>Array.isArray(o)?o:o&&typeof o=="object"?Object.values(o):[]);case"Pipe":{let c=d(t,r.left,n),o=M(r.left),u=[];for(let p of c)try{if(o!==null){let s={...n,currentPath:[...n.currentPath??[],...o]};u.push(...d(p,r.right,s))}else u.push(...d(p,r.right,n))}catch(s){throw s instanceof P?s.withPrependedResults(u):s}return u}case"Comma":{let c=d(t,r.left,n),o=d(t,r.right,n);return[...c,...o]}case"Literal":return[r.value];case"Array":return r.elements?[d(t,r.elements,n)]:[[]];case"Object":{let c=[Object.create(null)];for(let o of r.entries){let u=typeof o.key=="string"?[o.key]:d(t,o.key,n),p=d(t,o.value,n),s=[];for(let f of c)for(let i of u){if(typeof i!="string"){let h=i===null?"null":Array.isArray(i)?"array":typeof i;throw new Error(`Cannot use ${h} (${JSON.stringify(i)}) as object key`)}if(!g(i)){for(let h of p)s.push(O(f));continue}for(let h of p){let a=O(f);A(a,i,h),s.push(a)}}c.length=0,c.push(...s)}return c}case"Paren":return d(t,r.expr,n);case"BinaryOp":return xt(t,r.op,r.left,r.right,n);case"UnaryOp":return d(t,r.operand,n).map(o=>{if(r.op==="-"){if(typeof o=="number")return-o;if(typeof o=="string"){let u=p=>p.length>5?`"${p.slice(0,3)}...`:JSON.stringify(p);throw new Error(`string (${u(o)}) cannot be negated`)}return null}return r.op==="not"?!S(o):null});case"Cond":return d(t,r.cond,n).flatMap(o=>{if(S(o))return d(t,r.then,n);for(let u of r.elifs)if(d(t,u.cond,n).some(S))return d(t,u.then,n);return r.else?d(t,r.else,n):[t]});case"Try":try{return d(t,r.body,n)}catch(c){if(r.catch){let o=c instanceof H?c.value:c instanceof Error?c.message:String(c);return d(o,r.catch,n)}return[]}case"Call":return gt(t,r.name,r.args,n);case"VarBind":return d(t,r.value,n).flatMap(o=>{let u=null,p=[];r.pattern?p.push(r.pattern):r.name&&p.push({type:"var",name:r.name}),r.alternatives&&p.push(...r.alternatives);for(let s of p)if(u=L(n,s,o),u!==null)break;return u===null?[]:d(t,r.body,u)});case"VarRef":{if(r.name==="$ENV")return[n.env?X(n.env):Object.create(null)];let c=n.vars.get(r.name);return c!==void 0?[c]:[null]}case"Recurse":{let c=[],o=new WeakSet,u=p=>{if(p&&typeof p=="object"){if(o.has(p))return;o.add(p)}if(c.push(p),Array.isArray(p))for(let s of p)u(s);else if(p&&typeof p=="object")for(let s of Object.keys(p))u(p[s])};return u(t),c}case"Optional":try{return d(t,r.expr,n)}catch{return[]}case"StringInterp":return[r.parts.map(o=>typeof o=="string"?o:d(t,o,n).map(p=>typeof p=="string"?p:JSON.stringify(p)).join("")).join("")];case"UpdateOp":return[Mt(t,r.path,r.op,r.value,n)];case"Reduce":{let c=d(t,r.expr,n),o=d(t,r.init,n)[0],u=n.limits.maxDepth??bt;for(let p of c){let s;if(r.pattern){if(s=L(n,r.pattern,p),s===null)continue}else s=q(n,r.varName,p);if(o=d(o,r.update,s)[0],D(o,u+1)>u)return[null]}return[o]}case"Foreach":{let c=d(t,r.expr,n),o=d(t,r.init,n)[0],u=[];for(let p of c)try{let s;if(r.pattern){if(s=L(n,r.pattern,p),s===null)continue}else s=q(n,r.varName,p);if(o=d(o,r.update,s)[0],r.extract){let f=d(o,r.extract,s);u.push(...f)}else u.push(o)}catch(s){throw s instanceof P?s.withPrependedResults(u):s}return u}case"Label":try{return d(t,r.body,{...n,labels:new Set([...n.labels??[],r.name])})}catch(c){if(c instanceof P&&c.label===r.name)return c.partialResults;throw c}case"Break":throw new P(r.name);case"Def":{let c=new Map(n.funcs??[]),o=`${r.name}/${r.params.length}`;c.set(o,{params:r.params,body:r.funcBody,closure:new Map(n.funcs??[])});let u={...n,funcs:c};return d(t,r.body,u)}default:{let c=r;throw new Error(`Unknown AST node type: ${c.type}`)}}}function dt(t,r){return t<0?Math.max(0,r+t):Math.min(t,r)}function Mt(t,r,e,n,c){function o(s,f){switch(e){case"=":return f;case"|=":return d(s,n,c)[0]??null;case"+=":return typeof s=="number"&&typeof f=="number"||typeof s=="string"&&typeof f=="string"?s+f:Array.isArray(s)&&Array.isArray(f)?[...s,...f]:s&&f&&typeof s=="object"&&typeof f=="object"?G(s,f):f;case"-=":return typeof s=="number"&&typeof f=="number"?s-f:s;case"*=":return typeof s=="number"&&typeof f=="number"?s*f:s;case"/=":return typeof s=="number"&&typeof f=="number"?s/f:s;case"%=":return typeof s=="number"&&typeof f=="number"?s%f:s;case"//=":return s===null||s===!1?f:s;default:return f}}function u(s,f,i){switch(f.type){case"Identity":return i(s);case"Field":{if(!g(f.name))return s;if(f.base)return u(s,f.base,h=>{if(h&&typeof h=="object"&&!Array.isArray(h)){let a=O(h),y=Object.hasOwn(a,f.name)?a[f.name]:void 0;return A(a,f.name,i(y)),a}return h});if(s&&typeof s=="object"&&!Array.isArray(s)){let h=O(s),a=Object.hasOwn(h,f.name)?h[f.name]:void 0;return A(h,f.name,i(a)),h}return s}case"Index":{let a=d(t,f.index,c)[0];if(typeof a=="number"&&Number.isNaN(a))throw new Error("Cannot set array element at NaN index");if(typeof a=="number"&&!Number.isInteger(a)&&(a=Math.trunc(a)),f.base)return u(s,f.base,y=>{if(typeof a=="number"&&Array.isArray(y)){let l=[...y],m=a<0?l.length+a:a;if(m>=0){for(;l.length<=m;)l.push(null);l[m]=i(l[m])}return l}if(typeof a=="string"&&y&&typeof y=="object"&&!Array.isArray(y)){if(!g(a))return y;let l=O(y),m=Object.hasOwn(l,a)?l[a]:void 0;return A(l,a,i(m)),l}return y});if(typeof a=="number"){if(a>536870911)throw new Error("Array index too large");if(a<0&&(!s||!Array.isArray(s)))throw new Error("Out of bounds negative array index");if(Array.isArray(s)){let l=[...s],m=a<0?l.length+a:a;if(m>=0){for(;l.length<=m;)l.push(null);l[m]=i(l[m])}return l}if(s==null){let l=[];for(;l.length<=a;)l.push(null);return l[a]=i(null),l}return s}if(typeof a=="string"&&s&&typeof s=="object"&&!Array.isArray(s)){if(!g(a))return s;let y=O(s),l=Object.hasOwn(y,a)?y[a]:void 0;return A(y,a,i(l)),y}return s}case"Iterate":{let h=a=>{if(Array.isArray(a))return a.map(y=>i(y));if(a&&typeof a=="object"){let y=Object.create(null);for(let[l,m]of Object.entries(a))g(l)&&A(y,l,i(m));return y}return a};return f.base?u(s,f.base,h):h(s)}case"Pipe":{let h=u(s,f.left,a=>a);return u(h,f.right,i)}default:return i(s)}}return u(t,r,s=>{if(e==="|=")return o(s,s);let f=d(t,n,c);return o(s,f[0]??null)})}function jt(t,r,e){function n(o,u,p){switch(u.type){case"Identity":return p;case"Field":{if(!g(u.name))return o;if(u.base){let s=d(o,u.base,e)[0],f=n(s,{type:"Field",name:u.name},p);return n(o,u.base,f)}if(o&&typeof o=="object"&&!Array.isArray(o)){let s=O(o);return A(s,u.name,p),s}return o}case"Index":{if(u.base){let i=d(o,u.base,e)[0],h=n(i,{type:"Index",index:u.index},p);return n(o,u.base,h)}let f=d(t,u.index,e)[0];if(typeof f=="number"&&Array.isArray(o)){let i=[...o],h=f<0?i.length+f:f;return h>=0&&h=0&&h=0&&NS(s)?d(t,n,c).map(i=>S(i)):[!1]);if(r==="or")return d(t,e,c).flatMap(s=>S(s)?[!0]:d(t,n,c).map(i=>S(i)));if(r==="//"){let s=d(t,e,c).filter(f=>f!=null&&f!==!1);return s.length>0?s:d(t,n,c)}let o=d(t,e,c),u=d(t,n,c);return o.flatMap(p=>u.map(s=>{switch(r){case"+":return p===null?s:s===null?p:typeof p=="number"&&typeof s=="number"||typeof p=="string"&&typeof s=="string"?p+s:Array.isArray(p)&&Array.isArray(s)?[...p,...s]:p&&s&&typeof p=="object"&&typeof s=="object"&&!Array.isArray(p)&&!Array.isArray(s)?G(p,s):null;case"-":if(typeof p=="number"&&typeof s=="number")return p-s;if(Array.isArray(p)&&Array.isArray(s)){let f=new Set(s.map(i=>JSON.stringify(i)));return p.filter(i=>!f.has(JSON.stringify(i)))}if(typeof p=="string"&&typeof s=="string"){let f=i=>i.length>10?`"${i.slice(0,10)}...`:JSON.stringify(i);throw new Error(`string (${f(p)}) and string (${f(s)}) cannot be subtracted`)}return null;case"*":if(typeof p=="number"&&typeof s=="number")return p*s;if(typeof p=="string"&&typeof s=="number")return p.repeat(s);{let f=E(p),i=E(s);if(f&&i)return v(f,i)}return null;case"/":if(typeof p=="number"&&typeof s=="number"){if(s===0)throw new Error(`number (${p}) and number (${s}) cannot be divided because the divisor is zero`);return p/s}return typeof p=="string"&&typeof s=="string"?p.split(s):null;case"%":if(typeof p=="number"&&typeof s=="number"){if(s===0)throw new Error(`number (${p}) and number (${s}) cannot be divided (remainder) because the divisor is zero`);return!Number.isFinite(p)&&!Number.isNaN(p)?!Number.isFinite(s)&&!Number.isNaN(s)&&p<0&&s>0?-1:0:p%s}return null;case"==":return I(p,s);case"!=":return!I(p,s);case"<":return U(p,s)<0;case"<=":return U(p,s)<=0;case">":return U(p,s)>0;case">=":return U(p,s)>=0;default:return null}}))}function gt(t,r,e,n){let c=Ct.get(r);if(c)return typeof t=="number"?[c(t)]:[null];let o=rt(t,r,e,n,d);if(o!==null)return o;let u=ft(t,r,e,n,d);if(u!==null)return u;let p=Z(t,r,e,n,d);if(p!==null)return p;let s=tt(t,r,n.limits.maxDepth);if(s!==null)return s;let f=ct(t,r);if(f!==null)return f;let i=st(t,r,e,n,d);if(i!==null)return i;let h=W(t,r,e,n,d,mt,$,S,F,B);if(h!==null)return h;let a=it(t,r,e,n,d,S,K,V,jt,J);if(a!==null)return a;let y=et(t,r,e,n,d,I);if(y!==null)return y;let l=z(t,r,e,n,d,mt,S,B);if(l!==null)return l;let m=nt(t,r,e,n,d,S,Rt,gt);if(m!==null)return m;let b=ot(t,r,e,n,d,I);if(b!==null)return b;switch(r){case"builtins":return[["add/0","all/0","all/1","all/2","any/0","any/1","any/2","arrays/0","ascii/0","ascii_downcase/0","ascii_upcase/0","booleans/0","bsearch/1","builtins/0","combinations/0","combinations/1","contains/1","debug/0","del/1","delpaths/1","empty/0","env/0","error/0","error/1","explode/0","first/0","first/1","flatten/0","flatten/1","floor/0","from_entries/0","fromdate/0","fromjson/0","getpath/1","gmtime/0","group_by/1","gsub/2","gsub/3","has/1","implode/0","IN/1","IN/2","INDEX/1","INDEX/2","index/1","indices/1","infinite/0","inside/1","isempty/1","isnan/0","isnormal/0","isvalid/1","iterables/0","join/1","keys/0","keys_unsorted/0","last/0","last/1","length/0","limit/2","ltrimstr/1","map/1","map_values/1","match/1","match/2","max/0","max_by/1","min/0","min_by/1","mktime/0","modulemeta/1","nan/0","not/0","nth/1","nth/2","null/0","nulls/0","numbers/0","objects/0","path/1","paths/0","paths/1","pick/1","range/1","range/2","range/3","recurse/0","recurse/1","recurse_down/0","repeat/1","reverse/0","rindex/1","rtrimstr/1","scalars/0","scan/1","scan/2","select/1","setpath/2","skip/2","sort/0","sort_by/1","split/1","splits/1","splits/2","sqrt/0","startswith/1","strftime/1","strings/0","strptime/1","sub/2","sub/3","test/1","test/2","to_entries/0","toboolean/0","todate/0","tojson/0","tostream/0","fromstream/1","truncate_stream/1","tonumber/0","tostring/0","transpose/0","trim/0","ltrim/0","rtrim/0","type/0","unique/0","unique_by/1","until/2","utf8bytelength/0","values/0","walk/1","while/2","with_entries/1"]];case"error":{let w=e.length>0?d(t,e[0],n)[0]:t;throw new H(w)}case"env":return[n.env?X(n.env):Object.create(null)];case"debug":return[t];case"input_line_number":return[1];default:{let w=`${r}/${e.length}`,k=n.funcs?.get(w);if(k){let N=k.closure??n.funcs??new Map,C=new Map(N);C.set(w,k);for(let _=0;_=0;Q--)x={type:"Comma",left:{type:"Literal",value:j[Q]},right:x}}C.set(`${kt}/0`,{params:[],body:x})}}let wt={...n,funcs:C};return d(t,k.body,wt)}throw new Error(`Unknown function: ${r}`)}}}function J(t,r,e,n,c){if(r.type==="Comma"){let p=r;J(t,p.left,e,n,c),J(t,p.right,e,n,c);return}let o=M(r);if(o!==null){c.push([...n,...o]);return}if(r.type==="Iterate"){if(Array.isArray(t))for(let p=0;p{if(c.push([...n,...f]),s&&typeof s=="object")if(Array.isArray(s))for(let i=0;i0&&c.push(n)}var At=new Map([["and","AND"],["or","OR"],["not","NOT"],["if","IF"],["then","THEN"],["elif","ELIF"],["else","ELSE"],["end","END"],["as","AS"],["try","TRY"],["catch","CATCH"],["true","TRUE"],["false","FALSE"],["null","NULL"],["reduce","REDUCE"],["foreach","FOREACH"],["label","LABEL"],["break","BREAK"],["def","DEF"]]),Y=new Set(At.values());function Et(t){let r=[],e=0,n=(f=0)=>t[e+f],c=()=>t[e++],o=()=>e>=t.length,u=f=>f>="0"&&f<="9",p=f=>f>="a"&&f<="z"||f>="A"&&f<="Z"||f==="_",s=f=>p(f)||u(f);for(;!o();){let f=e,i=c();if(!(i===" "||i===" "||i===` `||i==="\r")){if(i==="#"){for(;!o()&&n()!==` `;)c();continue}if(i==="."&&n()==="."){c(),r.push({type:"DOTDOT",pos:f});continue}if(i==="="&&n()==="="){c(),r.push({type:"EQ",pos:f});continue}if(i==="!"&&n()==="="){c(),r.push({type:"NE",pos:f});continue}if(i==="<"&&n()==="="){c(),r.push({type:"LE",pos:f});continue}if(i===">"&&n()==="="){c(),r.push({type:"GE",pos:f});continue}if(i==="/"&&n()==="/"){c(),n()==="="?(c(),r.push({type:"UPDATE_ALT",pos:f})):r.push({type:"ALT",pos:f});continue}if(i==="+"&&n()==="="){c(),r.push({type:"UPDATE_ADD",pos:f});continue}if(i==="-"&&n()==="="){c(),r.push({type:"UPDATE_SUB",pos:f});continue}if(i==="*"&&n()==="="){c(),r.push({type:"UPDATE_MUL",pos:f});continue}if(i==="/"&&n()==="="){c(),r.push({type:"UPDATE_DIV",pos:f});continue}if(i==="%"&&n()==="="){c(),r.push({type:"UPDATE_MOD",pos:f});continue}if(i==="="&&n()!=="="){r.push({type:"ASSIGN",pos:f});continue}if(i==="."){r.push({type:"DOT",pos:f});continue}if(i==="|"){n()==="="?(c(),r.push({type:"UPDATE_PIPE",pos:f})):r.push({type:"PIPE",pos:f});continue}if(i===","){r.push({type:"COMMA",pos:f});continue}if(i===":"){r.push({type:"COLON",pos:f});continue}if(i===";"){r.push({type:"SEMICOLON",pos:f});continue}if(i==="("){r.push({type:"LPAREN",pos:f});continue}if(i===")"){r.push({type:"RPAREN",pos:f});continue}if(i==="["){r.push({type:"LBRACKET",pos:f});continue}if(i==="]"){r.push({type:"RBRACKET",pos:f});continue}if(i==="{"){r.push({type:"LBRACE",pos:f});continue}if(i==="}"){r.push({type:"RBRACE",pos:f});continue}if(i==="?"){r.push({type:"QUESTION",pos:f});continue}if(i==="+"){r.push({type:"PLUS",pos:f});continue}if(i==="-"){r.push({type:"MINUS",pos:f});continue}if(i==="*"){r.push({type:"STAR",pos:f});continue}if(i==="/"){r.push({type:"SLASH",pos:f});continue}if(i==="%"){r.push({type:"PERCENT",pos:f});continue}if(i==="<"){r.push({type:"LT",pos:f});continue}if(i===">"){r.push({type:"GT",pos:f});continue}if(u(i)){let h=i;for(;!o()&&(u(n())||n()==="."||n()==="e"||n()==="E");)(n()==="e"||n()==="E")&&(t[e+1]==="+"||t[e+1]==="-")&&(h+=c()),h+=c();r.push({type:"NUMBER",value:Number(h),pos:f});continue}if(i==='"'){let h="",a=0;for(;!o();){let y=n();if(a===0&&y==='"')break;if(a===0&&y==="\\"){if(c(),o())break;let l=c();switch(l){case"n":h+=` `;break;case"r":h+="\r";break;case"t":h+=" ";break;case"\\":h+="\\";break;case'"':h+='"';break;case"(":h+="\\(",a=1;break;default:h+=l}continue}if(a>0){if(y==='"'){for(h+=c();!o();){let l=n();if(l==="\\"){h+=c(),o()||(h+=c());continue}if(h+=c(),l==='"')break}continue}y==="("?a++:y===")"&&a--,h+=c();continue}h+=c()}o()||c(),r.push({type:"STRING",value:h,pos:f});continue}if(p(i)||i==="$"||i==="@"){let h=i;for(;!o()&&s(n());)h+=c();let a=At.get(h);a?r.push({type:a,value:h,pos:f}):r.push({type:"IDENT",value:h,pos:f});continue}throw new Error(`Unexpected character '${i}' at position ${f}`)}}return r.push({type:"EOF",pos:e}),r}var ut=class t{tokens;pos=0;constructor(r){this.tokens=r}peek(r=0){return this.tokens[this.pos+r]??{type:"EOF",pos:-1}}advance(){return this.tokens[this.pos++]}check(r){return this.peek().type===r}match(...r){for(let e of r)if(this.check(e))return this.advance();return null}expect(r,e){if(!this.check(r))throw new Error(`${e} at position ${this.peek().pos}, got ${this.peek().type}`);return this.advance()}isFieldNameAfterDot(r=0){let e=this.peek(r),n=this.peek(r+1);return n.type==="STRING"?!0:n.type==="IDENT"||Y.has(n.type)?n.pos===e.pos+1:!1}isIdentLike(){let r=this.peek().type;return r==="IDENT"||Y.has(r)}consumeFieldNameAfterDot(r){let e=this.peek();return e.type==="STRING"?this.advance().value:(e.type==="IDENT"||Y.has(e.type))&&e.pos===r.pos+1?this.advance().value:null}parse(){let r=this.parseExpr();if(!this.check("EOF"))throw new Error(`Unexpected token ${this.peek().type} at position ${this.peek().pos}`);return r}parseExpr(){return this.parsePipe()}parsePattern(){if(this.match("LBRACKET")){let n=[];if(!this.check("RBRACKET"))for(n.push(this.parsePattern());this.match("COMMA")&&!this.check("RBRACKET");)n.push(this.parsePattern());return this.expect("RBRACKET","Expected ']' after array pattern"),{type:"array",elements:n}}if(this.match("LBRACE")){let n=[];if(!this.check("RBRACE"))for(n.push(this.parsePatternField());this.match("COMMA")&&!this.check("RBRACE");)n.push(this.parsePatternField());return this.expect("RBRACE","Expected '}' after object pattern"),{type:"object",fields:n}}let r=this.expect("IDENT","Expected variable name in pattern"),e=r.value;if(!e.startsWith("$"))throw new Error(`Variable name must start with $ at position ${r.pos}`);return{type:"var",name:e}}parsePatternField(){if(this.match("LPAREN")){let e=this.parseExpr();this.expect("RPAREN","Expected ')' after computed key"),this.expect("COLON","Expected ':' after computed key");let n=this.parsePattern();return{key:e,pattern:n}}let r=this.peek();if(r.type==="IDENT"||Y.has(r.type)){let e=r.value;if(e.startsWith("$")){if(this.advance(),this.match("COLON")){let n=this.parsePattern();return{key:e.slice(1),pattern:n,keyVar:e}}return{key:e.slice(1),pattern:{type:"var",name:e}}}if(this.advance(),this.match("COLON")){let n=this.parsePattern();return{key:e,pattern:n}}return{key:e,pattern:{type:"var",name:`$${e}`}}}throw new Error(`Expected field name in object pattern at position ${r.pos}`)}parsePipe(){let r=this.parseComma();for(;this.match("PIPE");){let e=this.parseComma();r={type:"Pipe",left:r,right:e}}return r}parseComma(){let r=this.parseVarBind();for(;this.match("COMMA");){let e=this.parseVarBind();r={type:"Comma",left:r,right:e}}return r}parseVarBind(){let r=this.parseUpdate();if(this.match("AS")){let e=this.parsePattern(),n=[];for(;this.check("QUESTION")&&this.peekAhead(1)?.type==="ALT";)this.advance(),this.advance(),n.push(this.parsePattern());this.expect("PIPE","Expected '|' after variable binding");let c=this.parseExpr();return e.type==="var"&&n.length===0?{type:"VarBind",name:e.name,value:r,body:c}:{type:"VarBind",name:e.type==="var"?e.name:"",value:r,body:c,pattern:e.type!=="var"?e:void 0,alternatives:n.length>0?n:void 0}}return r}peekAhead(r){let e=this.pos+r;return e"],["GE",">="]]),n=this.match("EQ","NE","LT","LE","GT","GE");if(n){let c=e.get(n.type);if(c){let o=this.parseAddSub();r={type:"BinaryOp",op:c,left:r,right:o}}}return r}parseAddSub(){let r=this.parseMulDiv();for(;;)if(this.match("PLUS")){let e=this.parseMulDiv();r={type:"BinaryOp",op:"+",left:r,right:e}}else if(this.match("MINUS")){let e=this.parseMulDiv();r={type:"BinaryOp",op:"-",left:r,right:e}}else break;return r}parseMulDiv(){let r=this.parseUnary();for(;;)if(this.match("STAR")){let e=this.parseUnary();r={type:"BinaryOp",op:"*",left:r,right:e}}else if(this.match("SLASH")){let e=this.parseUnary();r={type:"BinaryOp",op:"/",left:r,right:e}}else if(this.match("PERCENT")){let e=this.parseUnary();r={type:"BinaryOp",op:"%",left:r,right:e}}else break;return r}parseUnary(){return this.match("MINUS")?{type:"UnaryOp",op:"-",operand:this.parseUnary()}:this.parsePostfix()}parsePostfix(){let r=this.parsePrimary();for(;;)if(this.match("QUESTION"))r={type:"Optional",expr:r};else if(this.check("DOT")&&this.isFieldNameAfterDot())this.advance(),r={type:"Field",name:this.advance().value,base:r};else if(this.check("LBRACKET"))if(this.advance(),this.match("RBRACKET"))r={type:"Iterate",base:r};else if(this.check("COLON")){this.advance();let e=this.check("RBRACKET")?void 0:this.parseExpr();this.expect("RBRACKET","Expected ']'"),r={type:"Slice",end:e,base:r}}else{let e=this.parseExpr();if(this.match("COLON")){let n=this.check("RBRACKET")?void 0:this.parseExpr();this.expect("RBRACKET","Expected ']'"),r={type:"Slice",start:e,end:n,base:r}}else this.expect("RBRACKET","Expected ']'"),r={type:"Index",index:e,base:r}}else break;return r}parsePrimary(){if(this.match("DOTDOT"))return{type:"Recurse"};if(this.check("DOT")){let r=this.advance();if(this.check("LBRACKET")){if(this.advance(),this.match("RBRACKET"))return{type:"Iterate"};if(this.check("COLON")){this.advance();let c=this.check("RBRACKET")?void 0:this.parseExpr();return this.expect("RBRACKET","Expected ']'"),{type:"Slice",end:c}}let n=this.parseExpr();if(this.match("COLON")){let c=this.check("RBRACKET")?void 0:this.parseExpr();return this.expect("RBRACKET","Expected ']'"),{type:"Slice",start:n,end:c}}return this.expect("RBRACKET","Expected ']'"),{type:"Index",index:n}}let e=this.consumeFieldNameAfterDot(r);return e!==null?{type:"Field",name:e}:{type:"Identity"}}if(this.match("TRUE"))return{type:"Literal",value:!0};if(this.match("FALSE"))return{type:"Literal",value:!1};if(this.match("NULL"))return{type:"Literal",value:null};if(this.check("NUMBER"))return{type:"Literal",value:this.advance().value};if(this.check("STRING")){let e=this.advance().value;return e.includes("\\(")?this.parseStringInterpolation(e):{type:"Literal",value:e}}if(this.match("LBRACKET")){if(this.match("RBRACKET"))return{type:"Array"};let r=this.parseExpr();return this.expect("RBRACKET","Expected ']'"),{type:"Array",elements:r}}if(this.match("LBRACE"))return this.parseObjectConstruction();if(this.match("LPAREN")){let r=this.parseExpr();return this.expect("RPAREN","Expected ')'"),{type:"Paren",expr:r}}if(this.match("IF"))return this.parseIf();if(this.match("TRY")){let r=this.parsePostfix(),e;return this.match("CATCH")&&(e=this.parsePostfix()),{type:"Try",body:r,catch:e}}if(this.match("REDUCE")){let r=this.parseAddSub();this.expect("AS","Expected 'as' after reduce expression");let e=this.parsePattern();this.expect("LPAREN","Expected '(' after variable");let n=this.parseExpr();this.expect("SEMICOLON","Expected ';' after init expression");let c=this.parseExpr();this.expect("RPAREN","Expected ')' after update expression");let o=e.type==="var"?e.name:"";return{type:"Reduce",expr:r,varName:o,init:n,update:c,pattern:e.type!=="var"?e:void 0}}if(this.match("FOREACH")){let r=this.parseAddSub();this.expect("AS","Expected 'as' after foreach expression");let e=this.parsePattern();this.expect("LPAREN","Expected '(' after variable");let n=this.parseExpr();this.expect("SEMICOLON","Expected ';' after init expression");let c=this.parseExpr(),o;this.match("SEMICOLON")&&(o=this.parseExpr()),this.expect("RPAREN","Expected ')' after expressions");let u=e.type==="var"?e.name:"";return{type:"Foreach",expr:r,varName:u,init:n,update:c,extract:o,pattern:e.type!=="var"?e:void 0}}if(this.match("LABEL")){let r=this.expect("IDENT","Expected label name (e.g., $out)"),e=r.value;if(!e.startsWith("$"))throw new Error(`Label name must start with $ at position ${r.pos}`);this.expect("PIPE","Expected '|' after label name");let n=this.parseExpr();return{type:"Label",name:e,body:n}}if(this.match("BREAK")){let r=this.expect("IDENT","Expected label name to break to"),e=r.value;if(!e.startsWith("$"))throw new Error(`Break label must start with $ at position ${r.pos}`);return{type:"Break",name:e}}if(this.match("DEF")){let e=this.expect("IDENT","Expected function name after def").value,n=[];if(this.match("LPAREN")){if(!this.check("RPAREN")){let u=this.expect("IDENT","Expected parameter name");for(n.push(u.value);this.match("SEMICOLON");){let p=this.expect("IDENT","Expected parameter name");n.push(p.value)}}this.expect("RPAREN","Expected ')' after parameters")}this.expect("COLON","Expected ':' after function name");let c=this.parseExpr();this.expect("SEMICOLON","Expected ';' after function body");let o=this.parseExpr();return{type:"Def",name:e,params:n,funcBody:c,body:o}}if(this.match("NOT"))return{type:"Call",name:"not",args:[]};if(this.check("IDENT")){let e=this.advance().value;if(e.startsWith("$"))return{type:"VarRef",name:e};if(this.match("LPAREN")){let n=[];if(!this.check("RPAREN"))for(n.push(this.parseExpr());this.match("SEMICOLON");)n.push(this.parseExpr());return this.expect("RPAREN","Expected ')'"),{type:"Call",name:e,args:n}}return{type:"Call",name:e,args:[]}}throw new Error(`Unexpected token ${this.peek().type} at position ${this.peek().pos}`)}parseObjectConstruction(){let r=[];if(!this.check("RBRACE"))do{let e,n;if(this.match("LPAREN"))e=this.parseExpr(),this.expect("RPAREN","Expected ')'"),this.expect("COLON","Expected ':'"),n=this.parseObjectValue();else if(this.isIdentLike()){let c=this.advance().value;this.match("COLON")?(e=c,n=this.parseObjectValue()):(e=c,n={type:"Field",name:c})}else if(this.check("STRING"))e=this.advance().value,this.expect("COLON","Expected ':'"),n=this.parseObjectValue();else throw new Error(`Expected object key at position ${this.peek().pos}`);r.push({key:e,value:n})}while(this.match("COMMA"));return this.expect("RBRACE","Expected '}'"),{type:"Object",entries:r}}parseObjectValue(){let r=this.parseVarBind();for(;this.match("PIPE");){let e=this.parseVarBind();r={type:"Pipe",left:r,right:e}}return r}parseIf(){let r=this.parseExpr();this.expect("THEN","Expected 'then'");let e=this.parseExpr(),n=[];for(;this.match("ELIF");){let o=this.parseExpr();this.expect("THEN","Expected 'then' after elif");let u=this.parseExpr();n.push({cond:o,then:u})}let c;return this.match("ELSE")&&(c=this.parseExpr()),this.expect("END","Expected 'end'"),{type:"Cond",cond:r,then:e,elifs:n,else:c}}parseStringInterpolation(r){let e=[],n="",c=0;for(;c0;){let f=r[c];if(f==='"'){for(u+=f,c++;c0&&(u+=f),c++}let p=Et(u),s=new t(p);e.push(s.parse())}else n+=r[c],c++;return n&&e.push(n),{type:"StringInterp",parts:e}}};function Ne(t){let r=Et(t);return new ut(r).parse()}export{d as a,Ne as b};