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 IUtenteApp } from '@/shared/model/utente-app.model';

import UtenteAppService from './utente-app.service';

export default defineComponent({
  name: 'UtenteAppDetails',
  setup() {
    const utenteAppService = inject('utenteAppService', () => new UtenteAppService());
    const alertService = inject('alertService', () => useAlertService(), true);

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

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

    const retrieveUtenteApp = async utenteAppId => {
      try {
        const res = await utenteAppService().find(utenteAppId);
        utenteApp.value = res;
      } catch (error) {
        alertService.showHttpError(error.response);
      }
    };

    if (route.params?.utenteAppId) {
      retrieveUtenteApp(route.params.utenteAppId);
    }

    return {
      alertService,
      utenteApp,

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