import{and as c,eq as C,gt as s,lt as f,or as p,sql as O}from"drizzle-orm";import{decodeCursor as a}from"./utils/decode-cursor.js";const w=i=>{if(i===null||typeof i!="object")return!1;const n=i;return typeof n.value=="string"&&typeof n.id=="string"},D=(i,n)=>{const{after:A,before:b}=n;if(!A&&!b)return{sqlBuilder:i,whereCondition:null};const S=a(A),m=a(b),h=S!=null,j=m!=null&&!h,l=(n.sort&&n.sort.length>0?n.sort:[{field:"id",order:"DESC"}])[0],e=O.identifier(l.field),r=O.identifier("id");let y=null;function g(v,u){if(!v)return null;try{const d=JSON.parse(v);if(!w(d))return null;const t=String(d.value),o=String(d.id);return l.field==="id"?l.order==="ASC"?u==="after"?s(r,o):f(r,o):u==="after"?f(r,o):s(r,o):l.order==="ASC"?u==="after"?p(s(e,t),c(C(e,t),s(r,o))):p(f(e,t),c(C(e,t),f(r,o))):u==="after"?p(f(e,t),c(C(e,t),f(r,o))):p(s(e,t),c(C(e,t),s(r,o)))}catch{return null}}return h?y=g(S,"after"):j&&(y=g(m,"before")),{sqlBuilder:i,whereCondition:y}};export{D as applyAfterAndBefore};
