import {
  reservationItemsQueryKeys
} from "./chunk-22XJUSMM.js";
import {
  inventoryItemsQueryKeys
} from "./chunk-BA2HXHTW.js";
import {
  queryClient
} from "./chunk-RHKRREUU.js";
import {
  queryKeysFactory
} from "./chunk-ZA2KFUFR.js";
import {
  sdk
} from "./chunk-RIN4CBRB.js";

// src/hooks/api/orders.tsx
import {
  useMutation,
  useQuery
} from "@tanstack/react-query";
var ORDERS_QUERY_KEY = "orders";
var _orderKeys = queryKeysFactory(ORDERS_QUERY_KEY);
_orderKeys.preview = function(id) {
  return [this.detail(id), "preview"];
};
_orderKeys.changes = function(id) {
  return [this.detail(id), "changes"];
};
_orderKeys.lineItems = function(id) {
  return [this.detail(id), "lineItems"];
};
_orderKeys.shippingOptions = function(id) {
  return [this.detail(id), "shippingOptions"];
};
var ordersQueryKeys = _orderKeys;
var useOrder = (id, query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: async () => sdk.admin.orders.$id.query({ $id: id, ...query }),
    queryKey: ordersQueryKeys.detail(id, query),
    ...options
  });
  return { ...data, ...rest };
};
var useUpdateOrder = (id, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.admin.orders.$id.mutate({ $id: id, ...payload }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.detail(id)
      });
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.changes(id)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useOrderPreview = (id, query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: async () => sdk.admin.orders.$id.preview.query({ $id: id, ...query }),
    queryKey: ordersQueryKeys.preview(id),
    ...options
  });
  return { ...data, ...rest };
};
var useOrders = (query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: async () => sdk.admin.orders.query({ ...query }),
    queryKey: ordersQueryKeys.list(query),
    ...options
  });
  return { ...data, ...rest };
};
var useOrderShippingOptions = (id, query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: async () => sdk.admin.orders.$id.shippingOptions.query({ $id: id, ...query }),
    queryKey: ordersQueryKeys.shippingOptions(id),
    ...options
  });
  return { ...data, ...rest };
};
var useOrderChanges = (id, query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: async () => sdk.admin.orders.$id.changes.query({ $id: id, ...query }),
    queryKey: ordersQueryKeys.changes(id),
    ...options
  });
  return { ...data, ...rest };
};
var useOrderLineItems = (id, query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: async () => sdk.admin.orders.$id.lineItems.query({ $id: id, ...query }),
    queryKey: ordersQueryKeys.lineItems(id),
    ...options
  });
  return { ...data, ...rest };
};
var useCreateOrderFulfillment = (orderId, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.admin.orders.$id.fulfillments.mutate({ $id: orderId, ...payload }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.all
      });
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.preview(orderId)
      });
      queryClient.invalidateQueries({
        queryKey: reservationItemsQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.details()
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useCancelOrderFulfillment = (orderId, fulfillmentId, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.admin.orders.$id.fulfillments.$fulfillmentId.cancel.mutate({
      $id: orderId,
      $fulfillmentId: fulfillmentId,
      ...payload
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.all
      });
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.preview(orderId)
      });
      queryClient.invalidateQueries({
        queryKey: reservationItemsQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.details()
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useCreateOrderShipment = (orderId, fulfillmentId, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.admin.orders.$id.fulfillments.$fulfillmentId.shipments.mutate({
      $id: orderId,
      $fulfillmentId: fulfillmentId,
      ...payload
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.all
      });
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.preview(orderId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useMarkOrderFulfillmentAsDelivered = (orderId, fulfillmentId, options) => {
  return useMutation({
    mutationFn: () => sdk.admin.orders.$id.fulfillments.$fulfillmentId.markAsDelivered.mutate({
      $id: orderId,
      $fulfillmentId: fulfillmentId
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.all
      });
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.preview(orderId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useCancelOrder = (orderId, options) => {
  return useMutation({
    mutationFn: () => sdk.admin.orders.$id.cancel.mutate({ $id: orderId }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.detail(orderId)
      });
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.preview(orderId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useRequestTransferOrder = (orderId, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.admin.orders.$id.transfer.mutate({ $id: orderId, ...payload }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.preview(orderId)
      });
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.changes(orderId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useCancelOrderTransfer = (orderId, options) => {
  return useMutation({
    mutationFn: () => sdk.admin.orders.$id.transfer.cancel.mutate({ $id: orderId }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.preview(orderId)
      });
      queryClient.invalidateQueries({
        queryKey: ordersQueryKeys.changes(orderId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};

export {
  ordersQueryKeys,
  useOrder,
  useUpdateOrder,
  useOrderPreview,
  useOrders,
  useOrderShippingOptions,
  useOrderChanges,
  useOrderLineItems,
  useCreateOrderFulfillment,
  useCancelOrderFulfillment,
  useCreateOrderShipment,
  useMarkOrderFulfillmentAsDelivered,
  useCancelOrder,
  useRequestTransferOrder,
  useCancelOrderTransfer
};
