/**
 * KRA - Params Utility
 * Get system parameters
 */
import { getSysParam } from '@/services/kratos/params';

// 参数缓存
const paramsCache: Record<string, any> = {};

/**
 * 获取参数方法
 * @param key 参数key
 * @returns 参数值
 */
export const getParams = async (key: string): Promise<any> => {
  if (paramsCache[key]) {
    return paramsCache[key];
  }
  try {
    const res = await getSysParam({ key });
    if (res.code === 0 && res.data?.sysParams) {
      paramsCache[key] = res.data.sysParams.value;
      return paramsCache[key];
    }
  } catch (error) {
    console.error('Failed to get param:', key, error);
  }
  return null;
};

/**
 * 清除参数缓存
 */
export const clearParamsCache = () => {
  Object.keys(paramsCache).forEach((key) => {
    delete paramsCache[key];
  });
};

export default {
  getParams,
  clearParamsCache,
};
