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 ILiberatoria } from '@/shared/model/liberatoria.model';

import LiberatoriaService from './liberatoria.service';

export default defineComponent({
  name: 'LiberatoriaDetails',
  setup() {
    const dateFormat = useDateFormat();
    const liberatoriaService = inject('liberatoriaService', () => new LiberatoriaService());
    const alertService = inject('alertService', () => useAlertService(), true);

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

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

    const retrieveLiberatoria = async liberatoriaId => {
      try {
        const res = await liberatoriaService().find(liberatoriaId);
        liberatoria.value = res;
      } catch (error) {
        alertService.showHttpError(error.response);
      }
    };

    if (route.params?.liberatoriaId) {
      retrieveLiberatoria(route.params.liberatoriaId);
    }

    return {
      ...dateFormat,
      alertService,
      liberatoria,

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