@sanity/sdk 2.2.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,3 @@
1
- /**
2
- * @public
3
- */
4
- export type ValidProjection = `{${string}}`
5
-
6
1
  /**
7
2
  * @public
8
3
  * The result of a projection query
@@ -16,8 +11,16 @@ export interface DocumentProjectionValues<TValue extends object = object> {
16
11
  [projectionHash: string]: ProjectionValuePending<TValue>
17
12
  }
18
13
 
14
+ /**
15
+ * @public
16
+ * @deprecated
17
+ * Template literals are a bit too limited, so this type is deprecated.
18
+ * Use `string` instead. Projection strings are validated at runtime.
19
+ */
20
+ export type ValidProjection = string
21
+
19
22
  export interface DocumentProjections {
20
- [projectionHash: string]: ValidProjection
23
+ [projectionHash: string]: string
21
24
  }
22
25
 
23
26
  interface DocumentProjectionSubscriptions {
@@ -1,5 +1,3 @@
1
- import {type ValidProjection} from './types'
2
-
3
1
  export const PROJECTION_TAG = 'projection'
4
2
  export const PROJECTION_PERSPECTIVE = 'raw'
5
3
  export const PROJECTION_STATE_CLEAR_DELAY = 1000
@@ -9,11 +7,11 @@ export const STABLE_EMPTY_PROJECTION = {
9
7
  isPending: false,
10
8
  }
11
9
 
12
- export function validateProjection(projection: string): ValidProjection {
10
+ export function validateProjection(projection: string): string {
13
11
  if (!projection.startsWith('{') || !projection.endsWith('}')) {
14
12
  throw new Error(
15
13
  `Invalid projection format: "${projection}". Projections must be enclosed in curly braces, e.g. "{title, 'author': author.name}"`,
16
14
  )
17
15
  }
18
- return projection as ValidProjection
16
+ return projection
19
17
  }
@@ -1,8 +1,9 @@
1
- import {type ResponseQueryOptions} from '@sanity/client'
1
+ import {CorsOriginError, type ResponseQueryOptions} from '@sanity/client'
2
2
  import {type SanityQueryResult} from 'groq'
3
3
  import {
4
4
  catchError,
5
5
  combineLatest,
6
+ defer,
6
7
  distinctUntilChanged,
7
8
  EMPTY,
8
9
  filter,
@@ -207,7 +208,18 @@ const listenToLiveClientAndSetLastLiveEventIds = ({
207
208
  apiVersion: QUERY_STORE_API_VERSION,
208
209
  }).observable.pipe(
209
210
  switchMap((client) =>
210
- client.live.events({includeDrafts: !!client.config().token, tag: 'query-store'}),
211
+ defer(() =>
212
+ client.live.events({includeDrafts: !!client.config().token, tag: 'query-store'}),
213
+ ).pipe(
214
+ catchError((error) => {
215
+ if (error instanceof CorsOriginError) {
216
+ // Swallow only CORS errors in store without bubbling up so that they are handled by the Cors Error component
217
+ state.set('setError', {error})
218
+ return EMPTY
219
+ }
220
+ throw error
221
+ }),
222
+ ),
211
223
  ),
212
224
  share(),
213
225
  filter((e) => e.type === 'message'),
@@ -0,0 +1,46 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import {CorsOriginError} from '@sanity/client'
3
+ import {afterEach, describe, expect, test} from 'vitest'
4
+
5
+ import {getCorsErrorProjectId} from './getCorsErrorProjectId'
6
+
7
+ describe('getCorsErrorProjectId', () => {
8
+ const originalLocation = (globalThis as any).location
9
+
10
+ afterEach(() => {
11
+ if (originalLocation === undefined) {
12
+ delete (globalThis as any).location
13
+ } else {
14
+ ;(globalThis as any).location = originalLocation
15
+ }
16
+ })
17
+
18
+ test('returns null for non-CorsOriginError', () => {
19
+ const err = new Error(
20
+ 'Change your configuration here: https://sanity.io/manage/project/abc123/api',
21
+ )
22
+
23
+ expect(getCorsErrorProjectId(err)).toBeNull()
24
+ })
25
+
26
+ test('extracts projectId from CorsOriginError in Node environment', () => {
27
+ delete (globalThis as any).location
28
+ const err = new CorsOriginError({projectId: 'abc123'})
29
+
30
+ expect(getCorsErrorProjectId(err as unknown as Error)).toBe('abc123')
31
+ })
32
+
33
+ test('extracts projectId from CorsOriginError with browser query params', () => {
34
+ ;(globalThis as any).location = {origin: 'https://example.com'}
35
+ const err = new CorsOriginError({projectId: 'p123-xyz'})
36
+
37
+ expect(getCorsErrorProjectId(err as unknown as Error)).toBe('p123-xyz')
38
+ })
39
+
40
+ test('returns null if CorsOriginError message does not contain manage URL', () => {
41
+ const err = new CorsOriginError({projectId: 'abc123'})
42
+ err.message = 'Some other message without the manage URL'
43
+
44
+ expect(getCorsErrorProjectId(err as unknown as Error)).toBeNull()
45
+ })
46
+ })
@@ -0,0 +1,15 @@
1
+ import {CorsOriginError} from '@sanity/client'
2
+
3
+ /**
4
+ * @public
5
+ * Extracts the project ID from a CorsOriginError message.
6
+ * @param error - The error to extract the project ID from.
7
+ * @returns The project ID or null if the error is not a CorsOriginError.
8
+ */
9
+ export function getCorsErrorProjectId(error: Error): string | null {
10
+ if (!(error instanceof CorsOriginError)) return null
11
+
12
+ const message = (error as unknown as {message?: string}).message || ''
13
+ const projMatch = message.match(/manage\/project\/([^/?#]+)/)
14
+ return projMatch ? projMatch[1] : null
15
+ }
@@ -1,4 +0,0 @@
1
- // NOTE: this is a stop-gap measure until sychronous groq is out
2
- // eslint-disable
3
- // prettier-ignore
4
- class e{pattern;patternRe;constructor(e){this.pattern=e,this.patternRe=function(e){const t=[];for(const r of e.split("."))"*"===r?t.push("[^.]+"):"**"===r?t.push(".*"):t.push(r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"));return new RegExp(`^${t.join(".")}$`)}(e)}matches(e){return this.patternRe.test(e)}toJSON(){return this.pattern}}class t{type="stream";generator;ticker;isDone;data;constructor(e){this.generator=e,this.ticker=null,this.isDone=!1,this.data=[]}isArray(){return!0}async get(){const e=[];for await(const t of this)e.push(await t.get());return e}async first(e=()=>!0){for await(const t of this)if(e(t))return t}async reduce(e,t){let r=t;for await(const t of this)r=await e(r,t);return r}async*[Symbol.asyncIterator](){let e=0;for(;;){for(;e<this.data.length;e++)yield this.data[e];if(this.isDone)return;await this._nextTick()}}_nextTick(){if(this.ticker)return this.ticker;let e;const t=()=>{this.ticker=new Promise((t=>{e=t}))},r=()=>{e(),t()};return t(),(async()=>{for await(const e of this.generator())this.data.push(e),r();this.isDone=!0,r()})(),this.ticker}}const r=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|([-+]\d{2}:\d{2}))$/;function n(e,t){let r=e.toString();for(;r.length<t;)r=`0${r}`;return r}class o{data;type;constructor(e,t){this.data=e,this.type=t}isArray(){return"array"===this.type}get(){return this.data}first(e=()=>!0){if(!this.isArray())throw new Error("`first` can only be called on array `StaticValue`s");const t=this.get();for(const r of t){const t=f(r,"sync");if(e(t))return t}}reduce(e,t){if(!this.isArray())throw new Error("`reduce` can only be called on array `StaticValue`s");const r=this.get();let n=t;for(const t of r){n=e(n,f(t,"sync"))}return n}[Symbol.asyncIterator](){if(Array.isArray(this.data))return function*(e){for(const t of e)yield f(t,"async")}(this.data);throw new Error(`Cannot iterate over: ${this.type}`)}}const i=new o(null,"null"),s=new o(!0,"boolean"),a=new o(!1,"boolean");class c{date;constructor(e){this.date=e}static parseToValue(e){const t=function(e){return r.test(e)?new Date(e):null}(e);return t?new o(new c(t),"datetime"):i}equals(e){return this.date.getTime()==e.date.getTime()}add(e){const t=new Date(this.date.getTime());return t.setTime(t.getTime()+1e3*e),new c(t)}difference(e){return(this.date.getTime()-e.date.getTime())/1e3}compareTo(e){return this.date.getTime()-e.date.getTime()}toString(){return function(e){const t=n(e.getUTCFullYear(),4),r=n(e.getUTCMonth()+1,2),o=n(e.getUTCDate(),2),i=n(e.getUTCHours(),2),s=n(e.getUTCMinutes(),2),a=n(e.getUTCSeconds(),2);let c="";const u=e.getMilliseconds();return 0!=u&&(c=`.${n(u,3)}`),`${t}-${r}-${o}T${i}:${s}:${a}${c}Z`}(this.date)}toJSON(){return this.toString()}}function u(e){return Number.isFinite(e)?new o(e,"number"):i}function p(e){return new o(e,"string")}function l(e){return new o(e,"datetime")}function f(e,r){return(n=e)&&"function"==typeof n.next&&"sync"!==r?new t((async function*(){for await(const t of e)yield f(t,"async")})):null==e?i:new o(e,y(e));var n}function y(t){return null===t||typeof t>"u"?"null":Array.isArray(t)?"array":t instanceof e?"path":t instanceof c?"datetime":typeof t}const d=e=>"object"==typeof e&&!!e&&"then"in e&&"function"==typeof e.then;function h(e){const t=e(),r=t.next();if(r.done)return r.value;function n(e){const r=t.next(e);if(r.done)return r.value;const o=r.value;return o&&d(o)?o.then(n):n(o)}const o=r.value;return o&&d(o)?o.then(n):n(o)}function m(e,t){return"string"===e.type&&"string"===t.type||"boolean"===e.type&&"boolean"===t.type||"null"===e.type&&"null"===t.type||"number"===e.type&&"number"===t.type?e.data===t.data:"datetime"===e.type&&"datetime"===t.type&&e.data.equals(t.data)}const b=/([^!@#$%^&*(),\\/?";:{}|[\]+<>\s-])+/g,g=/([^!@#$%^&(),\\/?";:{}|[\]+<>\s-])+/g,w=/(\b\.+|\.+\b)/g;function k(e){return e.replace(w,"").match(b)||[]}function v(e){return x(e).map((e=>t=>t.some((t=>e.test(t)))))}function x(e){return(e.replace(w,"").match(g)||[]).map((e=>new RegExp(`^${e.slice(0,1024).replace(/\*/g,".*")}$`,"i")))}const _={datetime:1,number:2,string:3,boolean:4};function A(e,t){const r=y(e);if(r!==y(t))return null;switch(r){case"number":case"boolean":return e-t;case"string":return e<t?-1:e>t?1:0;case"datetime":return e.compareTo(t);default:return null}}function E(e,t){const r=y(e),n=y(t),o=_[r]||100,i=_[n]||100;if(o!==i)return o-i;let s=A(e,t);return null===s&&(s=0),s}const j={"==":function(e,t){return m(e,t)?s:a},"!=":function(e,t){return m(e,t)?a:s},">":function(e,t){if("stream"===e.type||"stream"===t.type)return i;const r=A(e.data,t.data);return null===r?i:r>0?s:a},">=":function(e,t){if("stream"===e.type||"stream"===t.type)return i;const r=A(e.data,t.data);return null===r?i:r>=0?s:a},"<":function(e,t){if("stream"===e.type||"stream"===t.type)return i;const r=A(e.data,t.data);return null===r?i:r<0?s:a},"<=":function(e,t){if("stream"===e.type||"stream"===t.type)return i;const r=A(e.data,t.data);return null===r?i:r<=0?s:a},in:function(e,t){return h((function*(){return"path"===t.type?"string"!==e.type?i:t.data.matches(e.data)?s:a:t.isArray()?(yield t.first((t=>m(e,t))))?s:a:i}))},match:function(e,t){return h((function*(){const r=yield e.get(),n=yield t.get();let o,i=[];if(Array.isArray(r)?i=r.filter((e=>"string"==typeof e)):"string"==typeof r&&(i=[r]),Array.isArray(n)?o=n.filter((e=>"string"==typeof e)):"string"==typeof n&&(o=[n]),!o?.length)return a;return function(e,t){return 0!==e.length&&0!==t.length&&t.every((t=>t(e)))}(i.flatMap(k),o.flatMap(v))?s:a}))},"+":function(e,r,n){return"datetime"===e.type&&"number"===r.type?l(e.data.add(r.data)):"number"===e.type&&"number"===r.type?u(e.data+r.data):"string"===e.type&&"string"===r.type?p(e.data+r.data):"object"===e.type&&"object"===r.type?f({...e.data,...r.data},n):"array"===e.type&&"array"===r.type?f(e.data.concat(r.data),n):e.isArray()&&r.isArray()?"sync"===n?h((function*(){const t=[...yield e.get(),...yield r.get()];return new o(t,"array")})):new t((async function*(){for await(const t of e)yield t;for await(const e of r)yield e})):i},"-":function(e,t){return"datetime"===e.type&&"number"===t.type?l(e.data.add(-t.data)):"datetime"===e.type&&"datetime"===t.type?u(e.data.difference(t.data)):"number"===e.type&&"number"===t.type?u(e.data-t.data):i},"*":S(((e,t)=>e*t)),"/":S(((e,t)=>e/t)),"%":S(((e,t)=>e%t)),"**":S(((e,t)=>Math.pow(e,t)))};function S(e){return function(t,r){if("number"===t.type&&"number"===r.type){return u(e(t.data,r.data))}return i}}let O=class e{params;source;value;parent;context;isHidden=!1;constructor(e,t,r,n,o){this.params=e,this.source=t,this.value=r,this.context=n,this.parent=o}createNested(t){return this.isHidden?new e(this.params,this.source,t,this.context,this.parent):new e(this.params,this.source,t,this.context,this)}createHidden(e){const t=this.createNested(e);return t.isHidden=!0,t}};function I(e,t,r){return(0,$[e.type])(e,t,r)}const $={This:(e,t)=>t.value,Selector(){throw new Error("Selectors can not be evaluated")},Everything:(e,t)=>t.source,Parameter:({name:e},t,r)=>f(t.params[e],r),Context({key:e},t){if("before"===e||"after"===e)return t.context[e]||i;throw new Error(`unknown context key: ${e}`)},Parent({n:e},t){let r=t;for(let t=0;t<e;t++){if(!r.parent)return i;r=r.parent}return r.value},OpCall:({op:e,left:t,right:r},n,o)=>h((function*(){const i=j[e];if(!i)throw new Error(`Unknown operator: ${e}`);const s=yield I(t,n,o),a=yield I(r,n,o);return yield i(s,a,o)})),Select:({alternatives:e,fallback:t},r,n)=>h((function*(){for(const t of e){const e=yield I(t.condition,r,n);if("boolean"===e.type&&!0===e.data)return yield I(t.value,r,n)}return t?yield I(t,r,n):i})),InRange:({base:e,left:t,right:r,isInclusive:n},o,c)=>h((function*(){const u=yield I(e,o,c),p=yield I(t,o,c),l=yield I(r,o,c),f=A(yield u.get(),yield p.get());if(null===f)return i;const y=A(yield u.get(),yield l.get());return null===y?i:n?f>=0&&y<=0?s:a:f>=0&&y<0?s:a})),Filter:({base:e,expr:r},n,s)=>h((function*(){const a=yield I(e,n,s);if(!a.isArray())return i;if("sync"===s){const e=yield a.get(),t=[];for(const o of e){const e=f(o,s),i=n.createNested(e),a=yield I(r,i,s);"boolean"===a.type&&!0===a.data&&t.push(o)}return new o(t,"array")}return new t((async function*(){for await(const e of a){const t=n.createNested(e),o=await I(r,t,s);"boolean"===o.type&&!0===o.data&&(yield e)}}))})),Projection:({base:e,expr:t},r,n)=>h((function*(){const o=yield I(e,r,n);if("object"!==o.type)return i;const s=r.createNested(o);return yield I(t,s,n)})),FuncCall:({func:e,args:t},r,n)=>e(t,r,n),PipeFuncCall:({func:e,base:t,args:r},n,o)=>h((function*(){const i=yield I(t,n,o);return yield e(i,r,n,o)})),AccessAttribute:({base:e,name:t},r,n)=>h((function*(){let o=r.value;return e&&(o=yield I(e,r,n)),"object"===o.type&&o.data.hasOwnProperty(t)?f(o.data[t],n):i})),AccessElement:({base:e,index:t},r,n)=>h((function*(){const o=yield I(e,r,n);if(!o.isArray())return i;const s=yield o.get();return f(s[t<0?t+s.length:t],n)})),Slice:({base:e,left:t,right:r,isInclusive:n},o,s)=>h((function*(){const a=yield I(e,o,s);if(!a.isArray())return i;const c=yield a.get();let u=t,p=r;return u<0&&(u=c.length+u),p<0&&(p=c.length+p),n&&p++,u<0&&(u=0),p<0&&(p=0),f(c.slice(u,p),s)})),Deref:({base:e},t,r)=>h((function*(){const n=yield I(e,t,r);if(!t.source.isArray()||"object"!==n.type)return i;const o=n.data._ref;if("string"!=typeof o)return i;if(t.context.dereference){return f(yield t.context.dereference({_ref:o}),r)}return(yield t.source.first((e=>"object"===e.type&&o==e.data._id)))||i})),Value:({value:e},t,r)=>f(e,r),Group:({base:e},t,r)=>I(e,t,r),Object:({attributes:e},t,r)=>h((function*(){const n={};for(const o of e){const e=o.type;switch(o.type){case"ObjectAttributeValue":{const e=yield I(o.value,t,r);n[o.name]=yield e.get();break}case"ObjectConditionalSplat":{const e=yield I(o.condition,t,r);if("boolean"!==e.type||!1===e.data)continue;const i=yield I(o.value,t,r);"object"===i.type&&Object.assign(n,i.data);break}case"ObjectSplat":{const e=yield I(o.value,t,r);"object"===e.type&&Object.assign(n,e.data);break}default:throw new Error(`Unknown node type: ${e}`)}}return f(n,r)})),Array:({elements:e},r,n)=>h((function*(){if("sync"===n){const t=[];for(const o of e){const e=yield I(o.value,r,n);if(o.isSplat){if(e.isArray()){const r=yield e.get();t.push(...r)}}else t.push(yield e.get())}return new o(t,"array")}return new t((async function*(){for(const t of e){const e=await I(t.value,r,n);if(t.isSplat){if(e.isArray())for await(const t of e)yield t}else yield e}}))})),Tuple(){throw new Error("tuples can not be evaluated")},Or:({left:e,right:t},r,n)=>h((function*(){const o=yield I(e,r,n),c=yield I(t,r,n);return"boolean"===o.type&&!0===o.data||"boolean"===c.type&&!0===c.data?s:"boolean"!==o.type||"boolean"!==c.type?i:a})),And:({left:e,right:t},r,n)=>h((function*(){const o=yield I(e,r,n),c=yield I(t,r,n);return"boolean"===o.type&&!1===o.data||"boolean"===c.type&&!1===c.data?a:"boolean"!==o.type||"boolean"!==c.type?i:s})),Not:({base:e},t,r)=>h((function*(){const n=yield I(e,t,r);return"boolean"!==n.type?i:n.data?a:s})),Neg:({base:e},t,r)=>h((function*(){const n=yield I(e,t,r);return"number"!==n.type?i:u(-n.data)})),Pos:({base:e},t,r)=>h((function*(){const n=yield I(e,t,r);return"number"!==n.type?i:u(n.data)})),Asc:()=>i,Desc:()=>i,ArrayCoerce:({base:e},t,r)=>h((function*(){const n=yield I(e,t,r);return n.isArray()?n:i})),Map:({base:e,expr:r},n,s)=>h((function*(){const a=yield I(e,n,s);if(!a.isArray())return i;if("sync"===s){const e=yield a.get(),t=[];for(const o of e){const e=f(o,"sync"),i=n.createHidden(e),a=yield I(r,i,s);t.push(yield a.get())}return new o(t,"array")}return new t((async function*(){for await(const e of a){const t=n.createHidden(e);yield await I(r,t,s)}}))})),FlatMap:({base:e,expr:r},n,s)=>h((function*(){const a=yield I(e,n,s);if(!a.isArray())return i;if("sync"===s){const e=yield a.get(),t=[];for(const o of e){const e=f(o,"sync"),i=n.createHidden(e),a=yield I(r,i,s);if(a.isArray()){const e=yield a.get();t.push(...e)}else{const e=yield a.get();t.push(e)}}return new o(t,"array")}return new t((async function*(){for await(const e of a){const t=n.createHidden(e),o=await I(r,t,s);if(o.isArray())for await(const e of o)yield e;else yield o}}))}))};function C(e,t={}){const r=f(t.root,"sync"),n=f(t.dataset,"sync"),o={...t.params},i=new O(o,n,r,function(e={},t){return{timestamp:e.timestamp||new Date,identity:void 0===e.identity?"me":e.identity,sanity:e.sanity,after:e.after?f(e.after,t):null,before:e.before?f(e.before,t):null,dereference:e.dereference}}(t,"sync"),null),s=I(e,i,"sync");if(d(s))throw new Error("Unexpected promise when evaluating. This expression may not support evaluateSync.");return s}function M(e){switch(e.type){case"Group":return M(e.base);case"Value":case"Parameter":return!0;case"Pos":case"Neg":return M(e.base);case"OpCall":switch(e.op){case"+":case"-":case"*":case"/":case"%":case"**":return M(e.left)&&M(e.right);default:return!1}default:return!1}}const T=new O({},i,i,{timestamp:new Date(0),identity:"me",before:null,after:null},null);function P(e){return M(e)?function(e){const t=I(e,T,"sync");if(d(t))throw new Error("BUG: constant evaluate should never return a promise");return t}(e):null}function N(e,t){return h((function*(){if("object"===e.type)return V(e.data);if(e.isArray()){const r=yield U(e,t);if(r.length>0)return r.join("\n\n")}return null}))}function U(e,t){return h((function*(){const r=[],n=yield e.get();for(const e of n){const n=f(e,t);if("object"===n.type){const e=V(n.data);null!==e&&r.push(e)}else if(n.isArray()){const e=yield U(n,t);r.push(...e)}}return r}))}function V(e){if("string"!=typeof e._type)return null;const t=e.children;if(!Array.isArray(t))return null;let r="";for(const e of t)e&&"object"==typeof e&&"string"==typeof e._type&&"span"===e._type&&"string"==typeof e.text&&(r+=e.text);return r}const D=1.2;function F(e,t,r){return h((function*(){if("OpCall"===e.type&&"match"===e.op){return function(e,t){return h((function*(){const r=yield e.get(),n=yield t.get();let o,i=[];if(Array.isArray(r)?i=r.filter((e=>"string"==typeof e)):"string"==typeof r&&(i=[r]),Array.isArray(n)?o=n.filter((e=>"string"==typeof e)):"string"==typeof n&&(o=[n]),!o?.length)return 0;const s=i.flatMap(k),a=o.flatMap(x);if(0===s.length||0===a.length)return 0;let c=0;for(const e of a){const t=s.reduce(((t,r)=>t+(e.test(r)?1:0)),0);c+=2.2*t/(t+D)}return c}))}(yield I(e.left,t,r),yield I(e.right,t,r))}if("FuncCall"===e.type&&"boost"===e.name){const n=yield F(e.args[0],t,r),o=yield I(e.args[1],t,r);return"number"===o.type&&n>0?n+o.data:0}switch(e.type){case"Or":return(yield F(e.left,t,r))+(yield F(e.right,t,r));case"And":{const n=yield F(e.left,t,r),o=yield F(e.right,t,r);return 0===n||0===o?0:n+o}default:{const n=yield I(e,t,r);return"boolean"===n.type&&!0===n.data?1:0}}}))}function R(e,t){switch(y(e)){case"array":for(const r of e)if(R(r,t))return!0;break;case"object":if(e._ref)return t.has(e._ref);for(const r of Object.values(e))if(R(r,t))return!0}return!1}const q={anywhere:function(){throw new Error("not implemented")}};q.anywhere.arity=1,q.coalesce=function(e,t,r){return h((function*(){for(const n of e){const e=yield I(n,t,r);if("null"!==e.type)return e}return i}))},q.count=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if(!n.isArray())return i;return u(yield n.reduce((e=>e+1),0))}))},q.count.arity=1,q.dateTime=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);return"datetime"===n.type?n:"string"!==n.type?i:c.parseToValue(n.data)}))},q.dateTime.arity=1,q.defined=function(e,t,r){return h((function*(){return"null"===(yield I(e[0],t,r)).type?a:s}))},q.defined.arity=1,q.identity=function(e,t){return p(t.context.identity)},q.identity.arity=0,q.length=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if("string"===n.type)return u(function(e){let t=0;for(let r=0;r<e.length;r++){const n=e.charCodeAt(r);n>=55296&&n<=56319||t++}return t}(n.data));if(n.isArray()){return u(yield n.reduce((e=>e+1),0))}return i}))},q.length.arity=1,q.path=function(t,r,n){return h((function*(){const s=yield I(t[0],r,n);return"string"!==s.type?i:(a=new e(s.data),new o(a,"path"));var a}))},q.path.arity=1,q.string=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);switch(n.type){case"number":case"string":case"boolean":case"datetime":return p(`${n.data}`);default:return i}}))},q.string.arity=1,q.references=function(e,t,r){return h((function*(){const n=new Set;for(const o of e){const e=yield I(o,t,r);if("string"===e.type)n.add(e.data);else if(e.isArray()){const t=yield e.get();for(const e of t)"string"==typeof e&&n.add(e)}}if(0===n.size)return a;return R(yield t.value.get(),n)?s:a}))},q.references.arity=e=>e>=1,q.round=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if("number"!==n.type)return i;const o=n.data;let s=0;if(2===e.length){const n=yield I(e[1],t,r);if("number"!==n.type||n.data<0||!Number.isInteger(n.data))return i;s=n.data}return u(0===s?o<0?-Math.round(-o):Math.round(o):Number(o.toFixed(s)))}))},q.round.arity=e=>e>=1&&e<=2,q.now=function(e,t){return p(t.context.timestamp.toISOString())},q.now.arity=0,q.boost=function(){throw new Error("unexpected boost call")},q.boost.arity=2;const G={lower:function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);return"string"!==n.type?i:p(n.data.toLowerCase())}))}};G.lower.arity=1,G.upper=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);return"string"!==n.type?i:p(n.data.toUpperCase())}))},G.upper.arity=1,G.split=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if("string"!==n.type)return i;const o=yield I(e[1],t,r);return"string"!==o.type?i:0===n.data.length?f([],r):0===o.data.length?f(Array.from(n.data),r):f(n.data.split(o.data),r)}))},G.split.arity=2,q.lower=G.lower,q.upper=G.upper,G.startsWith=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if("string"!==n.type)return i;const o=yield I(e[1],t,r);return"string"!==o.type?i:n.data.startsWith(o.data)?s:a}))},G.startsWith.arity=2;const H={join:function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if(!n.isArray())return i;const o=yield I(e[1],t,r);if("string"!==o.type)return i;let s="",a=!1;const c=yield n.get();for(const e of c){const t=f(e,r);switch(a&&(s+=o.data),t.type){case"number":case"string":case"boolean":case"datetime":s+=`${t.data}`;break;default:return i}a=!0}return f(s,r)}))}};H.join.arity=2,H.compact=function(e,r,n){return h((function*(){const o=yield I(e[0],r,n);return o.isArray()?new t((async function*(){for await(const e of o)"null"!==e.type&&(yield e)})):i}))},H.compact.arity=1,H.unique=function(e,r,n){return h((function*(){const s=yield I(e[0],r,n);if(!s.isArray())return i;if("sync"===n){const e=yield s.get(),t=new Set,r=[];for(const n of e){const e=f(n,"sync");switch(e.type){case"number":case"string":case"boolean":case"datetime":t.has(n)||(t.add(n),r.push(e));break;default:r.push(e)}}return new o(r,"array")}return new t((async function*(){const e=new Set;for await(const t of s)switch(t.type){case"number":case"string":case"boolean":case"datetime":e.has(t.data)||(e.add(t.data),yield t);break;default:yield t}}))}))},H.unique.arity=1,H.intersects=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if(!n.isArray())return i;const o=yield I(e[1],t,r);return o.isArray()?(yield n.first((e=>!!o.first((t=>m(e,t))))))?s:a:i}))},H.intersects.arity=2;const B={text:function(e,t,r){return h((function*(){const n=yield I(e[0],t,r),o=yield N(n,r);return null===o?i:p(o)}))}};B.text.arity=1;const W={projectId:function(e,t){return t.context.sanity?p(t.context.sanity.projectId):i},dataset:function(e,t){return t.context.sanity?p(t.context.sanity.dataset):i},versionsOf:function(e,t,r){return h((function*(){if(!t.source.isArray())return i;const n=yield I(e[0],t,r);if("string"!==n.type)return i;const o=n.data;return f(yield t.source.reduce(((e,t)=>{if("object"===y(t)){const r=t.get();r&&"_id"in r&&2===r._id.split(".").length&&r._id.endsWith(`.${o}`)&&"_version"in r&&"object"==typeof r._version&&e.push(r._id)}return e}),[]),r)}))}};W.versionsOf.arity=1,W.partOfRelease=function(e,t,r){return h((function*(){if(!t.source.isArray())return i;const n=yield I(e[0],t,r);if("string"!==n.type)return i;const o=n.data;return f(yield t.source.reduce(((e,t)=>{if("object"===y(t)){const r=t.get();r&&"_id"in r&&2===r._id.split(".").length&&r._id.startsWith(`${o}.`)&&"_version"in r&&"object"==typeof r._version&&e.push(r._id)}return e}),[]),r)}))},W.partOfRelease.arity=1;const Z={order:function(e,t,r,n){return h((function*(){if(!e.isArray())return i;const o=[],s=[];let a=0;for(let e of t){let t="asc";"Desc"===e.type?(t="desc",e=e.base):"Asc"===e.type&&(e=e.base),o.push(e),s.push(t),a++}const c=[];let u=0;const p=yield e.get();for(const e of p){const t=f(e,n),i=r.createNested(t),s=[yield t.get(),u];for(let e=0;e<a;e++){const t=yield I(o[e],i,n);s.push(yield t.get())}c.push(s),u++}return c.sort(((e,t)=>{for(let r=0;r<a;r++){let n=E(e[r+2],t[r+2]);if("desc"===s[r]&&(n=-n),0!==n)return n}return e[1]-t[1]})),f(c.map((e=>e[0])),n)}))}};Z.order.arity=e=>e>=1,Z.score=function(e,t,r,n){return h((function*(){if(!e.isArray())return i;const o=[],s=[],a=yield e.get();for(const e of a){const i=f(e,n);if("object"!==i.type){o.push(yield i.get());continue}const a=r.createNested(i);let c="number"==typeof i.data._score?i.data._score:0;for(const e of t)c+=(yield F(e,a,n));const u=Object.assign({},i.data,{_score:c});s.push(u)}return s.sort(((e,t)=>t._score-e._score)),f(s,n)}))},Z.score.arity=e=>e>=1;const z={operation:function(e,t){const r=null!==t.context.before,n=null!==t.context.after;return r&&n?p("update"):n?p("create"):r?p("delete"):i},changedAny:()=>{throw new Error("not implemented")}};z.changedAny.arity=1,z.changedAny.mode="delta",z.changedOnly=()=>{throw new Error("not implemented")},z.changedOnly.arity=1,z.changedOnly.mode="delta";const J={changedAny:()=>{throw new Error("not implemented")}};J.changedAny.arity=3,J.changedOnly=()=>{throw new Error("not implemented")},J.changedOnly.arity=3;const Q={min:function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if(!n.isArray()||(yield n.first((e=>"null"!==e.type&&"number"!==e.type))))return i;const o=yield n.get();let s;for(const e of o)"number"==typeof e&&(void 0===s||e<s)&&(s=e);return f(s,r)}))}};Q.min.arity=1,Q.max=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if(!n.isArray()||(yield n.first((e=>"null"!==e.type&&"number"!==e.type))))return i;const o=yield n.get();let s;for(const e of o)"number"==typeof e&&(void 0===s||e>s)&&(s=e);return f(s,r)}))},Q.max.arity=1,Q.sum=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if(!n.isArray()||(yield n.first((e=>"null"!==e.type&&"number"!==e.type))))return i;return f(yield n.reduce(((e,t)=>"number"!==t.type?e:e+t.data),0),r)}))},Q.sum.arity=1,Q.avg=function(e,t,r){return h((function*(){const n=yield I(e[0],t,r);if(!n.isArray()||(yield n.first((e=>"null"!==e.type&&"number"!==e.type))))return i;const o=yield n.reduce(((e,t)=>"number"!==t.type?e:e+1),0),s=yield n.reduce(((e,t)=>"number"!==t.type?e:e+t.data),0);return 0===o?i:f(s/o,r)}))},Q.avg.arity=1;const L={now:function(e,t){return l(new c(t.context.timestamp))}};L.now.arity=0;const Y={global:q,string:G,array:H,pt:B,delta:z,diff:J,sanity:W,math:Q,dateTime:L};class K{string;marks;index;parseOptions;allowBoost=!1;constructor(e,t,r){this.string=e,this.marks=t,this.index=0,this.parseOptions=r}hasMark(e=0){return this.index+e<this.marks.length}getMark(e=0){return this.marks[this.index+e]}shift(){this.index+=1}process(e){const t=this.marks[this.index];this.shift();const r=e[t.name];if(!r)throw new Error(`Unknown handler: ${t.name}`);return r.call(e,this,t)}processString(){return this.shift(),this.processStringEnd()}processStringEnd(){const e=this.marks[this.index-1],t=this.marks[this.index];return this.shift(),this.string.slice(e.position,t.position)}slice(e){const t=this.marks[this.index].position;return this.string.slice(t,t+e)}}const X=/^([\t\n\v\f\r \u0085\u00A0]|(\/\/[^\n]*\n))+/,ee=/^\d+/,te=/^[a-zA-Z_][a-zA-Z_0-9]*/;function re(e,t,r){let n,o=t;switch(e[t]){case"+":{let r=re(e,se(e,t+1),10);if("error"===r.type)return r;n=[{name:"pos",position:o}].concat(r.marks),t=r.position;break}case"-":{let r=re(e,se(e,t+1),8);if("error"===r.type)return r;n=[{name:"neg",position:o}].concat(r.marks),t=r.position;break}case"(":{let r=re(e,se(e,t+1),0);if("error"===r.type)return r;switch(e[t=se(e,r.position)]){case",":for(n=[{name:"tuple",position:o}].concat(r.marks),t=se(e,t+1);;){if(r=re(e,t,0),"error"===r.type)return r;if(","!==e[t=se(e,r.position)])break;t=se(e,t+1)}if(")"!==e[t])return{type:"error",position:t};t++,n.push({name:"tuple_end",position:t});break;case")":t++,n=[{name:"group",position:o}].concat(r.marks);break;default:return{type:"error",position:t}}break}case"!":{let r=re(e,se(e,t+1),10);if("error"===r.type)return r;n=[{name:"not",position:o}].concat(r.marks),t=r.position;break}case"{":{let r=ie(e,t);if("error"===r.type)return r;n=r.marks,t=r.position;break}case"[":if(n=[{name:"array",position:t}],"]"!==e[t=se(e,t+1)])for(;;){"..."===e.slice(t,t+3)&&(n.push({name:"array_splat",position:t}),t=se(e,t+3));let r=re(e,t,0);if("error"===r.type)return r;if(n=n.concat(r.marks),","!==e[t=se(e,t=r.position)]||"]"===e[t=se(e,t+1)])break}if("]"!==e[t])return{type:"error",position:t};t++,n.push({name:"array_end",position:t});break;case"'":case'"':{let r=function(e,t){let r=e[t];t+=1;const n=[{name:"str",position:t}];e:for(;;t++){if(t>e.length)return{type:"error",position:t};switch(e[t]){case r:n.push({name:"str_end",position:t}),t++;break e;case"\\":n.push({name:"str_pause",position:t}),"u"===e[t+1]?"{"===e[t+2]?(n.push({name:"unicode_hex",position:t+3}),t=e.indexOf("}",t+3),n.push({name:"unicode_hex_end",position:t})):(n.push({name:"unicode_hex",position:t+2}),n.push({name:"unicode_hex_end",position:t+6}),t+=5):(n.push({name:"single_escape",position:t+1}),t+=1),n.push({name:"str_start",position:t+1})}}return{type:"success",marks:n,position:t}}(e,t);if("error"===r.type)return r;n=r.marks,t=r.position;break}case"^":for(t++,n=[];"."===e[t]&&"^"===e[t+1];)n.push({name:"dblparent",position:o}),t+=2;n.push({name:"parent",position:o});break;case"@":n=[{name:"this",position:o}],t++;break;case"*":n=[{name:"everything",position:o}],t++;break;case"$":{let r=ae(e,t+1,te);r&&(n=[{name:"param",position:o},{name:"ident",position:o+1},{name:"ident_end",position:t+=1+r}]);break}default:{let r=ae(e,t,ee);if(r){let i="integer";if("."===e[t+=r]){let r=ae(e,t+1,ee);r&&(i="float",t+=1+r)}if("e"===e[t]||"E"===e[t]){i="sci",("+"===e[++t]||"-"===e[t])&&t++;let r=ae(e,t,ee);if(!r)return{type:"error",position:t};t+=r}n=[{name:i,position:o},{name:i+"_end",position:t}];break}let i=ae(e,t,te);if(i){switch(e[t+=i]){case":":case"(":{let r=oe(e,o,t);if("error"===r.type)return r;n=r.marks,t=r.position;break}default:n=[{name:"this_attr",position:o},{name:"ident",position:o},{name:"ident_end",position:t}]}break}}}if(!n)return{type:"error",position:t};let i,s=12;e:for(;;){let a=se(e,t);if(a===e.length){t=a;break}if(i=ne(e,a),"success"!==i.type)switch(e[a]){case"=":switch(e[a+1]){case">":{if(r>1||s<=1)break e;let i=re(e,se(e,a+2),1);if("error"===i.type)return i;n=n.concat(i.marks),n.unshift({name:"pair",position:o}),t=i.position,s=1;break}case"=":{if(r>4||s<=4)break e;let i=re(e,se(e,a+2),5);if("error"===i.type)return i;n.unshift({name:"comp",position:o}),n.push({name:"op",position:a},{name:"op_end",position:a+2}),n=n.concat(i.marks),t=i.position,s=4;break}default:break e}break;case"+":{if(r>6||s<6)break e;let i=re(e,se(e,a+1),7);if("error"===i.type)return i;n=n.concat(i.marks),n.unshift({name:"add",position:o}),t=i.position,s=6;break}case"-":{if(r>6||s<6)break e;let i=re(e,se(e,a+1),7);if("error"===i.type)return i;n=n.concat(i.marks),n.unshift({name:"sub",position:o}),t=i.position,s=6;break}case"*":{if("*"===e[a+1]){if(r>8||s<=8)break e;let i=re(e,se(e,a+2),8);if("error"===i.type)return i;n=n.concat(i.marks),n.unshift({name:"pow",position:o}),t=i.position,s=8;break}if(r>7||s<7)break e;let i=re(e,se(e,a+1),8);if("error"===i.type)return i;n=n.concat(i.marks),n.unshift({name:"mul",position:o}),t=i.position,s=7;break}case"/":{if(r>7||s<7)break e;let i=re(e,se(e,a+1),8);if("error"===i.type)return i;n=n.concat(i.marks),n.unshift({name:"div",position:o}),t=i.position,s=7;break}case"%":{if(r>7||s<7)break e;let i=re(e,se(e,a+1),8);if("error"===i.type)return i;n=n.concat(i.marks),n.unshift({name:"mod",position:o}),t=i.position,s=7;break}case"<":case">":{if(r>4||s<=4)break e;let i=a+1;"="===e[i]&&i++;let c=re(e,se(e,i),5);if("error"===c.type)return c;n.unshift({name:"comp",position:o}),n.push({name:"op",position:a},{name:"op_end",position:i}),n=n.concat(c.marks),t=c.position,s=4;break}case"|":if("|"===e[a+1]){if(r>2||s<2)break e;let i=re(e,se(e,a+2),3);if("error"===i.type)return i;n=n.concat(i.marks),n.unshift({name:"or",position:o}),t=i.position,s=2}else{if(r>11||s<11)break e;let i=se(e,a+1),c=ae(e,i,te);if(!c)return{type:"error",position:i};if("("===e[t=i+c]||":"===e[t]){let r=oe(e,i,t);if("error"===r.type)return r;n=n.concat(r.marks),n.unshift({name:"pipecall",position:o}),t=r.position,s=11}}break;case"&":{if("&"!=e[a+1]||r>3||s<3)break e;let i=re(e,se(e,a+2),4);if("error"===i.type)return i;n=n.concat(i.marks),n.unshift({name:"and",position:o}),t=i.position,s=3;break}case"!":{if("="!==e[a+1]||r>4||s<=4)break e;let i=re(e,se(e,a+2),5);if("error"===i.type)return i;n.unshift({name:"comp",position:o}),n.push({name:"op",position:a},{name:"op_end",position:a+2}),n=n.concat(i.marks),t=i.position,s=4;break}case"d":if("desc"!==e.slice(a,a+4)||r>4||s<4)break e;n.unshift({name:"desc",position:o}),t=a+4,s=4;break;case"a":if("asc"!==e.slice(a,a+3)||r>4||s<4)break e;n.unshift({name:"asc",position:o}),t=a+3,s=4;break;default:switch(ce(e,a,te)){case"in":{if(r>4||s<=4)break e;let i=!1;"("===e[t=se(e,a+2)]&&(i=!0,t=se(e,t+1));let c=t,u=re(e,t,5);if("error"===u.type)return u;if("."===e[t=se(e,u.position)]&&"."===e[t+1]){let r="inc_range";"."===e[t+2]?(r="exc_range",t=se(e,t+3)):t=se(e,t+2);let i=re(e,t,5);if("error"===i.type)return i;n.unshift({name:"in_range",position:o}),n=n.concat({name:r,position:c},u.marks,i.marks),t=i.position}else n.unshift({name:"comp",position:o}),n.push({name:"op",position:a},{name:"op_end",position:a+2}),n=n.concat(u.marks);if(i){if(")"!==e[t=se(e,t)])return{type:"error",position:t};t++}s=4;break}case"match":{if(r>4||s<=4)break e;let i=re(e,se(e,a+5),5);if("error"===i.type)return i;n.unshift({name:"comp",position:o}),n.push({name:"op",position:a},{name:"op_end",position:a+5}),n=n.concat(i.marks),t=i.position,s=4;break}default:break e}}else{for(n.unshift({name:"traverse",position:o});"success"===i.type;)n=n.concat(i.marks),i=ne(e,se(e,t=i.position));n.push({name:"traversal_end",position:t})}}return{type:"success",marks:n,position:t,failPosition:"error"===i?.type&&i.position}}function ne(e,t){let r=t;switch(e[t]){case".":{let n=t=se(e,t+1),o=ae(e,t,te);return o?{type:"success",marks:[{name:"attr_access",position:r},{name:"ident",position:n},{name:"ident_end",position:t+=o}],position:t}:{type:"error",position:t}}case"-":if(">"!==e[t+1])return{type:"error",position:t};let n=[{name:"deref",position:r}],o=se(e,t+=2),i=ae(e,o,te);return i&&(t=o+i,n.push({name:"deref_attr",position:o},{name:"ident",position:o},{name:"ident_end",position:t})),{type:"success",marks:n,position:t};case"[":{if("]"===e[t=se(e,t+1)])return{type:"success",marks:[{name:"array_postfix",position:r}],position:t+1};let n=t,o=re(e,t,0);if("error"===o.type)return o;if("."===e[t=se(e,o.position)]&&"."===e[t+1]){let i="inc_range";"."===e[t+2]?(i="exc_range",t+=3):t+=2;let s=re(e,t=se(e,t),0);return"error"===s.type?s:"]"!==e[t=se(e,s.position)]?{type:"error",position:t}:{type:"success",marks:[{name:"slice",position:r},{name:i,position:n}].concat(o.marks,s.marks),position:t+1}}return"]"!==e[t]?{type:"error",position:t}:{type:"success",marks:[{name:"square_bracket",position:r}].concat(o.marks),position:t+1}}case"|":if("{"===e[t=se(e,t+1)]){let n=ie(e,t);return"error"===n.type||n.marks.unshift({name:"projection",position:r}),n}break;case"{":{let n=ie(e,t);return"error"===n.type||n.marks.unshift({name:"projection",position:r}),n}}return{type:"error",position:t}}function oe(e,t,r){let n=[];if(n.push({name:"func_call",position:t}),":"===e[r]&&":"===e[r+1]){n.push({name:"namespace",position:t}),n.push({name:"ident",position:t},{name:"ident_end",position:r});let o=ae(e,r=se(e,r+2),te);if(!o)return{type:"error",position:r};if(n.push({name:"ident",position:r},{name:"ident_end",position:r+o}),"("!==e[r=se(e,r+o)])return{type:"error",position:r};r=se(e,++r)}else n.push({name:"ident",position:t},{name:"ident_end",position:r}),r=se(e,r+1);let o=r;if(")"!==e[r])for(;;){let t=re(e,r,0);if("error"===t.type)return t;if(n=n.concat(t.marks),o=t.position,","!==e[r=se(e,t.position)]||")"===e[r=se(e,r+1)])break}return")"!==e[r]?{type:"error",position:r}:(n.push({name:"func_args_end",position:o}),{type:"success",marks:n,position:r+1})}function ie(e,t){let r=[{name:"object",position:t}];for(t=se(e,t+1);"}"!==e[t];){let n=t;if("..."===e.slice(t,t+3))if("}"!==e[t=se(e,t+3)]&&","!==e[t]){let o=re(e,t,0);if("error"===o.type)return o;r.push({name:"object_splat",position:n}),r=r.concat(o.marks),t=o.position}else r.push({name:"object_splat_this",position:n});else{let o=re(e,t,0);if("error"===o.type)return o;let i=se(e,o.position);if("str"===o.marks[0].name&&":"===e[i]){let s=re(e,se(e,i+1),0);if("error"===s.type)return s;r.push({name:"object_pair",position:n}),r=r.concat(o.marks,s.marks),t=s.position}else r=r.concat({name:"object_expr",position:t},o.marks),t=o.position}if(","!==e[t=se(e,t)])break;t=se(e,t+1)}return"}"!==e[t]?{type:"error",position:t}:(t++,r.push({name:"object_end",position:t}),{type:"success",marks:r,position:t})}function se(e,t){return t+ae(e,t,X)}function ae(e,t,r){let n=r.exec(e.slice(t));return n?n[0].length:0}function ce(e,t,r){let n=r.exec(e.slice(t));return n?n[0]:null}function ue(e,t){return r=>t(e(r))}function pe(e){return t=>({type:"Map",base:t,expr:e({type:"This"})})}function le(e,t){if(!t)return{type:"a-a",build:e};switch(t.type){case"a-a":return{type:"a-a",build:ue(e,t.build)};case"a-b":return{type:"a-b",build:ue(e,t.build)};case"b-b":return{type:"a-a",build:ue(e,pe(t.build))};case"b-a":return{type:"a-a",build:ue(e,(r=t.build,e=>({type:"FlatMap",base:e,expr:r({type:"This"})})))};default:throw new Error(`unknown type: ${t.type}`)}var r}function fe(e,t){if(!t)return{type:"b-b",build:e};switch(t.type){case"a-a":case"b-a":return{type:"b-a",build:ue(e,t.build)};case"a-b":case"b-b":return{type:"b-b",build:ue(e,t.build)};default:throw new Error(`unknown type: ${t.type}`)}}const ye={"'":"'",'"':'"',"\\":"\\","/":"/",b:"\b",f:"\f",n:"\n",r:"\r",t:"\t"};function de(e){const t=parseInt(e,16);return String.fromCharCode(t)}class he extends Error{name="GroqQueryError"}const me={group:e=>({type:"Group",base:e.process(me)}),everything:()=>({type:"Everything"}),this:()=>({type:"This"}),parent:()=>({type:"Parent",n:1}),dblparent:e=>({type:"Parent",n:e.process(me).n+1}),traverse(e){const t=e.process(me),r=[];for(;"traversal_end"!==e.getMark().name;)r.push(e.process(ge));e.shift();let n=null;for(let e=r.length-1;e>=0;e--)n=r[e](n);if(("Everything"===t.type||"Array"===t.type||"PipeFuncCall"===t.type)&&(n=le((e=>e),n)),null===n)throw new Error("BUG: unexpected empty traversal");return n.build(t)},this_attr(e){const t=e.processString();return"null"===t?{type:"Value",value:null}:"true"===t?{type:"Value",value:!0}:"false"===t?{type:"Value",value:!1}:{type:"AccessAttribute",name:t}},neg:e=>({type:"Neg",base:e.process(me)}),pos:e=>({type:"Pos",base:e.process(me)}),add:e=>({type:"OpCall",op:"+",left:e.process(me),right:e.process(me)}),sub:e=>({type:"OpCall",op:"-",left:e.process(me),right:e.process(me)}),mul:e=>({type:"OpCall",op:"*",left:e.process(me),right:e.process(me)}),div:e=>({type:"OpCall",op:"/",left:e.process(me),right:e.process(me)}),mod:e=>({type:"OpCall",op:"%",left:e.process(me),right:e.process(me)}),pow:e=>({type:"OpCall",op:"**",left:e.process(me),right:e.process(me)}),comp(e){const t=e.process(me);return{type:"OpCall",op:e.processString(),left:t,right:e.process(me)}},in_range(e){const t=e.process(me),r="inc_range"===e.getMark().name;e.shift();return{type:"InRange",base:t,left:e.process(me),right:e.process(me),isInclusive:r}},str(e){let t="";e:for(;e.hasMark();){const r=e.getMark();switch(r.name){case"str_end":t+=e.processStringEnd();break e;case"str_pause":t+=e.processStringEnd();break;case"str_start":e.shift();break;case"single_escape":{const r=e.slice(1);e.shift(),t+=ye[r];break}case"unicode_hex":e.shift(),t+=de(e.processStringEnd());break;default:throw new Error(`unexpected mark: ${r.name}`)}}return{type:"Value",value:t}},integer(e){const t=e.processStringEnd();return{type:"Value",value:Number(t)}},float(e){const t=e.processStringEnd();return{type:"Value",value:Number(t)}},sci(e){const t=e.processStringEnd();return{type:"Value",value:Number(t)}},object(e){const t=[];for(;"object_end"!==e.getMark().name;)t.push(e.process(be));return e.shift(),{type:"Object",attributes:t}},array(e){const t=[];for(;"array_end"!==e.getMark().name;){let r=!1;"array_splat"===e.getMark().name&&(r=!0,e.shift());const n=e.process(me);t.push({type:"ArrayElement",value:n,isSplat:r})}return e.shift(),{type:"Array",elements:t}},tuple(e){const t=[];for(;"tuple_end"!==e.getMark().name;)t.push(e.process(me));return e.shift(),{type:"Tuple",members:t}},func_call(e){let t="global";"namespace"===e.getMark().name&&(e.shift(),t=e.processString());const r=e.processString();if("global"===t&&"select"===r){const t={type:"Select",alternatives:[]};for(;"func_args_end"!==e.getMark().name;)if("pair"===e.getMark().name){if(t.fallback)throw new he("unexpected argument to select()");e.shift();const r=e.process(me),n=e.process(me);t.alternatives.push({type:"SelectAlternative",condition:r,value:n})}else{if(t.fallback)throw new he("unexpected argument to select()");const r=e.process(me);t.fallback=r}return e.shift(),t}const n=[];for(;"func_args_end"!==e.getMark().name;)xe(t,r,n.length)?(e.process(we),n.push({type:"Selector"})):n.push(e.process(me));if(e.shift(),"global"===t&&("before"===r||"after"===r)&&"delta"===e.parseOptions.mode)return{type:"Context",key:r};if("global"===t&&"boost"===r&&!e.allowBoost)throw new he("unexpected boost");const o=Y[t];if(!o)throw new he(`Undefined namespace: ${t}`);const i=o[r];if(!i)throw new he(`Undefined function: ${r}`);if(void 0!==i.arity&&ve(r,i.arity,n.length),void 0!==i.mode&&i.mode!==e.parseOptions.mode)throw new he(`Undefined function: ${r}`);return{type:"FuncCall",func:i,namespace:t,name:r,args:n}},pipecall(e){const t=e.process(me);e.shift();let r="global";if("namespace"===e.getMark().name&&(e.shift(),r=e.processString()),"global"!==r)throw new he(`Undefined namespace: ${r}`);const n=e.processString(),o=[],i=e.allowBoost;for("score"===n&&(e.allowBoost=!0);;){const t=e.getMark().name;if("func_args_end"===t)break;if("order"===n){if("asc"===t){e.shift(),o.push({type:"Asc",base:e.process(me)});continue}if("desc"===t){e.shift(),o.push({type:"Desc",base:e.process(me)});continue}}o.push(e.process(me))}e.shift(),e.allowBoost=i;const s=Z[n];if(!s)throw new he(`Undefined pipe function: ${n}`);return s.arity&&ve(n,s.arity,o.length),{type:"PipeFuncCall",func:s,base:t,name:n,args:o}},pair(){throw new he("unexpected =>")},and:e=>({type:"And",left:e.process(me),right:e.process(me)}),or:e=>({type:"Or",left:e.process(me),right:e.process(me)}),not:e=>({type:"Not",base:e.process(me)}),asc(){throw new he("unexpected asc")},desc(){throw new he("unexpected desc")},param(e){const t=e.processString();return e.parseOptions.params&&e.parseOptions.params.hasOwnProperty(t)?{type:"Value",value:e.parseOptions.params[t]}:{type:"Parameter",name:t}}},be={object_expr(e){if("pair"===e.getMark().name){e.shift();return{type:"ObjectConditionalSplat",condition:e.process(me),value:e.process(me)}}const t=e.process(me);return{type:"ObjectAttributeValue",name:ke(t),value:t}},object_pair(e){const t=e.process(me);if("Value"!==t.type)throw new Error("name must be string");const r=e.process(me);return{type:"ObjectAttributeValue",name:t.value,value:r}},object_splat:e=>({type:"ObjectSplat",value:e.process(me)}),object_splat_this:()=>({type:"ObjectSplat",value:{type:"This"}})},ge={square_bracket(e){const t=e.process(me),r=P(t);return r&&"number"===r.type?e=>function(e,t){if(!t)return{type:"a-b",build:e};switch(t.type){case"a-a":case"b-a":return{type:"a-a",build:ue(e,t.build)};case"a-b":case"b-b":return{type:"a-b",build:ue(e,t.build)};default:throw new Error(`unknown type: ${t.type}`)}}((e=>({type:"AccessElement",base:e,index:r.data})),e):r&&"string"===r.type?e=>fe((e=>({type:"AccessAttribute",base:e,name:r.data})),e):e=>le((e=>({type:"Filter",base:e,expr:t})),e)},slice(e){const t="inc_range"===e.getMark().name;e.shift();const r=e.process(me),n=e.process(me),o=P(r),i=P(n);if(!o||!i||"number"!==o.type||"number"!==i.type)throw new he("slicing must use constant numbers");return e=>le((e=>({type:"Slice",base:e,left:o.data,right:i.data,isInclusive:t})),e)},projection(e){const t=e.process(me);return e=>function(e,t){if(!t)return{type:"b-b",build:e};switch(t.type){case"a-a":return{type:"a-a",build:ue(pe(e),t.build)};case"a-b":return{type:"a-b",build:ue(pe(e),t.build)};case"b-a":return{type:"b-a",build:ue(e,t.build)};case"b-b":return{type:"b-b",build:ue(e,t.build)};default:throw new Error(`unknown type: ${t.type}`)}}((e=>({type:"Projection",base:e,expr:t})),e)},attr_access(e){const t=e.processString();return e=>fe((e=>({type:"AccessAttribute",base:e,name:t})),e)},deref(e){let t=null;"deref_attr"===e.getMark().name&&(e.shift(),t=e.processString());return e=>fe((e=>(e=>t?{type:"AccessAttribute",base:e,name:t}:e)({type:"Deref",base:e})),e)},array_postfix:()=>e=>le((e=>({type:"ArrayCoerce",base:e})),e)},we={group:e=>(e.process(we),null),everything(){throw new Error("Invalid selector syntax")},this(){throw new Error("Invalid selector syntax")},parent(){throw new Error("Invalid selector syntax")},dblparent(){throw new Error("Invalid selector syntax")},traverse(e){for(e.process(we);"traversal_end"!==e.getMark().name;)e.process(ge);return e.shift(),null},this_attr:e=>(e.processString(),null),neg(){throw new Error("Invalid selector syntax")},pos(){throw new Error("Invalid selector syntax")},add(){throw new Error("Invalid selector syntax")},sub(){throw new Error("Invalid selector syntax")},mul(){throw new Error("Invalid selector syntax")},div(){throw new Error("Invalid selector syntax")},mod(){throw new Error("Invalid selector syntax")},pow(){throw new Error("Invalid selector syntax")},comp(){throw new Error("Invalid selector syntax")},in_range(){throw new Error("Invalid selector syntax")},str(){throw new Error("Invalid selector syntax")},integer(){throw new Error("Invalid selector syntax")},float(){throw new Error("Invalid selector syntax")},sci(){throw new Error("Invalid selector syntax")},object(){throw new Error("Invalid selector syntax")},array(){throw new Error("Invalid selector syntax")},tuple(){throw new Error("Invalid selector syntax")},func_call(e,t){const r=me.func_call(e,t);if("anywhere"===r.name&&1===r.args.length)return null;throw new Error("Invalid selector syntax")},pipecall(){throw new Error("Invalid selector syntax")},pair(){throw new Error("Invalid selector syntax")},and(){throw new Error("Invalid selector syntax")},or(){throw new Error("Invalid selector syntax")},not(){throw new Error("Invalid selector syntax")},asc(){throw new Error("Invalid selector syntax")},desc(){throw new Error("Invalid selector syntax")},param(){throw new Error("Invalid selector syntax")}};function ke(e){if("AccessAttribute"===e.type&&!e.base)return e.name;if("PipeFuncCall"===e.type||"Deref"===e.type||"Map"===e.type||"Projection"===e.type||"Slice"===e.type||"Filter"===e.type||"AccessElement"===e.type||"ArrayCoerce"===e.type||"Group"===e.type)return ke(e.base);throw new he(`Cannot determine property key for type: ${e.type}`)}function ve(e,t,r){if("number"==typeof t){if(r!==t)throw new he(`Incorrect number of arguments to function ${e}(). Expected ${t}, got ${r}.`)}else if(t&&!t(r))throw new he(`Incorrect number of arguments to function ${e}().`)}function xe(e,t,r){return"diff"==e&&2==r&&["changedAny","changedOnly"].includes(t)}class _e extends Error{position;name="GroqSyntaxError";constructor(e){super(`Syntax error in GROQ query at position ${e}`),this.position=e}}function Ae(e,t={}){const r=function(e){let t=0;t=se(e,t);let r=re(e,t,0);return"error"===r.type?r:(t=se(e,r.position),t!==e.length?(r.failPosition&&(t=r.failPosition-1),{type:"error",position:t}):(delete r.position,delete r.failPosition,r))}(e);if("error"===r.type)throw new _e(r.position);return new K(e,r.marks,t).process(me)}export{C as evaluateSync,Ae as parse};