import type { GlobalConfig } from '@/share/types';

export function getEnumOptions<T extends string | number, M extends Record<T, string>>(enumObj: Record<string, T>, nameMap: M) {
  return Object.entries(enumObj)
    .filter(([_key, value]) => !isNaN(Number(value)) || typeof value === 'string')  // 保留数字和字符串类型的值
    .filter(([key, _value]) => isNaN(Number(key)))  // 过滤掉数字键（枚举的反向映射）
    .map(([_key, value]) => ({
      label: nameMap[value as T],
      value: value
    }));
}

/**
 * 获取全局配置项 (严格类型版本)
 * @param key 配置键名
 * @returns 配置值或undefined
 */
export function getGlobalConfig<T extends keyof GlobalConfig>(key: T): GlobalConfig[T] | undefined {
  return (window as typeof window & { CONFIG?: GlobalConfig }).CONFIG?.[key];
}

/**
 * 验证URL格式
 * @param url 待验证URL
 * @returns 验证结果
 */
export const validateUrl = (url: string): boolean => {
  try {
    new URL(url);
    return true;
  } catch {
    return false;
  }
};

/**
 * 验证Authorization头格式
 * @param auth 待验证字符串
 * @returns 验证结果
 */
export const validateAuthHeader = (auth: string): boolean => {
  return /^Basic [A-Za-z0-9+/]+={0,2}$/.test(auth);
};
