import { getComputingResourceReq } from '@/services/pipeline';
import { ComputingResource } from '@/types';
import { to } from '@/utils/promise';
import { type SelectProps } from 'antd';
import { useCallback, useEffect, useState } from 'react';

// 获取资源规格
export function useComputingResource() {
  const [resourceStandardList, setResourceStandardList] = useState<ComputingResource[]>([]);

  useEffect(() => {
    getComputingResource();
  }, []);

  // 获取资源规格列表数据
  const getComputingResource = useCallback(async () => {
    const params = {
      page: 0,
      size: 1000,
      resource_type: '',
    };
    const [res] = await to(getComputingResourceReq(params));
    if (res && res.data && res.data.content) {
      setResourceStandardList(res.data.content);
    }
  }, []);

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

  // 根据 standard 获取 description
  const getDescription = useCallback(
    (standard: string) => {
      return resourceStandardList.find((item) => item.standard === standard)?.description;
    },
    [resourceStandardList],
  );

  return [resourceStandardList, filterResourceStandard, getDescription] as const;
}
