import { ResourceSelectorResponse } from '@/components/ResourceSelectorModal';
import { ResourceInfoTabKeys } from '@/pages/Dataset/components/ResourceInfo';
import {
  DataSource,
  DatasetData,
  ModelData,
  ProjectDependency,
  TrainTask,
} from '@/pages/Dataset/config';
import { getGitUrl } from '@/utils';
// 格式化日期
export { formatDate } from '@/utils/date';

type SelectedCodeConfig = {
  code_path: string;
  branch: string;
  showValue?: string; // 前端使用的
  show_value?: string; // 后端使用的
};

// 格式化数据集数组
export const formatDatasets = (datasets?: DatasetData[]) => {
  if (!datasets || datasets.length === 0) {
    return undefined;
  }
  return datasets.map((item) => ({
    value: item.name,
    link: `/dataset/dataset/info/${item.id}?tab=${ResourceInfoTabKeys.Introduction}&version=${item.version}&name=${item.name}&owner=${item.owner}&identifier=${item.identifier}`,
  }));
};

// 格式化数据集
export const formatDataset = (dataset?: DatasetData) => {
  if (!dataset) {
    return undefined;
  }
  return {
    value: dataset.name,
    link: `/dataset/dataset/info/${dataset.id}?tab=${ResourceInfoTabKeys.Introduction}&version=${dataset.version}&name=${dataset.name}&owner=${dataset.owner}&identifier=${dataset.identifier}`,
  };
};

// 格式化模型
export const formatModel = (model: ModelData) => {
  if (!model) {
    return undefined;
  }
  return {
    value: model.name,
    link: `/dataset/model/info/${model.id}?tab=${ResourceInfoTabKeys.Introduction}&version=${model.version}&name=${model.name}&owner=${model.owner}&identifier=${model.identifier}`,
  };
};

// 格式化镜像
export const formatMirror = (mirror: ResourceSelectorResponse) => {
  if (!mirror) {
    return undefined;
  }
  return mirror.path;
};

// 格式化代码配置
export const formatCodeConfig = (project?: ProjectDependency | SelectedCodeConfig) => {
  if (!project) {
    return undefined;
  }
  // 创建表单，CodeSelect 组件返回，目前有流水线、模型部署、超参数自动寻优创建时选择了代码配置
  if ('code_path' in project) {
    const { showValue, show_value, code_path, branch } = project;
    return {
      value: showValue || show_value,
      url: getGitUrl(code_path, branch),
    };
  } else {
    // 数据集和模型的代码配置
    const { url, branch, name } = project;
    return {
      value: name,
      url: getGitUrl(url, branch),
    };
  }
};

// 格式化训练任务（实验实例）
export const formatTrainTask = (task?: TrainTask) => {
  if (!task) {
    return undefined;
  }
  return {
    value: task.name,
    url: `/pipeline/experiment/instance/${task.workflow_id}/${task.ins_id}`,
  };
};

// 格式化数据来源
export const formatSource = (source?: string) => {
  if (source === DataSource.Create) {
    return '用户上传';
  } else if (source === DataSource.HandExport) {
    return '手动导入';
  } else if (source === DataSource.AtuoExport) {
    return '实验自动导入';
  }
  return source;
};

// 格式化字符串数组，以逗号分隔
export const formatList = (value: string[] | null | undefined): string => {
  if (
    value === undefined ||
    value === null ||
    Array.isArray(value) === false ||
    value.length === 0
  ) {
    return '--';
  }
  return value.join(',');
};

// 格式化布尔值
export const formatBoolean = (value: boolean): string => {
  return value ? '是' : '否';
};

type FormatEnumFunc = (value: string | number) => string;

// 格式化枚举
export const formatEnum = (
  options: { value: string | number; label: string }[],
): FormatEnumFunc => {
  return (value: string | number) => {
    const option = options.find((item) => item.value === value);
    return option ? option.label : '--';
  };
};
