export enum STORAGE_TYPE {
  token = 'token',
  projectName = 'projectName', //项目名称
  platform = 'platform', // 0 建设平台  1 运营平台
}
const useStorage = (storage: Storage) => {
  const setItem = <T>(key: STORAGE_TYPE, value: T) => {
    storage.setItem(key, value ? JSON.stringify(value) : '');
  };

  const getItem = (key: STORAGE_TYPE) => {
    let value: string = storage.getItem(key) || '';
    try {
      value = JSON.parse(value);
      return value;
    } catch {
      return value;
    }
  };

  const removeItem = (key: STORAGE_TYPE) => {
    storage.removeItem(key);
  };

  const clearAll = () => {
    for (const key in storage) {
      if (key) {
        storage.removeItem(key);
      }
    }
  };
  return {
    setItem,
    getItem,
    removeItem,
    clearAll,
  };
};
const SessionService = useStorage(window.sessionStorage || sessionStorage);
const LocalService = useStorage(window.localStorage || sessionStorage);

export { SessionService, LocalService };
