import type { ApiConfig } from './types';

import { DictionaryApi, EnumApi, QueryApi } from '#/api';
import { requestClient } from '#/api/request';

export const createApiFunction = (apiConfig: ApiConfig) => {
  const api: any =
    typeof apiConfig.url === 'string' || !apiConfig.url
      ? (params: any) => {
          switch (apiConfig.type) {
            case 'api': {
              return QueryApi.postOptions(params);
            }
            case 'dict': {
              return DictionaryApi.getItemList(params);
            }
            case 'enum': {
              return EnumApi.getList(params);
            }
            default: {
              return (requestClient as any)[apiConfig.method](
                apiConfig.url as any,
                apiConfig.method === 'get' ? { params } : params,
              );
            }
          }
        }
      : apiConfig.url;
  return api;
};
