import i from"@redocly/ajv";import*as s from"openapi-sampler";import{isObject as t}from"../../../../../utils/guards/is-object.js";function p(a,o){if(!t(a))return{value:null,error:"Invalid JSON Schema: schema must be a plain object."};const{mode:l,spec:u}=o??{};let e;try{e=s.sample(a,{...l==="read"?{skipWriteOnly:!0}:void 0,...l==="write"?{skipReadOnly:!0}:void 0},u)}catch(r){return{value:null,error:`Failed to generate sample from schema. Spec is required for schemas with $ref: ${r?.message||""}`}}if(e==null||t(e)&&Object.keys(e).length===0||Array.isArray(e)&&e.length===1&&e[0]===null){const r=new i({}),n=r.validateSchema(a);return typeof n!="boolean"?{value:null,error:"Invalid JSON Schema: Async schemas are not supported."}:!n&&r.errors?.length?{value:null,error:`Invalid JSON Schema: ${r.errorsText(r.errors,{separator:`
`})}`}:{value:e,error:null}}return{value:e,error:null}}export{p as generateSampleFromSchema};
