export async function waitForSeconds(seconds: number) {
  return new Promise<void>(resolve => {
    setTimeout(() => {
      resolve();
    }, seconds * 1000);
  });
}

export const REGEXP_PHONE =
  /^[1](([3][0-9])|([4][01456789])|([5][012356789])|([6][2567])|([7][0-8])|([8][0-9])|([9][012356789]))[0-9]{8}$/;

/** 密码正则(密码为6-18位数字/字符/符号的组合，至少2种组合) */
export const REGEXP_PWD = /^(?![0-9]+$)(?![a-zA-Z]+$)(?![!@_#$%^&*]+$)[0-9A-Za-z!@_#$%^&*]{6,18}$/;

export const getRealPostName = (line_id: number, title: string) => {
  if (line_id != 15) {
    return title;
  }
  switch (title) {
    case '行车值班员':
      return '多职能站控';
    case '值班站长':
      return '多职能队长';
    case '设备值班员':
      return '多职能巡视';
    case '站务员':
      return '站台监护';
    case '司机':
      return '多职能列控';
    default:
      return title;
  }
};

export const getRealPostNameFormLineString = (line: string, title: string) => {
  if (line != '15号线') {
    return title;
  }
  switch (title) {
    case '行车值班员':
      return '多职能站控';
    case '值班站长':
      return '多职能队长';
    case '设备值班员':
      return '多职能巡视';
    case '站务员':
      return '站台监护';
    case '司机':
      return '多职能列控';
    default:
      return title;
  }
};

/**
 * Finds the first ancestor of the given HTMLElement that has the specified attribute.
 *
 * @param {HTMLElement} element - The starting element to search from.
 * @param {string} prop - The attribute to search for.
 * @return {HTMLElement | null} The first ancestor element that has the specified attribute, or null if no such element is found.
 */
export function findAncestorWithAttribute(element: HTMLElement, prop: string): HTMLElement | null {
  let currentElement: HTMLElement | null = element;
  while (currentElement) {
    if (currentElement.hasAttribute(prop)) {
      return currentElement;
    }
    currentElement = currentElement.parentElement;
  }
  return null;
}

/**
 * Calculates the offset of an HTML element relative to the viewport.
 *
 * @param {HTMLElement} element - The HTML element to calculate the offset for.
 * @return {Object} An object containing the top and left offset of the element.
 */
export function getElementOffset(element: HTMLElement): {
  top: number;
  left: number;
} {
  const box = element.getBoundingClientRect();
  return {
    top: box.top + window.scrollY - document.documentElement.clientTop,
    left: box.left + window.scrollX - document.documentElement.clientLeft,
  };
}

export const lineColorMap = new Map([
  ['1号线', '#e3002b'],
  ['2号线', '#8cc220'],
  ['3号线', '#fcd600'],
  ['4号线', '#461d84'],
  ['5号线', '#944d9a'],
  ['6号线', '#d40068'],
  ['7号线', '#ed6f00'],
  ['8号线', '#0094d8'],
  ['9号线', '#87caed'],
  ['10号线', '#c6afd4'],
  ['11号线', '#871c2b'],
  ['12号线', '#007a60'],
  ['13号线', '#e999c0'],
  ['14号线', '#616020'],
  ['15号线', '#b6a27a'],
  ['16号线', '#98d1c0'],
  ['17号线', '#bc796f'],
  ['18号线', '#c4984e'],
  ['浦江线', '#b5b5b6'],
  ['磁浮线', '#cf5617'],
]);

export const levelMap: Record<string, { color: string; text: string }> = {
  R1: {
    color: '#FE0707',
    text: '重大风险',
  },
  R2: {
    color: '#FF914D',
    text: '较大风险',
  },
  'R2*': {
    color: '#FF914D',
    text: '较大风险',
  },
  R3: {
    color: '#FFD666',
    text: '一般风险',
  },
  R4: {
    color: '#0177FB',
    text: '低风险',
  },
};

export const getLevelText = (level: string) => {
  return levelMap[level]?.text;
};

export const getLevelColor = (level: string) => {
  return levelMap[level]?.color;
};
