import { useQuery } from '@tanstack/react-query';
import { orderService } from '../api/order.service';

export function useStores() {
  const query = useQuery({
    queryKey: ['stores'],
    queryFn: () => orderService.findStores(),
    staleTime: 1000 * 60 * 30,
    retry: 1,
    retryOnMount: false,
    refetchOnWindowFocus: false,
  });

  const options =
    query.data?.map((store, index) => {
      const uniqueId =
        store.id || store.storeId || store.code || `store-${index}`;
      const label =
        store.store || store.label || store.name || `Loja ${index + 1}`;

      return {
        value: uniqueId,
        label: label,
        id: `${uniqueId}-${index}`,
      };
    }) ?? [];

  return {
    ...query,
    options,
  };
}
