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 { type IConferma } from '@/shared/model/conferma.model';

import ConfermaService from './conferma.service';

export default defineComponent({
  name: 'ConfermaDetails',
  setup() {
    const confermaService = inject('confermaService', () => new ConfermaService());
    const alertService = inject('alertService', () => useAlertService(), true);

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

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

    const retrieveConferma = async confermaId => {
      try {
        const res = await confermaService().find(confermaId);
        conferma.value = res;
      } catch (error) {
        alertService.showHttpError(error.response);
      }
    };

    if (route.params?.confermaId) {
      retrieveConferma(route.params.confermaId);
    }

    return {
      alertService,
      conferma,

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