import { GetContentListItem, GetContentListParams } from "@/api/CommonContent";
import { navTo } from "@/components/utils/PageAction";
import { type IHomeCommonCategoryListTabListDataSolve } from "../data/CommonCategoryDefine";
import { DateUtils } from "@imengyu/imengyu-utils";
import { getDynamicScript, isDynamicScript } from "../data/CommonCategoryScript";

/**
 * 通用内容首页小列表控制代码组合
 */

export function navCommonDetail(p: {
  id: number,
  title?: string,
  mainBodyColumnId?: string|number|number[],
  modelId?: number,
}) {
  navTo('/pages/article/details', {
    mainBodyColumnId: p.mainBodyColumnId,
    modelId: p.modelId,
    id: p.id,
  }) 
}
export function navCommonList(p: {
  title?: string,
  mainBodyColumnId?: string|number|number[],
  modelId?: number,
  itemType?: string,
  dataSolve?: IHomeCommonCategoryListTabListDataSolve[],
  detailsPage? : string|[string, Record<string, any>],
}) {
  navTo('/pages/article/common/list', {
    title: p.title,
    mainBodyColumnId: typeof p.mainBodyColumnId == 'object' ? 
      p.mainBodyColumnId.join(',') : 
      p.mainBodyColumnId,
    modelId: p.modelId,
    itemType: p.itemType || 'article-common',
    detailsPage: p.detailsPage || '/pages/article/details',
    dataSolve: (p.dataSolve || []).join(','),
  }) 
}


export function navigateToAutoContent(dataItem: GetContentListItem, params: any) {
  navTo(resolveCommonContentGetPageDetailUrlAuto(dataItem), {
    id: dataItem.id,
    mainBodyColumnId: dataItem.mainBodyColumnId,
    modelId: dataItem.modelId,
    ...params,
  });
}

export function resolveCommonContentFormData(item: GetContentListItem[]) {
  item.forEach(it => {
    it.bottomTags = it.keywords?.length ? it.keywords as string[] : [ it.mainBodyColumnName ];
  })
  return item;
}
export function resolveCommonContentGetPageDetailUrlAuto(item: GetContentListItem) {
  if (item.type === GetContentListParams.TYPE_VIDEO || item.video)
    return '/pages/video/details';
  return '/pages/article/details';
}

const resolveCommonContentData = {
  'none': (item: any[]) => item,
  'common': (item: any[]) => {
    item.forEach(it => {
      it.bottomTags = it.bottomTags || [];
      it.bottomTags = (it.bottomTags as string[]).concat(it.keywords?.length ? it.keywords as string[] : [ it.mainBodyColumnName ]);
    })
    return item;
  },
  'date': (item: any[]) => {
    item.forEach(p => {
      p.desc = DateUtils.formatDate(p.publishAt, 'YYYY-MM-dd') + (p.desc ? '\n' : '') + (p.desc || '');
    })
    return item;
  },
  'form': (item: any[]) => {
    item.forEach(p => {
      p.desc = `来源：${p.from || '暂无'}` + ' ' + (p.desc || '');
    })
    return item;
  },
  'ich': (item: any[]) => {
    item.forEach(it => {
      it.bottomTags = (it.bottomTags as string[] || []).concat([
        it.levelText as string, 
        it.ichTypeText as string, 
        it.batchText as string,
        it.regionText as string,
      ]);
    });
    return item;
  },
  'inheritor': (item: any[]) => {
    item.forEach(it => {
      it.bottomTags = (it.bottomTags as string[] || []).concat([
        it.age as string, 
      ]);
    });
    return item;
  },
  'inheritor-deadbox': (item: any[]) => {
    item.forEach(it => {
      it.titleBox = Boolean(it.deathBirth);
    });
    return item;
  },
} as Record<string, (item: any[]) => any[]>

export function resolveCommonContentSolveProps(res: any[], dataSolve: IHomeCommonCategoryListTabListDataSolve[]) {
  for (const solve of dataSolve) {
    if (isDynamicScript(solve)) {
      res = getDynamicScript().execute(solve, {
        main: res
      }) as any;
    }
    res = resolveCommonContentData[solve]?.(res) || res;
  }
  return res;
}