import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";import{memo,useCallback,useMemo}from"react";import styled from"styled-components";import{LayoutVariant}from"@redocly/theme";import{useAtom,useAtomValue}from"jotai";import{useWriteAtom}from"../../jotai/use-write-atom";import{SamplesMiddlePanel,SamplesPanel,Row,Section,CodeBlockPanel}from"../common";import{getMediaType}from"../../models";import{MediaTypeSamples}from"../PayloadSamples";import{Schema}from"../Schema";import{Markdown}from"../Markdown";import{layoutAtom}from"../../jotai/app";import{globalStoreAtom}from"../../jotai/store";import{operationStore}from"../../jotai/operation";const getMediaTypeInfo=(e,o)=>{if(!e)return{};const t={schema:{$ref:e}};return o&&(t.examples={example:{$ref:o}}),t};function SchemaDefinitionComponent({schemaRef:e,exampleRef:o,showReadOnly:t=!0,showWriteOnly:a=!1}){var i,m,n;const r=useAtomValue(layoutAtom),{parser:s,options:l}=useAtomValue(globalStoreAtom),p=useWriteAtom(),c=useMemo((()=>getMediaType(s,"json",!1,getMediaTypeInfo(e,o),l,{operation:{pointer:"SchemaDefinitionComponent"}})),[o,l,s,e]),[d,u]=useAtom(operationStore(null===(i=c.operation)||void 0===i?void 0:i.pointer)),f=useCallback((e=>{var o,t;if(!c.schema)return;const a=c.schema,i=a.oneOf;if(i&&(null==c?void 0:c.examples)&&Object.keys(null==c?void 0:c.examples).includes(e)){const m=i.findIndex((o=>o.title===e));-1!==m&&a&&p(operationStore(a.operationPointer),{activeExampleName:null===(t=null===(o=a.oneOf)||void 0===o?void 0:o[m])||void 0===t?void 0:t.title,activeOneOf:{[a.pointer]:m}})}d.activeExampleName!==e&&u({activeExampleName:e})}),[null==c?void 0:c.examples,c.schema,d.activeExampleName,u,p]),h=r===LayoutVariant.STACKED;return _jsx(Section,{children:_jsxs(Row,{layout:r,children:[_jsxs(SamplesMiddlePanel,{isStacked:h,children:[(null===(m=c.schema)||void 0===m?void 0:m.description)&&_jsx(Description,{children:_jsx(Markdown,{source:null===(n=c.schema)||void 0===n?void 0:n.description})}),_jsx(Schema,{skipWriteOnly:!a,skipReadOnly:!t,schema:c.schema,level:1})]}),_jsx(SamplesPanel,{isStacked:h,children:_jsx(CodeBlockPanel,{className:"panel-response-samples",children:_jsx(MediaTypeSamples,{mediaType:c,onChange:f})})})]})})}export const SchemaDefinition=memo(SchemaDefinitionComponent);const Description=styled.div`
  margin-bottom: var(--spacing-vertical);
`;
//# sourceMappingURL=SchemaDefinition.js.map