import { filterResourceStandard, resourceFieldNames } from '@/hooks/useComputingResource';
import { ServiceData } from '@/pages/ModelDeployment/types';
import { getDatasetList, getModelList } from '@/services/dataset/index.js';
import { getServiceListReq } from '@/services/modelDeployment';
import { type SelectProps } from 'antd';
import { pick } from 'lodash';

// id 从 number 转换为 string
const convertId = (item: any) => ({
  ...item,
  id: JSON.stringify({
    id: `${item.id}`,
    name: item.name,
    identifier: item.identifier,
    owner: item.owner,
  }),
});

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,
        is_public: false,
      });
      return res?.data?.content?.map(convertId) ?? [];
    },
    fieldNames: {
      label: 'name',
      value: 'id',
    },
    optionFilterProp: 'name',
  },
  model: {
    getOptions: async () => {
      const res = await getModelList({
        page: 0,
        size: 1000,
        is_public: false,
      });
      return res?.data?.content?.map(convertId) ?? [];
    },
    fieldNames: {
      label: 'name',
      value: 'id',
    },
    optionFilterProp: 'name',
  },
  service: {
    getOptions: async () => {
      const res = await getServiceListReq({
        page: 0,
        size: 1000,
      });
      return (
        res?.data?.content?.map((item: ServiceData) => ({
          label: item.service_name,
          value: JSON.stringify(pick(item, ['id', 'service_name'])),
        })) ?? []
      );
    },
    fieldNames: {
      label: 'label',
      value: 'value',
    },
    optionFilterProp: 'label',
  },
  resource: {
    getOptions: async () => {
      // 不需要这个函数
      return [];
    },
    fieldNames: resourceFieldNames,
    filterOption: filterResourceStandard as SelectProps['filterOption'],
  },
};
