import * as apecUtils from 'apec-fe-utils'
import arrayUtil from './arrayOperation'
import $consts from '../config/constant'

// 获取数组中元素按字体比例最长长度
function getMaxLength (arr) {
  return arr.reduce((length, item) => {
    if (!item) {
    } else {
      const str = item.toString()
      const char = str.match(/[\u2E80-\u9FFF]/g)
      const charLength = char ? char.length : 0
      const num = str.match(/\d|\./g)
      const numLength = num ? num.length : 0
      const nonCharLength = str.length - charLength - numLength
      let newLength = charLength * $consts.fontRate.CHAR_RATE + numLength * $consts.fontRate.NUM_RATE + nonCharLength * $consts.fontRate.NON_CHAR_RATE
      if (str.includes('\n')) newLength = getMaxLength(str.split('\n'))
      if (length < newLength) {
        length = newLength
      }
    }
    return length
  }, 0)
}
// 转换汉字为class支持的字母
function transChar (char) {
  return encodeURIComponent(char).replace(/[^a-zA-z]/g, 'eUC')
}
// 获取元素距离浏览器工作区视窗顶端的距离
function getTopOfWorkWindow (el) {
  if (!el) return 0
  if (el.offsetParent) return el.offsetTop + getTopOfWorkWindow(el.offsetParent)
  return el.offsetTop
}

// 首字母大写
function upperFirst (str) {
  return str.substring(0, 1).toUpperCase() + str.substring(1)
}

export default {
  ...apecUtils, // 中农网前端公用方法库
  arrayUtil,
  getMaxLength,
  transChar,
  upperFirst,
  getTopOfWorkWindow,
}
