import CommonContent from "@/api/CommonContent";
import type { IHomeCommonCategoryListTabNestCategoryItemDefine } from "../CommonCategoryDefine";
import type { IHomeCommonCategoryDynamicDataCommonContent } from "../CommonCategoryDynamicData";

/**
 * 分类项动态数据自动合并类型:获取多个分类并替换（某个分类下的所有分类）
 */
export interface IHomeCommonCategoryCategoryDynamicDataMergeTypeGetColumns {
  type: 'speicalMergeItem:getColumns',
  params: {
    /**
     * 模型ID
     */
    modelId: number,
    /**
     * 栏目ID
     */
    mainBodyColumnId: number,
    /**
     * 如果设置了替换项，可以强制覆盖指定ID的某些字段
     */
    overrideItems: ({
      id: number,
    } & IHomeCommonCategoryListTabNestCategoryItemDefine)[],
    /**
     * 每个单元的接口其他参数
     */
    otherParams?: Record<string, any>,
  },
}
/**
 * 分类项动态数据自动合并类型:获取单个分类并替换
 */
export interface IHomeCommonCategoryCategoryDynamicDataMergeTypeGetColumn {
  type: 'speicalMergeItem:getColumn',
  params: {
    /**
     * 模型ID
     */
    modelId: number,
    /**
     * 栏目ID
     */ 
    mainBodyColumnId: number,
  },
}

export type IHomeCommonCategoryCategoryDynamicDataMerg = 
  | IHomeCommonCategoryCategoryDynamicDataMergeTypeGetColumns 
  | IHomeCommonCategoryCategoryDynamicDataMergeTypeGetColumn;

export async function doLoadDynamicCategoryDataMergeTypeGetColumns(
  categorys: IHomeCommonCategoryListTabNestCategoryItemDefine[],
) { 
  const newCategorys = new Array<IHomeCommonCategoryListTabNestCategoryItemDefine>(categorys.length);
  for (let i = categorys.length - 1; i >= 0; i--) {
    let category = categorys[i];
    newCategorys[i] = category;
    if (category.type?.startsWith('speicalMergeItem:')) {
      const d = category.type.split(':');
      const op = d[1];
      const type = d[2];
      switch (op) {
        case 'getColumns': {
          const data = category as unknown as IHomeCommonCategoryCategoryDynamicDataMergeTypeGetColumns;
          const result = await CommonContent.getModelColumList(
            data.params.modelId,
            data.params.mainBodyColumnId
          )
          const newArray = result.map((item) => {
            const overrideItem = data.params?.overrideItems?.find((i) => i.id === item.id) || {};
            return {
              ...category,
              params: undefined,
              text: item.name as string,
              type,
              dataSolve: category.dataSolve || [],
              ...overrideItem,
              data: {
                type: 'commonContent',
                params: {
                  modelId: item.modelId,
                  mainBodyColumnId: item.id,
                },
                otherParams: data.params.otherParams,
              } as IHomeCommonCategoryDynamicDataCommonContent,
            }
          });   
          newCategorys.splice(i, 1, ...newArray);
          break;
        }
        case 'getColumn': {
          const data = category as unknown as IHomeCommonCategoryCategoryDynamicDataMergeTypeGetColumn;
          const result = await CommonContent.getModelColumList(
            data.params.modelId,
          )
          newCategorys.splice(i, 1, ...result
            .filter((item) => item.id === data.params.mainBodyColumnId)
            .map((item) => ({
              ...category,
              text: item.name as string,
              type,
              data: {
                type: 'commonContent',
                params: {
                  modelId: item.modelId,
                  mainBodyColumnId: item.id,
                },
              } as IHomeCommonCategoryDynamicDataCommonContent,
          })));
          continue;
        }
      }
    }
  }
  return newCategorys;
}
