import{getSchema as E}from"./schema.js";import{isFormUrlEncoded as M,isJsonLike as D,isXmlLike as j,isMultipartFormData as w,isSequentialMediaType as k,JsonPointer as U,mapValues as F}from"../utils/index.js";import{getExamples as c}from"./example.js";import{safeSample as R,convertSampleToString as O}from"../services/code-samples/index.js";function A({parser:t,info:{encoding:l,schema:x,itemSchema:r},schema:o,mime:n,options:{isRequestType:h,onlyRequiredInSamples:P,generatedSamplesMaxDepth:I,format:v="json"}}){const m={skipReadOnly:h,skipWriteOnly:!h,skipNonRequired:h&&P,maxSampleDepth:I,quiet:!0,format:v};let a={};const b=k(n),S=!!r,d=o?.rawSchema?.prefixItems||o?.schema?.prefixItems||Array.isArray(o?.rawSchema?.items),u=r?t.deref(r).resolved:null,y=u&&Array.isArray(u.oneOf)&&u.oneOf.length>0||o&&o.oneOf;if(o&&o.oneOf)if(b&&S&&!d){const i=[];for(const e of o.oneOf){let f=R(e.rawSchema,m,t.definition);f!==null&&(o.discriminatorProp&&typeof f=="object"&&f&&f[o.discriminatorProp]&&!e.isDefaultMapping&&(f[o.discriminatorProp]=e.title),i.push(f))}if(i.length>0){const e=O(i,n,o);a.default=c({parser:t,infoOrRef:{value:e},mime:n,encoding:l})}}else for(const i of o.oneOf){let e=R(i.rawSchema,m,t.definition);e!==null&&(o.discriminatorProp&&typeof e=="object"&&e&&e[o.discriminatorProp]&&!i.isDefaultMapping&&(e[o.discriminatorProp]=i.title),e=O(e,n,i),a[i.title]=c({parser:t,infoOrRef:{value:e},mime:n,encoding:l}))}else if(y&&u&&Array.isArray(u.oneOf))if(b&&S&&!d){const i=[];for(const e of u.oneOf){const f=t.deref(e).resolved;let p=R(f,m,t.definition);p!==null&&i.push(p)}if(i.length>0){const e=O(i,n,o);a.default=c({parser:t,infoOrRef:{value:e},mime:n,encoding:l})}}else for(const i of u.oneOf){const e=t.deref(i).resolved;let f=R(e,m,t.definition);if(f===null)continue;f=O(f,n,o);const p=e.title||(i.$ref?t.deref(i).resolved.title:"")||"variant";a[p]=c({parser:t,infoOrRef:{value:f},mime:n,encoding:l})}else if(o){let e=R(x||r,m,t.definition);e!==null?(e=O(e,n,o,!0),a={default:c({parser:t,infoOrRef:{value:e},mime:n,encoding:l})}):a={default:c({parser:t,infoOrRef:{value:null},mime:n,encoding:l})}}else a={default:c({parser:t,infoOrRef:{value:null},mime:n,encoding:l})};return a}function N(t,l,x,r,o,{operation:n,type:h,response:P,absolutePointer:I=""}){let v,m;const a=r?.itemSchema??r.schema,b=r?.itemSchema?"itemSchema":"schema";let S;const d=a&&E({parser:t,schemaOrRef:a,pointer:"",absolutePointer:U.join(I,["content",l,b]),options:o,deps:{operation:n,type:h,response:P}}),{onlyRequiredInSamples:u,generatedSamplesMaxDepth:y}=o,{encoding:i,examples:e,example:f}=r;if(!o.unstable_skipSamples){const p=s=>typeof s=="string"?s:O(s,l,d);if(e!==void 0)v=F(e,s=>{const g=t.deref(s).resolved.dataValue||t.deref(s).resolved.serializedValue||t.deref(s).resolved.value,V=p(g);return c({parser:t,infoOrRef:{value:V},mime:l,encoding:i})});else if(f!==void 0){const s=t.deref(f).resolved,g=typeof s=="object"&&s!==null&&"value"in s?s.dataValue||s.serializedValue||s.value:s,V=p(g);v={default:c({parser:t,infoOrRef:{value:V},mime:l,encoding:i})}}else D(l)||j(l)||k(l)?(v=A({parser:t,info:r,schema:d,mime:l,options:{isRequestType:x,onlyRequiredInSamples:u,generatedSamplesMaxDepth:y,format:j(l)?"xml":"json"}}),S=d?.pointer):(M(l)||w(l))&&(S=d?.pointer,m=A({parser:t,info:r,schema:d,mime:l,options:{isRequestType:x,onlyRequiredInSamples:u,generatedSamplesMaxDepth:y}}))}return{examples:v,schema:d,examplesPointer:S,name:l,isRequestType:x,formExamples:m,onlyRequiredInSamples:u,operation:n}}export{N as getMediaType};
