import{asc as a,desc as c,sql as f}from"drizzle-orm";import{transformToSnakeCase as p}from"./utils/transform-to-snake-case.js";import{getInvalidFields as m}from"./utils/field-pattern-matcher.js";function S(t,o=[],d={}){if(!t)return;const n=t.split(",").map(r=>r.startsWith("-")?{field:r.slice(1),order:"DESC"}:{field:r,order:"ASC"});if(o.length>0){const r=n.map(l=>l.field),i=m(r,o);if(i.length>0)throw new Error(`Invalid sort fields: ${i.join(", ")}`)}const e=n.map(r=>({field:d[r.field]||p(r.field),order:r.order}));return!e.some(r=>r.field==="id")&&e.length>0&&e.push({field:"id",order:e[0].order}),e}function g(t,o,d){if(!o.sort||o.sort.length===0)return t;const e=o.sort.map(s=>{const r=d?`${d}.${s.field}`:s.field;return s.order==="ASC"?a(f.raw(r)):c(f.raw(r))});return t.orderBy(...e)}export{g as applySorting,S as parseSort};
