import type { Router } from 'vue-router';

import { ElMessage } from 'element-plus';

import { $t } from '#/locales';

const FORCE_PASSWORD_NOTICE_INTERVAL = 1500;

let lastForcePasswordNoticeAt = 0;

export function buildForcePasswordRoute() {
  return {
    name: 'Profile',
    query: {
      force: '1',
      tab: 'password',
    },
  };
}

export function isForcePasswordRoute(
  route: Pick<{ name?: string | symbol | null; query?: Record<string, any> }, 'name' | 'query'>,
) {
  return route.name === 'Profile' && route.query?.tab === 'password';
}

export function shouldForcePasswordChange(
  userInfo?: null | Record<string, any>,
  forceChangePassword?: boolean,
) {
  return !!forceChangePassword || !!userInfo?.passwordResetRequired;
}

export function resolveForcePasswordPath(router: Router) {
  return router.resolve(buildForcePasswordRoute()).fullPath;
}

export function notifyForcePasswordChange() {
  const now = Date.now();
  if (now - lastForcePasswordNoticeAt < FORCE_PASSWORD_NOTICE_INTERVAL) {
    return;
  }
  lastForcePasswordNoticeAt = now;
  ElMessage({
    message: $t('sysAccount.forceChangePasswordNavigateTip'),
    type: 'warning',
    duration: 2200,
    grouping: true,
    plain: true,
    showClose: true,
  });
}
