/**
 * 胃镜诊断、病理诊断 推出最终诊断
 * @param {*} key
 */

let gastricData = [
  {
    label: '溃疡',
    value: '消化性溃疡',
  },
  {
    label: '息肉',
    value: '胃息肉',
  },
  {
    label: '反流性食管炎',
    value: '反流性食管炎',
  },
  {
    label: '黏膜下隆起',
    value: '黏膜下隆起病变',
  },
  {
    label: '萎缩性胃炎',
    value: '萎缩性胃炎',
  },
]

let pathologyData = [
  {
    label: '癌',
    value: '癌',
  },
  {
    label: '高级别',
    value: '高级别上皮内瘤变',
  },
  {
    label: '低级别',
    value: '低级别上皮内瘤变',
  },
  {
    label: '异型增生',
    value: '异型增生',
  },
  {
    label: '萎缩性胃炎',
    value: '萎缩性胃炎',
  },
]
export function computeResult(gastric, pathology) {
  let result = ''

  let gastricResult = ''
  for (let index = 0; index < gastricData.length; index++) {
    if (gastric && gastric.indexOf(gastricData[index].label) > -1) {
      if (gastricData[index].label == '萎缩性胃炎') {
        if (gastric.indexOf('非萎缩性胃炎') > -1) {
          gastricResult = '非萎缩性胃炎'
        } else {
          gastricResult = '萎缩性胃炎'
        }
      } else {
        gastricResult = gastricData[index].value
      }
      break
    }
  }

  let pathologyResult = ''
  for (let index = 0; index < pathologyData.length; index++) {
    if (pathology && pathology.indexOf(pathologyData[index].label) > -1) {
      if (
        pathologyData[index].label == '高级别' ||
        pathologyData[index].label == '低级别'
      ) {
        if (pathology.indexOf('食管') > -1) {
          pathologyResult = '食管' + pathologyData[index].value
        } else {
          pathologyResult = '胃' + pathologyData[index].value
        }
      } else if (pathologyData[index].label == '癌') {
        if (pathology.indexOf('食管') > -1) {
          pathologyResult = '食管' + pathologyData[index].value
        } else {
          pathologyResult = '胃' + pathologyData[index].value
        }
      } else {
        pathologyResult = pathologyData[index].value
      }
      break
    } else {
      pathologyResult = '非萎缩性胃炎'
    }
  }

  if (
    (gastricResult == '非萎缩性胃炎' || gastricResult == '萎缩性胃炎') &&
    (pathologyResult == '非萎缩性胃炎' || pathologyResult == '萎缩性胃炎')
  ) {
    result = pathologyResult
  } else if (gastricResult == '') {
    result = pathologyResult
  } else {
    result = gastricResult + '/' + pathologyResult
  }
  return result
}
// 防抖
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result;

  const later = function () {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp;

    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last);
    } else {
      timeout = null;
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args);
        if (!timeout) context = args = null;
      }
    }
  };
  return function (...args) {
    context = this;
    timestamp = +new Date();
    const callNow = immediate && !timeout;
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait);
    if (callNow) {
      result = func.apply(context, args);
      context = args = null;
    }

    return result;
  };
}
