import { getDatasetList, getModelList } from '@/services/dataset/index.js';
import { getComputingResourceReq } from '@/services/pipeline';
import { ComputingResource } from '@/types';
import { type SelectProps } from 'antd';

// 过滤资源规格
const filterResourceStandard: SelectProps<string, ComputingResource>['filterOption'] = (
  input: string,
  option?: ComputingResource,
) => {
  return (
    option?.computing_resource?.toLocaleLowerCase()?.includes(input.toLocaleLowerCase()) ?? false
  );
};

// id 从 number 转换为 string
const convertId = (item: any) => ({ ...item, id: String(item.id) });

export type SelectPropsConfig = {
  getOptions: () => Promise<any>; // 获取下拉数据
  fieldNames?: SelectProps['fieldNames']; // 下拉数据字段
  optionFilterProp?: SelectProps['optionFilterProp']; // 过滤字段名
  filterOption?: SelectProps['filterOption']; // 过滤函数
};

export const paramSelectConfig: Record<string, SelectPropsConfig> = {
  dataset: {
    getOptions: async () => {
      const res = await getDatasetList({
        page: 0,
        size: 1000,
        available_range: 0,
      });
      return res?.data?.content?.map(convertId) ?? [];
    },
    fieldNames: {
      label: 'name',
      value: 'id',
    },
    optionFilterProp: 'name',
  },
  model: {
    getOptions: async () => {
      const res = await getModelList({
        page: 0,
        size: 1000,
        available_range: 0,
      });
      return res?.data?.content?.map(convertId) ?? [];
    },
    fieldNames: {
      label: 'name',
      value: 'id',
    },
    optionFilterProp: 'name',
  },
  resource: {
    getOptions: async () => {
      const res = await getComputingResourceReq({
        page: 0,
        size: 1000,
        resource_type: '',
      });
      return res?.data?.content ?? [];
    },
    fieldNames: {
      label: 'description',
      value: 'standard',
    },
    filterOption: filterResourceStandard as SelectProps['filterOption'],
  },
};
