// 下拉列表动态数据接口定义

import CommonContent from "@/api/CommonContent";
import type { IHomeCommonCategoryListTabListDropdownDefine } from "../CommonCategoryDefine";

export function CommonCategorDynamicDropDownValuesToParams(dropDownValues: (number|string|boolean)[], dropdownDefines: IHomeCommonCategoryListTabListDropdownDefine[]) {
  const params: Record<string, any> = {};
  dropdownDefines.forEach((define, index) => {
    const v = dropDownValues[index]
    if (typeof v === 'number' && v > 0)
      params[define.key] = v;
    else if (typeof v === 'string')
      params[define.key] = v;
  });
  return params;
}

/**
 * 下拉列表动态数据接口定义 - 通用内容
 */
export interface IHomeCommonCategoryDropdownDynamicDataCommonContent {
  type: 'commonContent',
  /**
   * 分类类型id
   */
  typeId: number,
  /**
   * 其他参数
   */
  otherParams?: Record<string, any>,
  /**
   * 通用内容数据id键
   */
  idKey?: string,
  /**
   * 通用内容数据名称键
   */
  nameKey?: string,
}
/**
 * 下拉列表动态数据接口定义 - 静态内容
 */
export interface IHomeCommonCategoryDropdownStaticData {
  type: 'static',
  /**
   * 静态数据
   */
  data: Record<string, any>[],
  /**
   * 静态数据id键
   */
  idKey?: string,
  /**
   * 静态数据名称键
   */
  nameKey?: string,
}
/**
 * 下拉列表动态数据接口定义 - 请求内容
 */
export interface IHomeCommonCategoryDropdownDynamicDataRequest {
  type: 'request',
  method: "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE",
  url: string,
  querys?: Record<string, any>,
  params?: Record<string, any>,
  idKey?: string,
  nameKey?: string,
}

export type IHomeCommonCategoryDropdownDynamicData = 
  IHomeCommonCategoryDropdownDynamicDataCommonContent 
  | IHomeCommonCategoryDropdownDynamicDataRequest
  | IHomeCommonCategoryDropdownStaticData;

export async function doLoadDynamicDropdownData(item: IHomeCommonCategoryDropdownDynamicData) {
  switch (item.type) {
    case 'commonContent':
      return (await CommonContent.getCategoryList(item.typeId)) as unknown as Record<string, any>[];
    case 'static':
      return item.data;
    case 'request':
      return (await CommonContent.request(
        item.url, 
        { ...item.querys }, 
        {
          method: item.method, 
          data: item.params,
        },
        '',
        undefined,
      )).data as unknown as Record<string, any>[];
  }
  throw new Error(`未实现的动态数据接口`);
}