import{atom as a}from"jotai";import{atomWithStorage as l,createJSONStorage as g}from"jotai/utils";import{LayoutVariant as v}from"@redocly/config";import{getCookie as f,removeCookie as A,setCookie as y}from"../utils/cookies.js";import{globalOptionsAtom as k}from"./store.js";import{mergeEnvData as m}from"../utils/environments.js";import{fromSessionStorage as x,safeJsonParse as u,toSessionStorage as E}from"../utils/index.js";import{IS_BROWSER as O}from"../utils/dom.js";const S={isSidebarOpened:!0,activeMimeName:"",layout:v.THREE_PANEL,language:"",environment:"",environments:{},userClaims:{},allowedEnvironments:null},C={getItem:e=>{const t=u(f(e,globalThis.redoclyCookieStorage?.getStore?.())),o=u(x(e));return{...S,...o,...t}},setItem:(e,t)=>{const{environments:o,...n}=t,r={environments:o};y(e,JSON.stringify(n)),E(e,JSON.stringify(r))},removeItem:e=>{A(e),sessionStorage.removeItem(e)}},s=l("redoc.appStore",S,C),d=a({}),T=a(e=>{const t=e(s),o=e(d);return m(t.environments,o?.environments||{})}),h=a(e=>{const t=e(s),o=e(d),n=m(t.environments,o?.environments||{}),r=o?.environment||t.environment,i=n[r]?r:Object.keys(n)[0];return[n[i]||{},i||""]},(e,t,{environment:o,environments:n})=>{const r=e(s);t(s,{...r,environment:o||r.environment,environments:m(r.environments||{},n)})}),D=a(e=>e(s).isSidebarOpened,(e,t,o)=>{const n=e(s);t(s,{...n,isSidebarOpened:o})}),K=a(e=>e(s).activeMimeName,(e,t,o)=>{const n=e(s);t(s,{...n,activeMimeName:o})}),P=a(e=>e(s).layout,(e,t,o=v.STACKED)=>{const n=e(s);t(s,{...n,layout:o})}),W=a(e=>e(s).userClaims,(e,t,o)=>{const n=e(s);t(s,{...n,userClaims:o})}),_=a(e=>e(s).allowedEnvironments,(e,t,o)=>{const n=e(s);t(s,{...n,allowedEnvironments:o})}),c=l("redoc.appSessionStore",{collapsedSidebar:!1,unsupportedFeatureScreen:!1},O?g(()=>sessionStorage):void 0),j=a(e=>e(c).collapsedSidebar,(e,t,o)=>{const n=e(c);t(c,{...n,collapsedSidebar:o})});function b({lang:e,label:t}){return(t||e).toLowerCase()}const B=a(e=>{const{codeSamples:t}=e(k),o=t.languages.map(({lang:r,label:i},p)=>({key:b({lang:r,label:i}),lang:r,title:i||r,order:p})),n=e(s).language||o[0]?.key;return{languages:o,activeLanguage:o.find(({key:r})=>r===n)?.key||o[0]?.key}},(e,t,o)=>{const n=e(s);t(s,{...n,language:o})});export{K as activeMimeNameAtom,_ as allowedEnvironmentsAtom,c as appSessionStore,s as appStore,d as appStoreOverrideAtom,j as collapsedSidebarAtom,h as environmentAtom,T as environmentsAtom,b as getLangKey,D as isSidebarOpenedAtom,B as languageAtom,P as layoutAtom,W as userClaimsAtom};
