import Taro from "@tarojs/taro";

export enum EStorage {
  userInfo = "USER_INFO",
  tabMenu = "TAB_MENU",
  channelCode = "CHANNEL_CODE",
}

export default class StorageUtil {
  public static set(key: EStorage, value: any) {
    if (typeof value !== "string") {
      try {
        value = JSON.stringify(value);
      } catch (error) {}
    }
    Taro.setStorageSync(key, value);
  }

  public static get<T>(key: EStorage): T {
    let value = Taro.getStorageSync(key);
    try {
      value = value && JSON.parse(value);
    } catch (error) {}
    return value;
  }

  public static remove(key: EStorage): any {
    return Taro.removeStorageSync(key);
  }
}
