import { type Ref, defineComponent, inject, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';

import { useAlertService } from '@/shared/alert/alert.service';
import { useDateFormat } from '@/shared/composables';
import { type IDisponibilita } from '@/shared/model/disponibilita.model';

import DisponibilitaService from './disponibilita.service';

export default defineComponent({
  name: 'DisponibilitaDetails',
  setup() {
    const dateFormat = useDateFormat();
    const disponibilitaService = inject('disponibilitaService', () => new DisponibilitaService());
    const alertService = inject('alertService', () => useAlertService(), true);

    const route = useRoute();
    const router = useRouter();

    const previousState = () => router.go(-1);
    const disponibilita: Ref<IDisponibilita> = ref({});

    const retrieveDisponibilita = async disponibilitaId => {
      try {
        const res = await disponibilitaService().find(disponibilitaId);
        disponibilita.value = res;
      } catch (error) {
        alertService.showHttpError(error.response);
      }
    };

    if (route.params?.disponibilitaId) {
      retrieveDisponibilita(route.params.disponibilitaId);
    }

    return {
      ...dateFormat,
      alertService,
      disponibilita,

      previousState,
      t$: useI18n().t,
    };
  },
});
