import { useDataSource } from "@erp/core/hooks";
import { useQuery } from "@tanstack/react-query";
import { CustomerSummary, CustomersPage } from "../schemas";

export interface CustomersCriteria {
  q?: string;
  orderBy?: string;
  order?: string;
  pageSize?: number;
  pageNumber?: number;
}

// Obtener todos los clientes
export const useCustomersSearchQuery = (criteria: CustomersCriteria) => {
  const dataSource = useDataSource();

  return useQuery({
    queryKey: ["customer", criteria],
    queryFn: async (context) => {
      const { signal } = context;

      const customers = await dataSource.getList("customers", {
        signal,
        ...criteria,
      });

      return customers as CustomersPage;
    },
    select: (data) => data.items as CustomerSummary[],
  });
};
