import type { BvToast } from 'bootstrap-vue';
import { getCurrentInstance } from 'vue';
<%_ if (enableTranslation) { _%>
import { type Composer, useI18n } from 'vue-i18n';
<%_ } _%>

export const useAlertService = () => {
  const bvToast = getCurrentInstance().root.proxy._bv__toast;
  if (!bvToast) {
    throw new Error('BootstrapVue toast component was not found');
  }
<%_ if (enableTranslation) { _%>
  const i18n = useI18n();
<%_ } _%>
  return new AlertService({
    bvToast,
<%_ if (enableTranslation) { _%>
    i18n,
<%_ } _%>
  });
};

export default class AlertService {
  private bvToast: BvToast;
<%_ if (enableTranslation) { _%>
  private i18n: Composer;
<%_ } _%>

  constructor({
    bvToast,
<%_ if (enableTranslation) { _%>
    i18n,
<%_ } _%>
  }: {
    bvToast: BvToast;
<%_ if (enableTranslation) { _%>
    i18n: Composer,
<%_ } _%>
  }) {
    this.bvToast = bvToast;
<%_ if (enableTranslation) { _%>
    this.i18n = i18n;
<%_ } _%>
  }

  public showInfo(toastMessage: string, toastOptions?: any) {
    this.bvToast.toast(toastMessage, {
      toaster: 'b-toaster-top-center',
      title: 'Info',
      variant: 'info',
      solid: true,
      autoHideDelay: 5000,
      ...toastOptions,
    });
  }

  public showSuccess(toastMessage: string) {
    this.bvToast.toast(toastMessage, {
      toaster: 'b-toaster-top-center',
      title: 'Success',
      variant: 'success',
      solid: true,
      autoHideDelay: 5000,
    });
  }

  public showError(toastMessage: string) {
    this.bvToast.toast(toastMessage, {
      toaster: 'b-toaster-top-center',
      title: 'Error',
      variant: 'danger',
      solid: true,
      autoHideDelay: 5000,
    });
  }

  public showHttpError(httpErrorResponse: any) {
    let errorMessage: string | null = null;
    switch (httpErrorResponse.status) {
      case 0:
        errorMessage = this.i18n.t('error.server.not.reachable').toString();
        break;

      case 400: {
        const arr = Object.keys(httpErrorResponse.headers);
<%_ if (enableTranslation) { _%>
        let entityKey: string | null = null;
<%_ } _%>
        for (const entry of arr) {
          if (entry.toLowerCase().endsWith('app-error')) {
            errorMessage = httpErrorResponse.headers[entry];
<%_ if (enableTranslation) { _%>
            } else if (entry.toLowerCase().endsWith('app-params')) {
              entityKey = httpErrorResponse.headers[entry];
<%_ } _%>
            }
        }
<%_ if (enableTranslation) { _%>
        if (errorMessage && entityKey) {
          errorMessage = this.i18n.t(errorMessage, { entityName: this.i18n.t(`global.menu.entities.${entityKey}`) }).toString();
        } else if (!errorMessage) {
          errorMessage = this.i18n.t(httpErrorResponse.data.message).toString();
        }
<%_ } else { _%>
        if (!errorMessage && httpErrorResponse.data?.fieldErrors) {
          errorMessage = 'Validation error';
        } else if (!errorMessage) {
          errorMessage = httpErrorResponse.data.message;
        }
<%_ } _%>
        break;
      }

      case 404:
        errorMessage = this.i18n.t('error.http.404').toString();
        break;

      default:
<%_ if (enableTranslation) { _%>
        errorMessage = this.i18n.t(httpErrorResponse.data.message).toString();
<%_ } else { _%>
        errorMessage = httpErrorResponse.data.message;
<%_ } _%>
    }
    this.showError(errorMessage);
  }
}
