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