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 UseDealerListParams = {
  pagination: {
    pageIndex: number;
    pageSize: number;
  };
  searchTerm?: string[];
  enabled?: boolean;
  queryOptions?: Record<string, unknown>;
};

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

export const useDealerList = (params: UseDealerListParams): UseDealerListResponse => {
  const dataSource = useDataSource();
  const keys = useQueryKey();

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

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