var __rest=this&&this.__rest||function(e,t){var o={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&t.indexOf(a)<0&&(o[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(a=Object.getOwnPropertySymbols(e);r<a.length;r++)t.indexOf(a[r])<0&&Object.prototype.propertyIsEnumerable.call(e,a[r])&&(o[a[r]]=e[a[r]])}return o};import{atom}from"jotai";import{atomWithStorage,createJSONStorage}from"jotai/utils";import{IS_BROWSER,LayoutVariant}from"@redocly/theme";import{getCookie,removeCookie,setCookie}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:{},userClaims:{}},customStorage={getItem:e=>{var t,o;const a=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),a)},setItem:(e,t)=>{const{environments:o}=t,a=__rest(t,["environments"]),r={environments:o};setCookie(e,JSON.stringify(a)),toSessionStorage(e,JSON.stringify(r))},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),a=mergeEnvData(t.environments,(null==o?void 0:o.environments)||{}),r=(null==o?void 0:o.environment)||t.environment,n=a[r]?r:Object.keys(a)[0];return[a[n]||{},n||""]}),((e,t,{environment:o,environments:a})=>{const r=e(appStore);t(appStore,Object.assign(Object.assign({},r),{environment:o||r.environment,environments:mergeEnvData(r.environments||{},a)}))}));export const isSidebarOpenedAtom=atom((e=>e(appStore).isSidebarOpened),((e,t,o)=>{const a=e(appStore);t(appStore,Object.assign(Object.assign({},a),{isSidebarOpened:o}))}));export const activeMimeNameAtom=atom((e=>e(appStore).activeMimeName),((e,t,o)=>{const a=e(appStore);t(appStore,Object.assign(Object.assign({},a),{activeMimeName:o}))}));export const layoutAtom=atom((e=>e(appStore).layout),((e,t,o=LayoutVariant.STACKED)=>{const a=e(appStore);t(appStore,Object.assign(Object.assign({},a),{layout:o}))}));export const userClaimsAtom=atom((e=>e(appStore).userClaims),((e,t,o)=>{const a=e(appStore);t(appStore,Object.assign(Object.assign({},a),{userClaims: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 a=e(appSessionStore);t(appSessionStore,Object.assign(Object.assign({},a),{collapsedSidebar:o}))}));export function getLangKey({lang:e,label:t}){return(t||e).toLowerCase()}export const languageAtom=atom((e=>{var t,o,a;const{codeSamples:r}=e(globalOptionsAtom),n=r.languages.map((({lang:e,label:t},o)=>({key:getLangKey({lang:e,label:t}),lang:e,title:t||e,order:o}))),s=e(appStore).language||(null===(t=n[0])||void 0===t?void 0:t.key);return{languages:n,activeLanguage:(null===(o=n.find((({key:e})=>e===s)))||void 0===o?void 0:o.key)||(null===(a=n[0])||void 0===a?void 0:a.key)}}),((e,t,o)=>{const a=e(appStore);t(appStore,Object.assign(Object.assign({},a),{language:o}))}));
//# sourceMappingURL=app.js.map