import {StorageKeys} from "../../globalConstants";

/**
 * 判断窗口是否为黑色主题
 */
/* istanbul ignore next line */
export const isSystemDark = window?.matchMedia
  ? window.matchMedia('(prefers-color-scheme: dark)')?.matches
  : undefined;

/**
 * 将主题变量存入浏览器缓存
 * @param theme
 */
export function saveTheme(theme) {
  window.localStorage && localStorage.setItem(StorageKeys.Theme, theme);
}


/**
 * 获取本地缓存中的主题变量
 */
/* istanbul ignore next line */
export function getThemeFromStorage() {
  let theme = 'light';
  try {
    const storedTheme =
      window.localStorage && localStorage.getItem(StorageKeys.Theme);
    if (storedTheme) {
      theme = storedTheme;
    }
  } catch (error) {
    throw error;
  }
  return theme;
}
