import CommonContent, { GetContentListItem, GetContentListParams } from "@/api/CommonContent";
import { useSimpleDataLoader, type ISimpleDataLoader } from "@/common/composeabe/SimpleDataLoader";
import { navTo } from "@/common/utils/PageAction"

export interface IHomePageMiniCommonListGoMoreAndGoDetail {
  loader: ISimpleDataLoader<GetContentListItem[], any>;
  goDetail: (id: number) => void;
  goList: () => void; 
}

/**
 * 专用于通用内容的首页小列表控制代码组合
 * @param p 
 * @returns 
 */
export function useHomePageMiniCommonListGoMoreAndGoDetail(p: {
  title?: string,
  mainBodyColumnId?: number|number[],
  modelId?: number,
  itemType?: string,
  detailsPage: string,
  count?: number,
}) : IHomePageMiniCommonListGoMoreAndGoDetail {
  function goDetail(id: number) {
    navTo(p.detailsPage, {
      mainBodyColumnId: p.mainBodyColumnId,
      modelId: p.modelId,
      id,
    }) 
  }
  function goList() {
    navTo('/pages/article/common/list', {
      title: p.title,
      mainBodyColumnId: typeof p.mainBodyColumnId == 'object' ? 
        p.mainBodyColumnId.join(',') : 
        p.mainBodyColumnId,
      modelId: p.modelId,
      itemType: p.itemType,
      detailsPage: p.detailsPage,
    }) 
  }

  const loader = useSimpleDataLoader(async () => 
    (await CommonContent.getContentList(new GetContentListParams().setSelfValues({
      mainBodyColumnId: p.mainBodyColumnId,
      modelId: p.modelId,
    }), 1, p.count ?? 4)).list
  );

  return {
    loader,
    goDetail,
    goList,
  }
}