import { UseListQueryResult, useList } from "@/lib/hooks/useDataSource";
import { useDataSource } from "@/lib/hooks/useDataSource/useDataSource";
import { useQueryKey } from "@/lib/hooks/useQueryKey";
import { IListArticles_Response_DTO, IListResponse_DTO } from "@shared/contexts";

export type UseCatalogListParams = {
  pagination: {
    pageIndex: number;
    pageSize: number;
  };
  searchTerm?: string[];
  enabled?: boolean;
  queryOptions?: Record<string, unknown>;
};

export type UseCatalogListResponse = UseListQueryResult<
  IListResponse_DTO<IListArticles_Response_DTO>,
  unknown
>;

export const useCatalogList = (params: UseCatalogListParams): UseCatalogListResponse => {
  const dataSource = useDataSource();
  const keys = useQueryKey();

  const { pagination, searchTerm = [], enabled = true, queryOptions } = params;

  return useList({
    queryKey: keys().data().resource("catalog").action("list").params(params).get(),
    queryFn: () => {
      return dataSource.getList({
        resource: "catalog",
        quickSearchTerm: searchTerm,
        pagination,
      });
    },
    enabled,
    queryOptions,
  });
};
