import { inject, Injector, runInInjectionContext, signal } from '@angular/core';
import type { DocumentNode } from 'graphql';
import { finalize } from 'rxjs';
import { mutateDeleteById } from '../../../modules/graphql/fetchers/mutate-delete-by-id';

export function useGraphqlDeleteById(mutation: DocumentNode) {
  const injector = inject(Injector);
  const loading = signal(false);

  function deleteById(id: string, headers?: Record<string, string>) {
    loading.set(true);
    return runInInjectionContext(injector, () => mutateDeleteById(mutation, id, headers)).pipe(
      finalize(() => loading.set(false)),
    );
  }

  return {
    deleteById,
    loading: loading.asReadonly(),
  };
}
