import { multiply, round } from 'lodash'

/**
 * 清除对象中的字符串前后空格
 * @param target 目标对象
 * @returns 去除前后空格后的对象
 */
export const recordValueTrim = (target: Record<string, any>) => {
  // 不为空且是对象或数组
  if (target && typeof target === 'object') {
    // 如果数组则使用数组解构，如果是对象则使用对象结构
    const query: any = target instanceof Array ? [...target] : { ...target }
    const keys = Object.keys(query)
    keys.forEach((key) => {
      // 是string则进行trim
      if (typeof query[key] === 'string') {
        query[key] = (query[key] as string).trim()
        return
      }
      // 如果是数组，则递归处理
      if (query[key] instanceof Array) {
        query[key] = query[key].map((item: any) => {
          return typeof item === 'string' ? item.trim() : recordValueTrim(item)
        })
        return
      }
      // 如果是对象则处理对象
      if (typeof query[key] === 'object') {
        query[key] = recordValueTrim(query[key])
      }
    })

    return query
  }

  return target
}

//格式化档位
export function formatGears(gear: any) {
  let gears = gear.gears
  if (gear.variableType == 1) {
    if (gears.length == 0) {
      return '零位'
    } else {
      if (gears.length > 1) {
        return gears[0] + gears[gears.length - 1]
      } else {
        return gears[0]
      }
    }
  } else {
    if (gears.length == 0) {
      return '零位'
    } else {
      return gears[gears.length - 1]
    }
  }
}

/** value值,  translate 系数*/
export const formatValue = (value: any, translate: any) => {
  if (value == null) return '--'
  if (value == "闭合" || value == "开启") {
    return value
  }

  let num = parseFloat(value) * translate;
  let surplus = num % 1;
  if (surplus != 0.0) {
    return (Math.floor(num * 100) / 100).toFixed(2)
  } else {
    //无小数
    return parseInt(num.toString());
  }

}

/**
 *
 * @param light 处理载荷 灯
 * @returns
 */
export function formatWeightLight(light: any) {
  const obj: any = {
    green: '#6AD28C',
    red: '#FE5656',
    yellow: '#F49F51',
    gray: '#9999'
  }
  if (light == null) {
    return obj['gray']
  }

  if (light.value == 'true' || light.value == true) {
    return obj['red']
  } else {
    return obj['green']
  }
}

