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

// src/hooks/api/store.tsx
import { ClientError } from "@mercurjs/client";
import { useMutation, useQuery } from "@tanstack/react-query";
var STORE_QUERY_KEY = "store";
var storeQueryKeys = queryKeysFactory(STORE_QUERY_KEY);
async function retrieveActiveStore(query) {
  const response = await sdk.admin.stores.query(query);
  const activeStore = response.stores?.[0];
  if (!activeStore) {
    throw new ClientError("No active store found", "Not Found", 404);
  }
  return { store: activeStore };
}
var useStore = (query, options) => {
  const { data, ...rest } = useQuery({
    queryFn: () => retrieveActiveStore(query),
    queryKey: storeQueryKeys.details(),
    ...options
  });
  return {
    ...data,
    ...rest
  };
};
var useUpdateStore = (id, options) => {
  return useMutation({
    mutationFn: (payload) => sdk.admin.stores.$id.mutate({ $id: id, ...payload }),
    onSuccess: (data, variables, context) => {
      queryClient.invalidateQueries({
        queryKey: pricePreferencesQueryKeys.list()
      });
      queryClient.invalidateQueries({
        queryKey: pricePreferencesQueryKeys.details()
      });
      queryClient.invalidateQueries({ queryKey: storeQueryKeys.details() });
      options?.onSuccess?.(data, variables, context);
    },
    ...options
  });
};

export {
  storeQueryKeys,
  retrieveActiveStore,
  useStore,
  useUpdateStore
};
