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

// src/hooks/api/promotions.tsx
import {
  useMutation as useMutation2,
  useQuery as useQuery2
} from "@tanstack/react-query";

// src/hooks/api/campaigns.tsx
import {
  useMutation,
  useQuery
} from "@tanstack/react-query";
var REGIONS_QUERY_KEY = "campaigns";
var campaignsQueryKeys = queryKeysFactory(REGIONS_QUERY_KEY);
var useCampaign = (id, query, options) => {
  const { data, ...rest } = useQuery({
    queryKey: campaignsQueryKeys.detail(id),
    queryFn: () => sdk.admin.campaigns.$id.query({ $id: id, ...query }),
    ...options
  });
  return { ...data, ...rest };
};
var useCampaigns = (query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: () => sdk.admin.campaigns.query({ ...query }),
    queryKey: campaignsQueryKeys.list(query),
    ...options
  });
  return { ...data, ...rest };
};
var useCreateCampaign = (options) => {
  return useMutation({
    mutationFn: (payload) => sdk.admin.campaigns.mutate(payload),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useUpdateCampaign = (id, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.admin.campaigns.$id.mutate({ $id: id, ...payload }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() });
      queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() });
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() });
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.details() });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useDeleteCampaign = (id, options) => {
  return useMutation({
    mutationFn: () => sdk.admin.campaigns.$id.delete({ $id: id }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() });
      queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useAddOrRemoveCampaignPromotions = (id, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.admin.campaigns.$id.promotions.mutate({ $id: id, ...payload }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() });
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() });
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.details() });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};

// src/hooks/api/promotions.tsx
var PROMOTIONS_QUERY_KEY = "promotions";
var promotionsQueryKeys = {
  ...queryKeysFactory(PROMOTIONS_QUERY_KEY),
  // TODO: handle invalidations properly
  listRules: (id, ruleType, query) => [PROMOTIONS_QUERY_KEY, id, ruleType, query],
  listRuleAttributes: (ruleType, promotionType, applicationMethodTargetType) => [
    PROMOTIONS_QUERY_KEY,
    ruleType,
    promotionType,
    applicationMethodTargetType
  ],
  listRuleValues: (ruleType, ruleValue, query) => [PROMOTIONS_QUERY_KEY, ruleType, ruleValue, query]
};
var usePromotion = (id, options) => {
  const { data, ...rest } = useQuery2({
    queryKey: promotionsQueryKeys.detail(id),
    queryFn: async () => sdk.admin.promotions.$id.query({ $id: id }),
    ...options
  });
  return { ...data, ...rest };
};
var usePromotionRules = (id, ruleType, query, options) => {
  const { data, ...rest } = useQuery2({
    queryKey: promotionsQueryKeys.listRules(id, ruleType, query),
    queryFn: async () => sdk.admin.promotions.$id.$ruleType.query({
      $id: id,
      $ruleType: ruleType,
      ...query
    }),
    ...options
  });
  return { ...data, ...rest };
};
var usePromotions = (query, options) => {
  const { data, ...rest } = useQuery2({
    queryKey: promotionsQueryKeys.list(query),
    queryFn: async () => sdk.admin.promotions.query({ ...query }),
    ...options
  });
  return { ...data, ...rest };
};
var usePromotionRuleAttributes = (ruleType, promotionType, applicationMethodTargetType, options) => {
  const { data, ...rest } = useQuery2({
    queryKey: promotionsQueryKeys.listRuleAttributes(
      ruleType,
      promotionType,
      applicationMethodTargetType
    ),
    queryFn: async () => sdk.admin.promotions.ruleAttributeOptions.$ruleType.query({
      $ruleType: ruleType,
      promotion_type: promotionType,
      application_method_target_type: applicationMethodTargetType
    }),
    ...options
  });
  return { ...data, ...rest };
};
var useDeletePromotion = (id, options) => {
  return useMutation2({
    mutationFn: () => sdk.admin.promotions.$id.delete({ $id: id }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() });
      queryClient.invalidateQueries({
        queryKey: promotionsQueryKeys.detail(id)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useCreatePromotion = (options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.admin.promotions.mutate(payload),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() });
      queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useUpdatePromotion = (id, options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.admin.promotions.$id.mutate({ $id: id, ...payload }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var usePromotionAddRules = (id, ruleType, options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.admin.promotions.$id.$ruleType.batch.mutate({
      $id: id,
      $ruleType: ruleType,
      create: payload.rules
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var usePromotionRemoveRules = (id, ruleType, options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.admin.promotions.$id.$ruleType.batch.mutate({
      $id: id,
      $ruleType: ruleType,
      delete: payload.rule_ids
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var usePromotionUpdateRules = (id, ruleType, options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.admin.promotions.$id.$ruleType.batch.mutate({
      $id: id,
      $ruleType: ruleType,
      update: payload.rules
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};

export {
  promotionsQueryKeys,
  usePromotion,
  usePromotionRules,
  usePromotions,
  usePromotionRuleAttributes,
  useDeletePromotion,
  useCreatePromotion,
  useUpdatePromotion,
  usePromotionAddRules,
  usePromotionRemoveRules,
  usePromotionUpdateRules,
  campaignsQueryKeys,
  useCampaign,
  useCampaigns,
  useCreateCampaign,
  useUpdateCampaign,
  useDeleteCampaign,
  useAddOrRemoveCampaignPromotions
};
