import { useAppConfig } from '@vben/hooks';
import { useAccessStore } from '@vben/stores';

const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
const accessStore = useAccessStore();
/**
 * 获取上传接口完整地址
 * @param path 上传接口路径，默认为 '/api/core/upload'
 * @returns 完整的上传接口地址
 */
export function getUploadAction(path = '/common/upload'): string {
  return `${apiURL}${path}`;
}

/**
 * 单张图片上传配置
 * @param action 上传接口路径，默认为 '/common/upload'
 * @returns 上传配置对象
 */
export function getSingleImageUploadConfig(action = '/common/upload') {
  return {
    accept: 'image/*',
    name: 'file',
    listType: 'picture-card' as const,
    multiple: false,
    maxCount: 1,
    limit: 1,
    action: getUploadAction(action),
    headers: {
      Authorization: `Bearer ${accessStore.accessToken}`,
    },
  };
}

/**
 * 多张图片上传配置
 * @param maxCount 最大上传数量
 * @param action 上传接口路径，默认为 '/api/core/upload'
 * @returns 上传配置对象
 */
export function getMultipleImageUploadConfig(
  maxCount: number,
  action = '/common/upload',
) {
  return {
    accept: 'image/*',
    listType: 'picture-card' as const,
    maxCount,
    name: 'file',
    action: getUploadAction(action),
    headers: {
      Authorization: `Bearer ${accessStore.accessToken}`,
    },
  };
}

/**
 * 多张图片上传配置
 * @param maxCount 最大上传数量
 * @param action 上传接口路径，默认为 '/api/core/upload'
 * @returns 上传配置对象
 */
export function getMultipleUploadConfig(
  maxCount: number,
  action = '/common/upload',
  accept = '*',
) {
  return {
    accept,
    listType: 'text' as const,
    maxCount,
    name: 'file',
    action: getUploadAction(action),
    headers: {
      Authorization: `Bearer ${accessStore.accessToken}`,
    },
  };
}

/**
 * 文件上传配置
 * @param accept 接受的文件类型
 * @param listType 列表类型
 * @param maxCount 最大上传数量
 * @param action 上传接口路径，默认为 '/api/core/upload'
 * @returns 上传配置对象
 */
export function getFileUploadConfig(
  accept: string,
  listType: 'picture' | 'picture-card' | 'text' = 'picture-card',
  maxCount?: number,
  action = '/common/upload',
) {
  const config: any = {
    accept,
    listType,
    name: 'file',
    action: getUploadAction(action),
    headers: {
      Authorization: `Bearer ${accessStore.accessToken}`,
    },
  };
  if (maxCount !== undefined) {
    config.maxCount = maxCount;
  }
  return config;
}
