import { ref } from 'vue';
import { api } from 'src/boot/axios';
import { notifyError } from 'src/helpers';

export function useTogglePublicImage(refetch: () => void) {
  const updatingImage = ref(false);

  const togglePublicImage = ({ fileId, isPublic }: { fileId: number; isPublic: boolean }) => {
    updatingImage.value = true;

    api
      .put(`file/${fileId}`, { PUBLICO: isPublic })
      .then(() => {
        refetch();
      })
      .catch((err) => {
        notifyError(err, 'Error al actualizar la imagen');
      })
      .finally(() => {
        updatingImage.value = false;
      });
  };

  return {
    updatingImage,
    togglePublicImage,
  };
}
