import React__default,{useRef,useState,useInsertionEffect,useCallback,useEffect,useMemo,createContext}from"react";import PropTypes from"prop-types";import ReactDOM__default from"react-dom";import{c as css_248z,a as css_248z$1,t as tailwindTheme}from"./ibm-plex-BUIARG2h.js";const ShadowRoot=a=>{let{mode:b="open",delegatesFocus:c=!1,children:d=null}=a;const e=useRef(),[f,g]=useState();return React__default.useEffect(()=>{e.current&&g(e.current.attachShadow({delegatesFocus:c,mode:b}))},[]),React__default.createElement("div",{ref:e,"data-shadow-host":"true",style:{height:"100%"}},f&&ReactDOM__default.createPortal(d,f))};ShadowRoot.propTypes={mode:PropTypes.oneOf(["open","closed"]),delegatesFocus:PropTypes.bool,children:PropTypes.node};const STYLE_ID="juno-style-provider-golbal-fonts",Fonts=a=>{let{inline:b}=a;return useInsertionEffect(()=>{if(!b&&!document.querySelector(`[id="${STYLE_ID}"]`)){const a=document.createElement("style");a.setAttribute("id",STYLE_ID),a.innerHTML=css_248z.toString(),document.head.appendChild(a)}},[]),b?React__default.createElement("style",{[`data-${STYLE_ID}`]:""},css_248z.toString()):null};Fonts.propTypes={inline:PropTypes.bool};const styles=css_248z$1.toString(),GLOBAL_STYLE_ID="juno-style-provider-global-styles",GlobalStyles=a=>{let{inline:b}=a;return useInsertionEffect(()=>{if(!(b||document.querySelector(`[id="${GLOBAL_STYLE_ID}"]`))){const a=document.createElement("style");a.setAttribute("id",GLOBAL_STYLE_ID),a.appendChild(document.createTextNode(styles)),document.head.append(a)}},[]),b?React__default.createElement("style",{[`data-${GLOBAL_STYLE_ID}`]:""},styles):null};GlobalStyles.propTypes={inline:PropTypes.bool};function useLocalStorage(a,b){const[c,d]=useState(()=>{try{const c=window.localStorage.getItem(a);return c?JSON.parse(c):b}catch(a){return console.log("Juno Error: useLocalStorage error: ",a),b}});return[c,e=>{try{const b=e instanceof Function?e(c):e;d(b),window.localStorage.setItem(a,JSON.stringify(b))}catch(a){return console.log("Juno Error: useLocalStorage error: ",a),b}}]}const StylesContext=createContext(),APP_BODY_CSS_CLASS_NAME="juno-app-body",DEFAULT_THEME_NAME="theme-dark",StyleProvider=a=>{let{stylesWrapper:d="inline",theme:b,children:e=null,shadowRootMode:c}=a;const f=b||DEFAULT_THEME_NAME,[g,h]=useLocalStorage("juno-theme",f),i=useRef(g),j=useRef(APP_BODY_CSS_CLASS_NAME+" "+f),k=useRef(null),l=useCallback(a=>{let{children:b}=a;return"shadowRoot"===d?React__default.createElement(ShadowRoot,{mode:c},b):b},[d,c]),m=useCallback(a=>{k.current&&"string"==typeof a&&(k.current.classList.add(a),j.current=k.current.className)},[]),n=useCallback(a=>{k.current&&"string"==typeof a&&(k.current.classList.remove(a),j.current=k.current.className)},[]),o=useCallback(a=>{k.current&&"string"==typeof a&&(k.current.classList.remove(i.current),k.current.classList.add(a),i.current=a,j.current=k.current.className,h(a))},[k.current,i.current]);return useEffect(()=>{k.current&&o(b||DEFAULT_THEME_NAME)},[b]),useMemo(()=>React__default.createElement(l,null,React__default.createElement(Fonts,{inline:"head"!==d}),React__default.createElement(GlobalStyles,{inline:"head"!==d}),React__default.createElement(StylesContext.Provider,{value:{styles,theme:tailwindTheme,currentTheme:i.current,setThemeClass:o,addCssClass:m,removeCssClass:n}},React__default.createElement("div",{className:j.current,ref:k},e))),[d,e,c,j.current,o])};StyleProvider.propTypes={children:PropTypes.node,stylesWrapper:PropTypes.oneOf(["head","inline","shadowRoot"]),theme:PropTypes.string,shadowRootMode:PropTypes.oneOf(["open","closed"])},StyleProvider.useStyles=()=>React__default.useContext(StylesContext);export{ShadowRoot as S,StyleProvider as a};