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

import NotificaService from './notifica.service';

export default defineComponent({
  name: 'NotificaDetails',
  setup() {
    const dateFormat = useDateFormat();
    const notificaService = inject('notificaService', () => new NotificaService());
    const alertService = inject('alertService', () => useAlertService(), true);

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

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

    const retrieveNotifica = async notificaId => {
      try {
        const res = await notificaService().find(notificaId);
        notifica.value = res;
      } catch (error) {
        alertService.showHttpError(error.response);
      }
    };

    if (route.params?.notificaId) {
      retrieveNotifica(route.params.notificaId);
    }

    return {
      ...dateFormat,
      alertService,
      notifica,

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