// @bun
var{defineProperty:G,getOwnPropertyNames:I,getOwnPropertyDescriptor:J}=Object,K=Object.prototype.hasOwnProperty;function L(h){return this[h]}var Z=(h)=>{var j=(H??=new WeakMap).get(h),q;if(j)return j;if(j=G({},"__esModule",{value:!0}),h&&typeof h==="object"||typeof h==="function"){for(var x of I(h))if(!K.call(j,x))G(j,x,{get:L.bind(h,x),enumerable:!(q=J(h,x))||q.enumerable})}return H.set(h,j),j},H;var M=(h)=>h;function O(h,j){this[h]=M.bind(null,j)}var _=(h,j)=>{for(var q in j)G(h,q,{get:j[q],enumerable:!0,configurable:!0,set:O.bind(j,q)})};var $=(h,j)=>()=>(h&&(j=h(h=0)),j);class Q{spec;constructor(h){this.spec=h}generate(h){let{primary:j}=this.spec.source,q=h.pagination?.page??1,x=h.pagination?.pageSize??R(this.spec.view)??20,A=(q-1)*x,B=x,D={skip:A,take:B,...h.filters};if(h.search)D.search=h.search;if(h.sort)D.orderBy={[h.sort.field]:h.sort.direction};return{operationName:j.key,input:D,meta:{pagination:{page:q,pageSize:x,skip:A,take:B},sorting:h.sort}}}validateParams(h){let j=[];if(h.filters&&this.spec.view.filters)for(let q of Object.keys(h.filters)){let x=this.spec.view.filters.find((B)=>B.key===q);if(!x){j.push(`Unknown filter key: ${q}`);continue}let A=T(x,h.filters[q]);if(A)j.push(A)}if(h.sort){let q=this.spec.view.fields.find((x)=>x.key===h.sort?.field);if(!q)j.push(`Unknown sort field: ${h.sort.field}`);else if(q.sortable===!1)j.push(`Field is not sortable: ${h.sort.field}`)}return j}}function R(h){if(h.kind==="detail")return;return h.collection?.pagination?.pageSize}function T(h,j){if(j==null)return;let q=U(j);switch(h.type){case"number":case"percent":case"currency":case"duration":return X(h.key,q);case"boolean":return typeof q==="boolean"?void 0:`Filter must be boolean: ${h.key}`;case"date":case"time":case"datetime":return Y(h.key,q);case"search":case"enum":default:return}}function U(h){if(!W(h))return h;switch(h.kind){case"single":return h.value;case"multi":return h.values;case"range":return[h.from,h.to].filter((j)=>j!=null);case"composite":return}}function W(h){return typeof h==="object"&&h!=null&&"kind"in h&&typeof h.kind==="string"}function X(h,j){return(Array.isArray(j)?j:[j]).every((x)=>typeof x==="number")?void 0:`Filter must be numeric: ${h}`}function Y(h,j){return(Array.isArray(j)?j:[j]).every((x)=>typeof x==="string"||typeof x==="number")?void 0:`Filter must be comparable: ${h}`}export{Q as DataViewQueryGenerator};
