import type { ErrorMessageMode } from '#/axios';
import { useMessage } from '@/hooks/web/useMessage';
<%_ if (enableTranslation) { _%>
import { useI18n } from '@/hooks/web/useI18n';
<%_ } _%>
// import router from '@/router';
// import { PageEnum } from '@/enums/pageEnum';
import { useUserStoreWithOut } from '@/store/modules/user';
import projectSetting from '@/settings/projectSetting';
import { SessionTimeoutProcessingEnum } from '@/enums/appEnum';

// begcode-please-regenerate-this-file 如果您不希望重新生成代码时被覆盖，将please修改为don't ！！！

const { createMessage, createErrorModal } = useMessage();
const error = createMessage.error!;
const stp = projectSetting.sessionTimeoutProcessing;

export function checkStatus(
  status: number,
  msg: string,
  errorMessageMode: ErrorMessageMode = 'message',
): void {
<%_ if (enableTranslation) { _%>
  const { t } = useI18n();
<%_ } _%>
  const userStore = useUserStoreWithOut();
  let errMessage = '';

  switch (status) {
    case 400:
      errMessage = `${msg}`;
      break;
    // 401: Not logged in
    // Jump to the login page if not logged in, and carry the path of the current page
    // Return to the current page after successful login. This step needs to be operated on the login page.
    case 401:
      userStore.setToken(undefined);
      errMessage = msg || <% if (enableTranslation) { %>t('sys.api.errMsg401')<% } else { %>'t$t('sys.api.errMsg401')$t$'<% } %>;
      if (stp === SessionTimeoutProcessingEnum.PAGE_COVERAGE) {
        userStore.setSessionTimeout(true);
      } else {
        userStore.logout(true);
      }
      break;
    case 403:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg403')<% } else { %>'t$t('sys.api.errMsg403')$t$'<% } %>;
      break;
    // 404请求不存在
    case 404:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg404')<% } else { %>'t$t('sys.api.errMsg404')$t$'<% } %>;
      break;
    case 405:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg405')<% } else { %>'t$t('sys.api.errMsg405')$t$'<% } %>;
      break;
    case 408:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg408')<% } else { %>'t$t('sys.api.errMsg408')$t$'<% } %>;
      break;
    case 500:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg500')<% } else { %>'t$t('sys.api.errMsg500')$t$'<% } %>;
      break;
    case 501:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg501')<% } else { %>'t$t('sys.api.errMsg501')$t$'<% } %>;
      break;
    case 502:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg502')<% } else { %>'t$t('sys.api.errMsg502')$t$'<% } %>;
      break;
    case 503:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg503')<% } else { %>'t$t('sys.api.errMsg503')$t$'<% } %>;
      break;
    case 504:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg504')<% } else { %>'t$t('sys.api.errMsg504')$t$'<% } %>;
      break;
    case 505:
      errMessage = <% if (enableTranslation) { %>t('sys.api.errMsg505')<% } else { %>'t$t('sys.api.errMsg505')$t$'<% } %>;
      break;
    default:
  }

  if (errMessage) {
    if (errorMessageMode === 'modal') {
      createErrorModal({ title: <% if (enableTranslation) { %>t('sys.api.errorTip')<% } else { %>'t$t('sys.api.errorTip')$t$'<% } %>, content: errMessage });
    } else if (errorMessageMode === 'message') {
      error({ content: errMessage, key: `global_error_message_status_${status}` });
    }
  }
}
