"use strict";var __awaiter=this&&this.__awaiter||function(e,t,i,r){return new(i||(i=Promise))((function(n,o){function a(e){try{l(r.next(e))}catch(e){o(e)}}function s(e){try{l(r.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?n(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(a,s)}l((r=r.apply(e,t||[])).next())}))},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.StoreProvider=exports.AppProvider=void 0;const jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),markdoc_1=require("@markdoc/markdoc"),theme_1=require("@redocly/theme"),jotai_1=require("jotai"),services_1=require("../../services"),utils_1=require("./utils"),utils_2=require("../../utils"),SchemaDefinition_1=require("../SchemaDefinition"),app_1=require("../../jotai/app"),nexus_1=__importDefault(require("../../jotai/nexus")),object_1=require("../../utils/object"),operation_1=require("../../models/operation"),operation_2=require("../../jotai/operation"),environments_1=require("../../utils/environments"),store_1=require("../../jotai/store"),AppProvider=({options:e,definitionUrl:t,definition:i,activeSampleLanguage:r,children:n})=>{const[,o]=(0,react_1.useState)(),[a,s]=(0,react_1.useState)(!0),[l,u]=(0,react_1.useState)(null);(0,react_1.useEffect)((()=>{!function(){__awaiter(this,void 0,void 0,(function*(){s(!0);try{u(yield(0,utils_1.loadSingle)(i,t,e))}catch(e){o((()=>{throw e}))}}))}()}),[i,t,e]);const c=(0,react_1.useMemo)((()=>{var i,n,o;if(l)return(null===(i=null==e?void 0:e.unstable_hooks)||void 0===i?void 0:i.onInit)&&(null===(o=null===(n=e.unstable_hooks)||void 0===n?void 0:n.onInit)||void 0===o||o.call(n,{store:{definition:l,options:e,definitionUrl:t,activeSampleLanguage:r}})),{definition:l,options:e,definitionUrl:t,activeSampleLanguage:r}}),(0,utils_1.useDeepCompareMemoize)([l,e,t]));return(0,react_1.useEffect)((()=>{c&&s(!1)}),[c]),c?a?(0,utils_2.argValueToBoolean)(null==e?void 0:e.hideLoading,!1)?null:(0,jsx_runtime_1.jsx)(theme_1.Loading,{color:"--loading-spinner-color"}):(0,react_1.isValidElement)(n)?(0,react_1.cloneElement)(n,{store:c}):null:null};exports.AppProvider=AppProvider;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 i=e.transformAttributes(t),r=e.transformChildren(t),n="string"==typeof i.id?i.id:(0,utils_1.getMarkdownHeaderId)(r);return new markdoc_1.Tag("Heading",Object.assign(Object.assign({},i),{id:n,level:e.attributes.level}),r)}}},components:{SchemaDefinition:SchemaDefinition_1.SchemaDefinition,Heading:theme_1.markdoc.components.Heading}}};exports.StoreProvider=(0,react_1.memo)((({children:e,options:t,definitionUrl:i,definition:r,withState:n})=>{const o=(0,react_1.useCallback)((()=>{const e=(0,services_1.normalizeOptions)(t,DEFAULT_OPTIONS);return{options:e,parser:new services_1.OpenAPIParser(r,i,e)}}),[r,i,t]),a=(0,react_1.useMemo)((()=>{var e;const t=(0,jotai_1.createStore)(),i=o();t.set(store_1.globalStoreAtom,i);const r=(0,utils_2.normalizeServers)(i.parser.definitionUrl,(0,operation_1.mergeInMockServer)((null===(e=i.parser.definition)||void 0===e?void 0:e.servers)||[],i.options.mockServer)),a=Object.fromEntries(r.map((e=>[(0,environments_1.getServerEnvName)(e),Object.assign({server:e.url},(0,object_1.mapObject)(e.variables||{},(e=>e.default||"")))])));return t.sub(app_1.appStore,(()=>{})),t.set(app_1.environmentAtom,{environments:a}),n&&t.set(app_1.appStoreOverrideAtom,{environment:n.environment,environments:n.environments,activeMimeName:n.activeMimeName}),(null==n?void 0:n.operation)&&t.set((0,operation_2.operationStore)(n.operation.pointer),(0,operation_2.getDefaultOperationStore)(n.operation.pointer,n.operation.state.requestValues)),t}),[o,n]);return(0,react_1.useEffect)((()=>{(null==n?void 0:n.layout)&&a.set(app_1.layoutAtom,null==n?void 0:n.layout)}),[a,null==n?void 0:n.layout]),(0,jsx_runtime_1.jsxs)(jotai_1.Provider,{store:a,children:[(0,jsx_runtime_1.jsx)(nexus_1.default,{}),e]})}));
//# sourceMappingURL=Providers.js.map