import { computed } from '@angular/core';
import type { DocumentNode } from 'graphql';
import { useGraphqlDeleteById } from '../atoms/graphql/use-graphql-delete-by-id';
import { useGraphqlPaginate } from '../atoms/graphql/use-graphql-paginate';

interface GraphqlListConfig {
  readonly queryPagination: DocumentNode;
  readonly paginationKey: string;
  readonly mutationDeleteObjectById: DocumentNode;
  readonly fields?: string;
}

export function useGraphqlList<T>(config: GraphqlListConfig) {
  const paginateComposable = useGraphqlPaginate<T>(
    config.queryPagination,
    config.paginationKey,
    config.fields,
  );
  const deleteComposable = useGraphqlDeleteById(config.mutationDeleteObjectById);

  const loading = computed(() => paginateComposable.loading() || deleteComposable.loading());

  return {
    paginate: paginateComposable.paginate,
    pagination: paginateComposable.pagination,
    deleteById: deleteComposable.deleteById,
    loading,
  };
}

export type GraphqlList<T> = ReturnType<typeof useGraphqlList<T>>;
