import type { NdmSwitchPortInfo } from '@/apis';

export const getPortStatusValue = (portInfo: NdmSwitchPortInfo): string => {
  const { upDown } = portInfo;
  return upDown === 1 ? '已连接' : upDown === 2 ? '未连接' : '-';
};

export const transformPortSpeed = (portInfo: NdmSwitchPortInfo, type: 'in' | 'out' | 'total'): string => {
  const units = ['b/s', 'Kb/s', 'Mb/s', 'Gb/s', 'Tb/s'];
  const { inFlow, outFlow, flow } = portInfo;
  let result: number = 0;
  if (type === 'in') {
    result = inFlow;
  } else if (type === 'out') {
    result = outFlow;
  } else if (type === 'total') {
    result = flow;
  }
  let unit = 0;
  result *= 8;
  while (result >= 1024 && unit < units.length - 1) {
    result /= 1024;
    unit++;
  }
  return `${result.toFixed(3)} ${units[unit]}`;
};
