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

type UseCustomersQueryParams = ListCustomersQueryDTO;

// Obtener clientes
export const useCustomersQuery = (
  params: UseCustomersQueryParams
): 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,
      });
    },
  });
};
