import { isObject, cloneDeep, random } from 'lodash'
// 转为树结构树
export function toTree(data, id = 'id', pid = "parentId", children = 'children') {
  let obj = {}, // 使用对象重新存储数据
    res = [], // 最后结果
    len = data.length

  for (let i = 0; i < len; i++) {
    obj[data[i][id]] = data[i]
  }

  for (let j = 0; j < len; j++) {
    let list = data[j]
    let parentList = obj[list[pid]]

    if (parentList) {
      if (!parentList[children]) {
        parentList[children] = []
      }
      parentList[children].push(list)
    } else {
      res.push(list)
    }
  }

  return res
}

/* 获取标注点可视范围 */
export function getMaxMinBounds(arr) {

    if(arr.length<2) return
    let lngArr = arr.map(item => item[0])
    let latArr = arr.map(item => item[1])

    return {
      southWest: [Math.min(...lngArr),  Math.min(...latArr)],
      northEast: [Math.max(...lngArr),  Math.max(...latArr)],
    }
}



// 是否为空
export function isNull(data) {
  let str = `${data}`
  return str == 'null' || str == 'undefined' || str == '';
}


//复制对象属性
export function copyProps(target, source, exc = [], depexc = [], clear = false,objcopy = false) {

  if (!isObject(target) || !isObject(source)) {
    console.error('params 1 and parmas2 must be Object')
    return false
  }
  if (clear) {
    clearProps(target)
  }
  for (let i in source) {
    if (target.hasOwnProperty(i)) {
      if (exc.indexOf(i) == -1) {
        let type = Object.prototype.toString.call(target[i])

        if(objcopy) {
          if (type == '[object Object]') {
            copyProps(target[i], source[i], depexc, [], false, true)
          } else if (type == '[object Array]') {
            for (let j = 0; j < target[i].length; j++) {
              copyProps(target[i][j], source[i][j], depexc, [], false, true)
            }
          } else {
            target[i] = cloneDeep(source[i])
          }
        } else {
            target[i] = cloneDeep(source[i])
        }

      }

    }
  }
}

//清除对象属性
export function clearProps(target, exc = [], defulatDataObj) {

  let defulatData = Object.assign({
    '[object Array]': [],
    '[object Object]': {},
    '[object Boolean]': false,
    '[object Number]': '',
    '[object String]': '',
  },defulatDataObj)
  if (!isObject(target)) {
    console.error('params 1 must be Object')
    return false
  }
  for (let i in target) {
    if (exc.indexOf(i) == -1) {
      let type = Object.prototype.toString.call(target[i])
      // console.log(type,'type')

      if (defulatData.hasOwnProperty(type)) {
        target[i] = defulatData[type]
      } else {
        target[i] = ''
      }
    }

  }
}

// 读取对象值
export function renderItem(item, key, defalutValue = '') {
  if (item) {
    if (Object.prototype.toString.call(item) === '[object Object]') {
      if (item.hasOwnProperty(key)) {
        return renderData(item[key], defalutValue)
      } else {
        return defalutValue
      }
    } else {
      return defalutValue
    }

  } else {
    return defalutValue
  }
}
// 读取值
export function renderData(str, defalutValue = '--') {
  return isNull(str) ? defalutValue : str
}

// 方位
export function judgingDirectionAngle(data) {
    let obj;
    if (data == 0) {
        obj = "正北";
    } else if (data > 0 && data <= 44) {
        obj = "北偏东";
    } else if (data == 45) {
        obj = "东北";
    } else if (data > 45 && data <= 89) {
        obj = "东偏北";
    } else if (data == 90) {
        obj = "正东";
    } else if (data > 90 && data <= 134) {
        obj = "东偏南";
    } else if (data == 135) {
        obj = "东南";
    } else if (data > 135 && data <= 179) {
        obj = "东南";
    } else if (data == 180) {
        obj = "正南";
    } else if (data > 180 && data <= 224) {
        obj = "南偏西";
    } else if (data == 225) {
        obj = "西南";
    } else if (data > 225 && data <= 269) {
        obj = "西偏南";
    } else if (data == 270) {
        obj = "正西";
    } else if (data > 270 && data <= 314) {
        obj = "西偏北";
    } else if (data == 315) {
        obj = "西北";
    } else if (data > 315 && data <= 359) {
        obj = "北偏西";
    }
    return obj;
}

/* 处理数组增减 */
export function listCrudChange(newList=[],oldList=[],key="id") {
  let res = {
    adds: [],
    deletes: [],
  }
  let newIds = newList.map(e => e[key])
  let oldIds = oldList.map(e => e[key])
  newList.forEach(e => {
      if(!oldIds.includes(e[key])) {
        res.adds.push(e)
      }
  })
  oldList.forEach(e => {
    if(!newIds.includes(e[key])) {
        res.deletes.push(e)
      }
  })

  return res;
}

// 转换字符串，undefined,null等转化为""
export function parseStrEmpty(str) {
  if (!str || str == "undefined" || str == "null") {
    return "";
  }
  return str;
}
