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

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

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

// src/hooks/api/product-attributes.tsx
import { useQuery } from "@tanstack/react-query";
var PRODUCT_ATTRIBUTES_QUERY_KEY = "product_attributes";
var productAttributesQueryKeys = queryKeysFactory(
  PRODUCT_ATTRIBUTES_QUERY_KEY
);
var sortAttributeValuesByRank = (attributes) => {
  attributes?.forEach((attribute) => {
    if (Array.isArray(attribute.values)) {
      attribute.values.sort((a, b) => (a.rank ?? 0) - (b.rank ?? 0));
    }
  });
};
var useProductAttributes = (query, options) => {
  const { data, ...rest } = useQuery({
    queryKey: productAttributesQueryKeys.list(query),
    queryFn: async () => {
      const res = await sdk.vendor.productAttributes.query({ ...query });
      sortAttributeValuesByRank(res?.product_attributes);
      return res;
    },
    ...options
  });
  return { ...data, ...rest };
};

// src/hooks/api/products.tsx
var PRODUCTS_QUERY_KEY = "products";
var productsQueryKeys = queryKeysFactory(PRODUCTS_QUERY_KEY);
var VARIANTS_QUERY_KEY = "product_variants";
var variantsQueryKeys = queryKeysFactory(VARIANTS_QUERY_KEY);
var PRODUCT_CHANGE_QUERY_KEY = "product_change";
var productChangeQueryKeys = queryKeysFactory(
  PRODUCT_CHANGE_QUERY_KEY
);
var useProduct = (id, query, options) => {
  const { data, ...rest } = useQuery2({
    queryFn: () => sdk.vendor.products.$id.query({ $id: id, ...query }),
    queryKey: productsQueryKeys.detail(id, query),
    ...options
  });
  return { ...data, ...rest };
};
var useProducts = (query, options) => {
  const { data, ...rest } = useQuery2({
    queryFn: () => sdk.vendor.products.query({ ...query }),
    queryKey: productsQueryKeys.list(query),
    ...options
  });
  return { ...data, ...rest };
};
var useProductChange = (productId, options) => {
  const { data, ...rest } = useQuery2({
    queryFn: () => sdk.vendor.products.$id.preview.query({ $id: productId }),
    queryKey: productChangeQueryKeys.detail(productId),
    ...options
  });
  return { ...data, ...rest };
};
var useCreateProduct = (options) => {
  return useMutation({
    mutationFn: (payload) => sdk.vendor.products.mutate(payload),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() });
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.lists()
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useUpdateProduct = (id, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.vendor.products.$id.mutate({
      $id: id,
      ...payload
    }),
    onSuccess: async (data, variables, context) => {
      await queryClient.invalidateQueries({
        queryKey: productChangeQueryKeys.detail(id)
      });
      await queryClient.invalidateQueries({
        queryKey: productsQueryKeys.detail(id)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useDeleteProduct = (id, options) => {
  return useMutation({
    mutationFn: () => sdk.vendor.products.$id.delete({ $id: id }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: productChangeQueryKeys.detail(id)
      });
      queryClient.invalidateQueries({ queryKey: productsQueryKeys.detail(id) });
      queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useCancelProductEdit = (id, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.vendor.products.$id.cancel.mutate({
      $id: id,
      ...payload ?? {}
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: productChangeQueryKeys.detail(id)
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.detail(id)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useProductVariant = (productId, variantId, query, options) => {
  const { data, ...rest } = useQuery2({
    queryFn: () => sdk.vendor.products.$id.variants.$variantId.query({
      $id: productId,
      $variantId: variantId,
      ...query
    }),
    queryKey: variantsQueryKeys.detail(variantId, query),
    ...options
  });
  return { ...data, ...rest };
};
var useProductVariants = (productId, query, options) => {
  const { data, ...rest } = useQuery2({
    queryFn: () => sdk.vendor.products.$id.variants.query({ $id: productId, ...query }),
    queryKey: variantsQueryKeys.list({ productId, ...query }),
    ...options,
    enabled: !!productId && (options?.enabled !== void 0 ? options.enabled : true)
  });
  return { ...data, ...rest };
};
var useCreateProductVariant = (productId, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.vendor.products.$id.variants.mutate({
      $id: productId,
      ...payload
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: productChangeQueryKeys.detail(productId)
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.detail(productId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useUpdateProductVariant = (productId, variantId, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.vendor.products.$id.variants.$variantId.mutate({
      $id: productId,
      $variantId: variantId,
      ...payload
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: productChangeQueryKeys.detail(productId)
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.detail(productId)
      });
      queryClient.invalidateQueries({
        queryKey: variantsQueryKeys.detail(variantId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useDeleteVariant = (productId, variantId, options) => {
  return useMutation({
    mutationFn: () => sdk.vendor.products.$id.variants.$variantId.delete({
      $id: productId,
      $variantId: variantId
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: productChangeQueryKeys.detail(productId)
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.detail(productId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useDeleteVariantLazy = (productId, options) => {
  return useMutation({
    mutationFn: ({ variantId }) => sdk.vendor.products.$id.variants.$variantId.delete({
      $id: productId,
      $variantId: variantId
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: productChangeQueryKeys.detail(productId)
      });
      queryClient.invalidateQueries({
        queryKey: variantsQueryKeys.detail(variables.variantId)
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.detail(productId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useBatchProductAttributes = (productId, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.vendor.products.$id.attributes.batch.mutate({
      $id: productId,
      ...payload
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: productAttributesQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.detail(productId)
      });
      queryClient.invalidateQueries({
        queryKey: productChangeQueryKeys.detail(productId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useRemoveAttributeFromProduct = (productId, attributeId, options) => {
  return useMutation({
    mutationFn: () => sdk.vendor.products.$id.attributes.batch.mutate({
      $id: productId,
      remove: [attributeId]
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: productAttributesQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: productsQueryKeys.detail(productId)
      });
      queryClient.invalidateQueries({
        queryKey: productChangeQueryKeys.detail(productId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};

// src/hooks/api/inventory.tsx
var INVENTORY_ITEMS_QUERY_KEY = "inventory_items";
var inventoryItemsQueryKeys = queryKeysFactory(
  INVENTORY_ITEMS_QUERY_KEY
);
var INVENTORY_ITEM_LEVELS_QUERY_KEY = "inventory_item_levels";
var inventoryItemLevelsQueryKeys = queryKeysFactory(
  INVENTORY_ITEM_LEVELS_QUERY_KEY
);
var useInventoryItems = (query, options) => {
  const { data, ...rest } = useQuery3({
    queryFn: () => sdk.vendor.inventoryItems.query({ ...query }),
    queryKey: inventoryItemsQueryKeys.list(query),
    ...options
  });
  return { ...data, ...rest };
};
var useInventoryItem = (id, query, options) => {
  const { data, ...rest } = useQuery3({
    queryFn: () => sdk.vendor.inventoryItems.$id.query({ $id: id, ...query }),
    queryKey: inventoryItemsQueryKeys.detail(id, query),
    ...options
  });
  return { ...data, ...rest };
};
var useCreateInventoryItem = (options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.vendor.inventoryItems.mutate(payload),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.lists()
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useUpdateInventoryItem = (id, options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.vendor.inventoryItems.$id.mutate({ $id: id, ...payload }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.detail(id)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useDeleteInventoryItem = (id, options) => {
  return useMutation2({
    mutationFn: () => sdk.vendor.inventoryItems.$id.delete({ $id: id }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.detail(id)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useDeleteInventoryItemLevel = (inventoryItemId, locationId, options) => {
  return useMutation2({
    mutationFn: () => sdk.vendor.inventoryItems.$id.locationLevels.$locationId.delete({
      $id: inventoryItemId,
      $locationId: locationId
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.detail(inventoryItemId)
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId)
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useInventoryItemLevels = (inventoryItemId, query, options) => {
  const { data, ...rest } = useQuery3({
    queryFn: () => sdk.vendor.inventoryItems.$id.locationLevels.query({
      $id: inventoryItemId,
      limit: query?.limit ?? 100,
      offset: query?.offset ?? 0,
      ...query
    }),
    queryKey: inventoryItemLevelsQueryKeys.list({
      ...query || {},
      inventoryItemId
    }),
    ...options,
    enabled: !!inventoryItemId && (options?.enabled !== void 0 ? options.enabled : true)
  });
  return { ...data, ...rest };
};
var useUpdateInventoryLevel = (inventoryItemId, locationId, options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.vendor.inventoryItems.$id.locationLevels.$locationId.mutate({
      $id: inventoryItemId,
      $locationId: locationId,
      ...payload
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.detail(inventoryItemId)
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId)
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemLevelsQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: variantsQueryKeys.details()
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useBatchInventoryItemLocationLevels = (inventoryItemId, options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.vendor.inventoryItems.$id.locationLevels.batch.mutate({
      $id: inventoryItemId,
      ...payload
    }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.lists()
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.detail(inventoryItemId)
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId)
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemLevelsQueryKeys.list({ inventoryItemId })
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};
var useBatchInventoryItemsLocationLevels = (options) => {
  return useMutation2({
    mutationFn: (payload) => sdk.vendor.inventoryItems.locationLevels.batch.mutate(payload),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: inventoryItemsQueryKeys.all
      });
      queryClient.invalidateQueries({
        queryKey: inventoryItemLevelsQueryKeys.all
      });
      queryClient.invalidateQueries({
        queryKey: variantsQueryKeys.lists()
      });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};

export {
  inventoryItemsQueryKeys,
  inventoryItemLevelsQueryKeys,
  useInventoryItems,
  useInventoryItem,
  useCreateInventoryItem,
  useUpdateInventoryItem,
  useDeleteInventoryItem,
  useDeleteInventoryItemLevel,
  useInventoryItemLevels,
  useUpdateInventoryLevel,
  useBatchInventoryItemLocationLevels,
  useBatchInventoryItemsLocationLevels,
  useProductAttributes,
  productsQueryKeys,
  variantsQueryKeys,
  useProduct,
  useProducts,
  useProductChange,
  useCreateProduct,
  useUpdateProduct,
  useDeleteProduct,
  useCancelProductEdit,
  useProductVariant,
  useProductVariants,
  useCreateProductVariant,
  useUpdateProductVariant,
  useDeleteVariant,
  useDeleteVariantLazy,
  useBatchProductAttributes,
  useRemoveAttributeFromProduct
};
