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

interface UpdateFilePayload {
  fileId: number;
  [key: string]: any;
}

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

  const updateFileData = async (data: UpdateFilePayload): Promise<void> => {
    const { fileId, ...restData } = data;
    updatingImage.value = true;

    try {
      await api.put(`file/${fileId}`, restData);
      refetch();
    } catch (err) {
      notifyError(err, 'Error al actualizar la imagen');
      throw err; // Re-lanza el error para manejarlo en el consumidor si es necesario
    } finally {
      updatingImage.value = false;
    }
  };

  return {
    updatingImage,
    updateFileData,
  };
}
