import{jsx as i,jsxs as S}from"react/jsx-runtime";import{memo as M,useCallback as j,useMemo as C}from"react";import{LayoutVariant as D}from"@redocly/config";import{useAtom as k,useAtomValue as O}from"jotai";import{useWriteAtom as T}from"../../jotai/use-write-atom.js";import{SamplesMiddlePanel as b,SamplesPanel as N,Row as P,Section as E,CodeBlockPanel as w}from"../common/index.js";import{getMediaType as B}from"../../models/index.js";import{MediaTypeSamples as I}from"../PayloadSamples/index.js";import{Schema as L}from"../Schema/index.js";import{Markdown as W}from"../Markdown/index.js";import{layoutAtom as V}from"../../jotai/app.js";import{globalStoreAtom as _}from"../../jotai/store.js";import{operationStore as x}from"../../jotai/operation.js";import{styled as g}from"../../styled-components.js";import{SchemaCatalogLink as $}from"../SchemaCatalogLink/SchemaCatalogLink.js";const F=(e,t)=>{if(!e)return{};const n={schema:{$ref:e}};return t&&(n.examples={example:{$ref:t}}),n};function K(e){if(!e?.description)return!1;const t=Array.isArray(e.type)?e.type:[e.type],n=t.includes("object")&&(e.fields?.length??0)>0,c=t.includes("array"),a=!!e.oneOf?.length,s=!!e.discriminatorProp;return n||c||a||s}function q({schemaRef:e,exampleRef:t,showReadOnly:n=!0,showWriteOnly:c=!1}){const a=O(V),{parser:s,options:l}=O(_),d=T(),o=C(()=>B(s,"json",!1,F(e,t),l,{operation:{pointer:"SchemaDefinitionComponent"}}),[t,l,s,e]),[f,h]=k(x(o.operation?.pointer)),v=j(m=>{if(!o.schema)return;const r=o.schema,y=r.oneOf;if(y&&o?.examples&&Object.keys(o?.examples).includes(m)){const p=y.findIndex(A=>A.title===m);p!==-1&&r&&d(x(r.operationPointer),{activeExampleName:r.oneOf?.[p]?.title,activeOneOf:{[r.pointer]:p}})}f.activeExampleName!==m&&h({activeExampleName:m})},[o?.examples,o.schema,f.activeExampleName,h,d]),u=a===D.STACKED;return i(E,{className:"schema-definition",children:S(P,{layout:a,children:[S(G,{isStacked:u,children:[e&&i($,{schemaRef:e}),o.schema?.description&&K(o.schema)&&i(z,{children:i(W,{source:o.schema.description})}),i(L,{skipWriteOnly:!c,skipReadOnly:!n,schema:o.schema,level:1})]}),i(N,{isStacked:u,children:i(w,{className:"panel-response-samples",children:i(I,{mediaType:o,onChange:v})})})]})})}const se=M(q),z=g.div`
  margin-bottom: var(--spacing-vertical);
`,G=g(b)`
  && {
    padding-left: 0;
    padding-right: 0;
  }
`;export{se as SchemaDefinition};
