import { api } from 'src/boot/axios';
import { Ref, ref, watch } from 'vue';

export interface DocumentoIdentidad {
  ID_DOCUMENTO_IDENTIDAD: number;
  CODIGO: string;
  NOMBRE: string;
}

export function useFetchDocumentosIdentidad(idPais: Ref<number | null>) {
  const loading = ref(false);
  const documentos = ref<DocumentoIdentidad[]>([]);
  watch(idPais, (id) => fetchDocumentos(id), { immediate: true });

  function fetchDocumentos(id: number | null) {
    loading.value = true;
    api
      .get('/documentos_identidad_paises', { params: { idPais: id } })
      .then(({ data }) => {
        documentos.value = data || [];
      })
      .finally(() => (loading.value = false));
  }
  return { loading, documentos };
}
