import { StorageManager } from '@vben-core/shared/cache';

type StorageType = 'localStorage' | 'sessionStorage';

/**
 * 用于生成带前缀的存储键
 * @param key 存储键
 * @returns 带前缀的存储键
 */
export const useStorage = (
  key: string,
  options?: {
    prefix?: string;
    storageType?: StorageType;
  },
) => {
  // 实例化
  const storage = new StorageManager(options);

  const env = import.meta.env.PROD ? 'prod' : 'dev';
  const appVersion = import.meta.env.VITE_APP_VERSION;
  const appNamespace = import.meta.env.VITE_APP_NAMESPACE;

  const keyPrefix = `${appNamespace}-${appVersion}-${env}-${key}`;
  return {
    getItem: () => storage.getItem(`${keyPrefix}`),
    setItem: (value: any) => storage.setItem(`${keyPrefix}`, value),
    removeItem: () => storage.removeItem(`${keyPrefix}-${key}`),
    clear: () => storage.clear(),
  };
};
