import { QueryKey, useQueryClient } from '@tanstack/vue-query';
import { getValue } from '@win2win/shared-ui';
import { MaybeRef } from 'vue';

export interface RefetchOptions {
  queryKey?: MaybeRef<QueryKey>;
  type?: 'active' | 'inactive' | 'all';
}

export function useRefetch(options: RefetchOptions | RefetchOptions[]) {
  const optionsArray = Array.isArray(options) ? options : [options];
  const queryClient = useQueryClient();
  const refetch = () => {
    optionsArray.forEach((option) => {
      const { queryKey, type = 'all' } = option;
      queryClient.invalidateQueries({ queryKey: getValue(queryKey), type });
    });
  };
  return refetch;
}
