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 useDataUtils from '@/shared/data/data-utils.service';
import { type IModelloLiberatoria } from '@/shared/model/modello-liberatoria.model';

import ModelloLiberatoriaService from './modello-liberatoria.service';

export default defineComponent({
  name: 'ModelloLiberatoriaDetails',
  setup() {
    const dateFormat = useDateFormat();
    const modelloLiberatoriaService = inject('modelloLiberatoriaService', () => new ModelloLiberatoriaService());
    const alertService = inject('alertService', () => useAlertService(), true);

    const dataUtils = useDataUtils();

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

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

    const retrieveModelloLiberatoria = async modelloLiberatoriaId => {
      try {
        const res = await modelloLiberatoriaService().find(modelloLiberatoriaId);
        modelloLiberatoria.value = res;
      } catch (error) {
        alertService.showHttpError(error.response);
      }
    };

    if (route.params?.modelloLiberatoriaId) {
      retrieveModelloLiberatoria(route.params.modelloLiberatoriaId);
    }

    return {
      ...dateFormat,
      alertService,
      modelloLiberatoria,

      ...dataUtils,

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