import { useDataSource, useQueryKey } from "@erp/core/client";
import { ListCustomersQueryDTO, ListCustomersResultDTO } from "@erp/customers";
import { UseQueryResult, useQuery } from "@tanstack/react-query";

// Obtener clientes
export const useCustomersQuery = (
  params: ListCustomersQueryDTO
): UseQueryResult<ListCustomersResultDTO, Error> => {
  const dataSource = useDataSource();
  const keys = useQueryKey();

  return useQuery<ListCustomersResultDTO, Error>({
    queryKey: keys().data().resource("customers").action("list").params(params).get(),
    queryFn: (context) => {
      const { signal } = context;
      return dataSource.getList<ListCustomersResultDTO>("customers", {
        signal,
        ...params,
      });
    },
  });
};
