import { parseJsonText } from './index';

export default class SessionStorage {
  /** 用于新建镜像 */
  static readonly mirrorNameKey = 'mirror-name';
  /** 模型部署服务版本 */
  static readonly serviceVersionInfoKey = 'service-version-info';
  /** 编辑器 url */
  static readonly editorUrlKey = 'editor-url';
  /** 客户端信息 */
  static readonly clientInfoKey = 'client-info';
  /** aim url */
  static readonly aimUrlKey = 'aim-url';
  /** tensorBoard url */
  static readonly tensorBoardUrlKey = 'tensor-board-url';

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

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

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