import { defineComponent, inject, ref, type Ref } from 'vue';
<%_ if (enableTranslation) { _%>
import { useI18n } from 'vue-i18n';
<%_ } _%>

import HealthService from './health.service';
import <%=jhiPrefixCapitalized%>HealthModal from './health-modal.vue';

export default defineComponent({
  name: '<%=jhiPrefixCapitalized%>Health',
  components: {
    'health-modal': <%=jhiPrefixCapitalized%>HealthModal,
  },
  setup() {
    const healthService = inject('healthService', () => new HealthService(), true);

    const healthData: Ref<any> = ref(null);
    const currentHealth: Ref<any> = ref(null);
    const updatingHealth = ref(false);

    return {
      healthService,
      healthData,
      currentHealth,
      updatingHealth,
<%_ if (enableTranslation) { _%>
      t$: useI18n().t,
<%_ } _%>
    };
  },
  mounted(): void {
    this.refresh();
  },
  methods: {
    baseName(name: any): any {
      return this.healthService.getBaseName(name);
    },
    getBadgeClass(statusState: any): string {
      if (statusState === 'UP') {
        return 'bg-success';
      }
      return 'bg-danger';
    },
    refresh(): void {
      this.updatingHealth = true;
      this.healthService
        .checkHealth()
        .then(res => {
          this.healthData = this.healthService.transformHealthData(res.data);
          this.updatingHealth = false;
        })
        .catch(error => {
          if (error.status === 503) {
            this.healthData = this.healthService.transformHealthData(error.error);
          }
          this.updatingHealth = false;
        });
    },
    showHealth(health: any): void {
      this.currentHealth = health;
      (<any>this.$refs.healthModal).show();
    },
    subSystemName(name: string): string {
      return this.healthService.getSubSystemName(name);
    },
  },
});
