import { useFetch } from '@win2win/shared-ui';
import { formatContactLiteName } from 'src/helpers/formatters';
import { ContactoLite } from 'src/models';
import { computed } from 'vue';
import { useDocumentsDialogContext } from './useDocumentsDialogContext';

export function useDocumentsContact() {
  const { currentDocument } = useDocumentsDialogContext();
  const currentIdContact = computed(() => {
    const document = currentDocument.value;
    return document?.ID_CONTACTO ?? null;
  });

  const queryKey = computed(() => ['contacto', { id: currentIdContact.value }]);
  const path = computed(() => {
    return `w2w-query/contacto(${currentIdContact.value})`;
  });
  const { data, fetching } = useFetch<ContactoLite>(queryKey, path, undefined, undefined, { enabled: computed(() => !!currentIdContact.value) });
  const name = computed(() => (data.value ? formatContactLiteName(data.value) : ''));

  return {
    data,
    fetching,
    name,
  };
}
