import { gql, useQuery, QueryHookOptions } from '@apollo/client';
import { useGraphQLData } from './helper';

export const QUERY_IS_AUTHORIZED = gql`
  query isAuthorized($action: String!, $object: AuthorizationObject!) {
    isAuthorized(action: $action, object: $object)
  }
`;

export function useIsAuthorizedQuery<A = any, R = Record<string, any>>(options: QueryHookOptions<A, R> = {}) {
  return useQuery(QUERY_IS_AUTHORIZED, options);
}

export function useIsAuthorized<A = any, R = Record<string, any>>(options: QueryHookOptions<A, R> = {}, defaultValue = false) {
  const { data, loading, error } = useGraphQLData<boolean>(
    QUERY_IS_AUTHORIZED, 'isAuthorized', defaultValue, options
  );
  return { isAuthorized: data, loading, error };
}