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

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

export function goCommonContentDetail(id: number) {
  navTo('/pages/article/details', {
    id,
  })
}

export function goCommonContentList(p: {
  title?: string,
  mainBodyId?: number,
  mainBodyColumnId?: number|number[],
  modelId?: number,
  latitude?: number,
  longitude?: number,
  isNearby?: boolean,
  autoJumpFirst?: boolean,
}) {
  navTo('/pages/article/common/list', {
    title: p.title,
    mainBodyId: p.mainBodyId,
    mainBodyColumnId: typeof p.mainBodyColumnId == 'object' ? 
      p.mainBodyColumnId.join(',') : 
      p.mainBodyColumnId,
    modelId: p.modelId,
    latitude: p.latitude,
    longitude: p.longitude,
    isNearby: p.isNearby,
    autoJumpFirst: p.autoJumpFirst,
  }) 
} 

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

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

  return {
    loader,
    goDetail,
    goList,
  }
}