var __rest=this&&this.__rest||function(e,t){var o={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(e);r<n.length;r++)t.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(e,n[r])&&(o[n[r]]=e[n[r]])}return o};import{atom}from"jotai";import{atomWithStorage,createJSONStorage}from"jotai/utils";import{LayoutVariant,IS_BROWSER}from"@redocly/theme";import{getCookie,setCookie,removeCookie}from"../utils/cookies";import{globalOptionsAtom}from"./store";import{mergeEnvData}from"../utils/environments";import{fromSessionStorage,safeJsonParse,toSessionStorage}from"../utils";const defaultAppStoreValue={isSidebarOpened:!0,activeMimeName:"",layout:LayoutVariant.THREE_PANEL,language:"",environment:"",environments:{}},customStorage={getItem:e=>{var t,o;const n=safeJsonParse(getCookie(e,null===(o=null===(t=globalThis.redoclyCookieStorage)||void 0===t?void 0:t.getStore)||void 0===o?void 0:o.call(t))),r=safeJsonParse(fromSessionStorage(e));return Object.assign(Object.assign(Object.assign({},defaultAppStoreValue),r),n)},setItem:(e,t)=>{const{environments:o,layout:n}=t,r=__rest(t,["environments","layout"]),a={environments:o};setCookie(e,JSON.stringify(r)),toSessionStorage(e,JSON.stringify(a))},removeItem:e=>{removeCookie(e),sessionStorage.removeItem(e)}};export const appStore=atomWithStorage("redoc.appStore",defaultAppStoreValue,customStorage);export const appStoreOverrideAtom=atom({});export const environmentsAtom=atom((e=>{const t=e(appStore),o=e(appStoreOverrideAtom);return mergeEnvData(t.environments,(null==o?void 0:o.environments)||{})}));export const environmentAtom=atom((e=>{const t=e(appStore),o=e(appStoreOverrideAtom),n=mergeEnvData(t.environments,(null==o?void 0:o.environments)||{}),r=(null==o?void 0:o.environment)||t.environment,a=n[r]?r:Object.keys(n)[0];return[n[a]||{},a||""]}),((e,t,{environment:o,environments:n})=>{const r=e(appStore);t(appStore,Object.assign(Object.assign({},r),{environment:o||r.environment,environments:mergeEnvData(r.environments||{},n)}))}));export const isSidebarOpenedAtom=atom((e=>e(appStore).isSidebarOpened),((e,t,o)=>{const n=e(appStore);t(appStore,Object.assign(Object.assign({},n),{isSidebarOpened:o}))}));export const activeMimeNameAtom=atom((e=>e(appStore).activeMimeName),((e,t,o)=>{const n=e(appStore);t(appStore,Object.assign(Object.assign({},n),{activeMimeName:o}))}));export const layoutAtom=atom((e=>e(appStore).layout),((e,t,o=LayoutVariant.STACKED)=>{const n=e(appStore);t(appStore,Object.assign(Object.assign({},n),{layout:o}))}));export const appSessionStore=atomWithStorage("redoc.appSessionStore",{collapsedSidebar:!1,unsupportedFeatureScreen:!1},IS_BROWSER?createJSONStorage((()=>sessionStorage)):void 0);export const collapsedSidebarAtom=atom((e=>e(appSessionStore).collapsedSidebar),((e,t,o)=>{const n=e(appSessionStore);t(appSessionStore,Object.assign(Object.assign({},n),{collapsedSidebar:o}))}));export function getLangKey({lang:e,label:t}){return(t||e).toLowerCase()}export const languageAtom=atom((e=>{var t;const{codeSamples:o}=e(globalOptionsAtom),n=o.languages.map((({lang:e,label:t},o)=>({key:getLangKey({lang:e,label:t}),lang:e,title:t||e,order:o}))),r=e(appStore).language||n[0].key;return{languages:n,activeLanguage:(null===(t=n.find((({key:e})=>e===r)))||void 0===t?void 0:t.key)||n[0].key}}),((e,t,o)=>{const n=e(appStore);t(appStore,Object.assign(Object.assign({},n),{language:o}))}));export const unsupportedFeatureScreenAtom=atom((e=>e(appSessionStore).unsupportedFeatureScreen),((e,t,o)=>{const n=e(appSessionStore);t(appSessionStore,Object.assign(Object.assign({},n),{unsupportedFeatureScreen:o}))}));
//# sourceMappingURL=app.js.map