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