import {
  productsQueryKeys
} from "./chunk-YAUGB4LD.js";
import {
  queryClient
} from "./chunk-RHKRREUU.js";
import {
  queryKeysFactory
} from "./chunk-ZA2KFUFR.js";
import {
  sdk
} from "./chunk-DTGEFKK4.js";

// src/hooks/api/offers.tsx
import {
  useMutation,
  useQuery
} from "@tanstack/react-query";
var OFFERS_QUERY_KEY = "offers";
var offerQueryKeys = queryKeysFactory(OFFERS_QUERY_KEY);
var useOffers = (query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: () => sdk.vendor.offers.query({ ...query }),
    queryKey: offerQueryKeys.list(query),
    ...options
  });
  return { ...data, ...rest };
};
var useOffer = (id, query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: () => sdk.vendor.offers.$id.query({ $id: id, ...query }),
    queryKey: offerQueryKeys.detail(id, query),
    enabled: !!id,
    ...options
  });
  return { ...data, ...rest };
};
var useBulkCreateOffers = (options) => {
  return useMutation({
    mutationFn: (payload) => sdk.vendor.offers.batch.mutate(payload),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: offerQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.all
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useUpdateOffer = (id, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.vendor.offers.$id.mutate({ $id: id, ...payload }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: offerQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.all
      });
      queryClient.invalidateQueries({
        queryKey: offerQueryKeys.detail(id)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useBulkDeleteOffers = (options) => {
  return useMutation({
    mutationFn: async (ids) => {
      const results = await Promise.allSettled(
        ids.map((id) => sdk.vendor.offers.$id.delete({ $id: id }))
      );
      const succeeded = results.map((r, i) => r.status === "fulfilled" ? ids[i] : null).filter((x) => x !== null);
      const failed = results.map(
        (r, i) => r.status === "rejected" ? { id: ids[i], error: r.reason } : null
      ).filter(
        (x) => x !== null
      );
      return { succeeded, failed };
    },
    onSuccess: (result, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: offerQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.all
      });
      result.succeeded.forEach((id) => {
        queryClient.removeQueries({
          queryKey: offerQueryKeys.detail(id)
        });
      });
      options?.onSuccess?.(result, variables, context);
    },
    ...options
  });
};

export {
  offerQueryKeys,
  useOffers,
  useOffer,
  useBulkCreateOffers,
  useUpdateOffer,
  useBulkDeleteOffers
};
