import { parseJsonText } from './index';

export default class LocalStorage {
  // 登录的用户，包括用户名、密码和版本号
  static readonly loginUserKey = 'login-user';
  // 记住密码
  static readonly rememberPasswordKey = 'login-remember-password';

  /**
   * 获取 LocalStorage 值
   * @param key - LocalStorage key
   * @param isObject - 是不是对象
   */
  static getItem(key: string, isObject: boolean = false) {
    const jsonStr = localStorage.getItem(key);
    if (!isObject) {
      return jsonStr;
    }
    if (jsonStr) {
      return parseJsonText(jsonStr);
    }
    return null;
  }

  /**
   * 设置 LocalStorage 值
   * @param key - LocalStorage key
   * @param state - SessionStorage state
   * @param isObject - 是不是对象
   */
  static setItem(key: string, state?: any, isObject: boolean = false) {
    if (state) {
      localStorage.setItem(key, isObject ? JSON.stringify(state) : state);
    }
  }

  /**
   * 移除 LocalStorage 值
   * @param key - LocalStorage key
   */
  static removeItem(key: string) {
    localStorage.removeItem(key);
  }
}
