import p from"@redocly/ajv";import{parseFilterQuery as a}from"./filter.js";import{parseSearch as m,SEARCHABLE_FIELDS as u}from"./search.js";import{parseSort as l}from"./sort.js";const y=()=>({type:"object",properties:{after:{type:"string"},before:{type:"string"},sort:{type:"string"},filter:{type:"string"},search:{type:"string"},limit:{anyOf:[{type:"string"},{type:"number",minimum:0}]},skip:{anyOf:[{type:"string"},{type:"number",minimum:0}]}},additionalProperties:!1,required:[]}),g=(e,o=[],s={})=>{const r={};if(e.after!==void 0&&(r.after=e.after),e.before!==void 0&&(r.before=e.before),e.sort!==void 0&&(r.sort=l(e.sort,o,s)),e.filter!==void 0&&(r.filter=a(e.filter,o,s)),e.search!==void 0&&(r.search=m(e.search,u,s)),e.limit!==void 0){const t=typeof e.limit=="string"?parseInt(e.limit,10):e.limit;!isNaN(t)&&t>=0&&(r.limit=t)}if(e.skip!==void 0){const t=typeof e.skip=="string"?parseInt(e.skip,10):e.skip;!isNaN(t)&&t>=0&&(r.skip=t)}return r},b=(e=[],o={})=>{const s=new p({coerceTypes:!0,removeAdditional:!0,strictSchema:!1,allowUnionTypes:!0,useDefaults:!0,allErrors:!0,strictTypes:!1}),r=y(),t=s.compile(r),f={validate:n=>{if(!t(n))return{success:!1,errors:t.errors?.map(c=>`${c.instancePath} ${c.message}`.trim())||["Validation failed"]};try{return{success:!0,data:g(n,e,o)}}catch(i){return{success:!1,errors:[i instanceof Error?i.message:"Transformation failed"]}}},parse:n=>{if(!n)return{};const i=f.validate(n);if(!i.success)throw new Error(`Validation failed: ${i.errors.join(", ")}`);return i.data}};return f};export{b as createPaginationParamsValidator};
