var __awaiter=this&&this.__awaiter||function(e,t,o,i){return new(o||(o=Promise))((function(n,r){function a(e){try{l(i.next(e))}catch(e){r(e)}}function s(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?n(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(a,s)}l((i=i.apply(e,t||[])).next())}))};import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";import{cloneElement,isValidElement,memo,useCallback,useEffect,useMemo,useState}from"react";import{Tag}from"@markdoc/markdoc";import{Loading,markdoc}from"@redocly/theme";import{createStore,Provider}from"jotai";import{normalizeOptions,OpenAPIParser}from"../../services";import{getMarkdownHeaderId,loadSingle,useDeepCompareMemoize}from"./utils";import{argValueToBoolean,normalizeServers}from"../../utils";import{SchemaDefinition}from"../SchemaDefinition";import{appStore,appStoreOverrideAtom,environmentAtom,layoutAtom}from"../../jotai/app";import JotaiNexus from"../../jotai/nexus";import{mapObject}from"../../utils/object";import{mergeInMockServer}from"../../models/operation";import{getDefaultOperationStore,operationStore}from"../../jotai/operation";import{getServerEnvName}from"../../utils/environments";import{globalStoreAtom}from"../../jotai/store";export const AppProvider=({options:e,definitionUrl:t,definition:o,activeSampleLanguage:i,children:n})=>{const[,r]=useState(),[a,s]=useState(!0),[l,m]=useState(null);useEffect((()=>{!function(){__awaiter(this,void 0,void 0,(function*(){s(!0);try{m(yield loadSingle(o,t,e))}catch(e){r((()=>{throw e}))}}))}()}),[o,t,e]);const u=useMemo((()=>{var o,n,r;if(l)return(null===(o=null==e?void 0:e.unstable_hooks)||void 0===o?void 0:o.onInit)&&(null===(r=null===(n=e.unstable_hooks)||void 0===n?void 0:n.onInit)||void 0===r||r.call(n,{store:{definition:l,options:e,definitionUrl:t,activeSampleLanguage:i}})),{definition:l,options:e,definitionUrl:t,activeSampleLanguage:i}}),useDeepCompareMemoize([l,e,t]));return useEffect((()=>{u&&s(!1)}),[u]),u?a?argValueToBoolean(null==e?void 0:e.hideLoading,!1)?null:_jsx(Loading,{color:"--loading-spinner-color"}):isValidElement(n)?cloneElement(n,{store:u}):null:null};const DEFAULT_OPTIONS={ignoreNamedSchemas:["java.io.ObjectStreamField"],maxDisplayedEnumValues:10,markdocOptions:{tags:{schemaDefinition:{render:"SchemaDefinition",attributes:{schemaRef:{type:String},exampleRef:{type:String},showReadOnly:{type:Boolean},showWriteOnly:{type:Boolean},htmlWrap:{type:String,default:!1}}}},nodes:{heading:{children:["inline"],attributes:{id:{type:String},level:{type:Number,required:!0,default:1}},transform(e,t){const o=e.transformAttributes(t),i=e.transformChildren(t),n="string"==typeof o.id?o.id:getMarkdownHeaderId(i);return new Tag("Heading",Object.assign(Object.assign({},o),{id:n,level:e.attributes.level}),i)}}},components:{SchemaDefinition:SchemaDefinition,Heading:markdoc.components.Heading}}};export const StoreProvider=memo((({children:e,options:t,definitionUrl:o,definition:i,withState:n})=>{const r=useCallback((()=>{const e=normalizeOptions(t,DEFAULT_OPTIONS);return{options:e,parser:new OpenAPIParser(i,o,e)}}),[i,o,t]),a=useMemo((()=>{var e;const t=createStore(),o=r();t.set(globalStoreAtom,o);const i=normalizeServers(o.parser.definitionUrl,mergeInMockServer((null===(e=o.parser.definition)||void 0===e?void 0:e.servers)||[],o.options.mockServer)),a=Object.fromEntries(i.map((e=>[getServerEnvName(e),Object.assign({server:e.url},mapObject(e.variables||{},(e=>e.default||"")))])));return t.sub(appStore,(()=>{})),t.set(environmentAtom,{environments:a}),n&&t.set(appStoreOverrideAtom,{environment:n.environment,environments:n.environments,activeMimeName:n.activeMimeName}),(null==n?void 0:n.operation)&&t.set(operationStore(n.operation.pointer),getDefaultOperationStore(n.operation.pointer,n.operation.state.requestValues)),t}),[r,n]);return useEffect((()=>{(null==n?void 0:n.layout)&&a.set(layoutAtom,null==n?void 0:n.layout)}),[a,null==n?void 0:n.layout]),_jsxs(Provider,{store:a,children:[_jsx(JotaiNexus,{}),e]})}));
//# sourceMappingURL=Providers.js.map