function mergeBigData(datas) {
  let index = 0
  let len = datas.reduce((p, c) => p + c.length, 0)
  let v3 = new Float32Array(len * 3)

  datas.forEach(data => {
    let length = data.length
    for (let i = 0; i < length; i++) {
      v3[index] = data[i].x
      v3[index + 1] = data[i].y
      v3[index + 2] = data[i].z
      index += 3
    }
  })

  return v3
}

function getMaxHeight(verticesBuffArrays) {
  let maxHeight = verticesBuffArrays[2]
  let len = verticesBuffArrays.length

  for (let i = 3; i < len; i += 3) {
    if (maxHeight < verticesBuffArrays[i + 2]) {
      maxHeight = verticesBuffArrays[i + 2]
    }
  }

  return maxHeight
}

function grentAnimationArray(verticesBuffArrays, stepTotal, maxHeight) {
  let len = verticesBuffArrays.length
  let animateBuffArrays = new Float32Array(
    (len / 3) * stepTotal
  )

  for (let i = 0, index = 0; i < len; i += 3, index++) {
    let indexes = index * stepTotal
    let origin = verticesBuffArrays[i + 2]
    let average = (maxHeight - origin) / stepTotal
    for (let j = 1; j < stepTotal + 1; j++) {
      animateBuffArrays[indexes + j - 1] = maxHeight - (j * average)
    }
  }

  return animateBuffArrays
}


function extractType(features) {
  let types = []
  let typeFeatures = [[]]

  features.forEach(f => {
    if (f.properties && f.properties.type) {
      let index = types.indexOf(f.properties.type)
      // index = 0
      if (~index) {
        typeFeatures[index + 1].push(f)
      } else {
        types.push(f.properties.type)
        typeFeatures[types.length] = [f]
      }
    } else {
      typeFeatures[0].push(f)
    }
  })

  return {
    typeFeatures,
    types
  }
}



function fromGeometry(geometry) {

  var faces = geometry.faces;
  var vertices = geometry.vertices;

  if (vertices.length > 0 && faces.length === 0) {

    console.error('THREE.DirectGeometry: Faceless geometries are not supported.');

  }

  var _vertices = [],
    _normals = [];

  for (var i = 0; i < faces.length; i++) {

    var face = faces[i];

    _vertices.push(vertices[face.a], vertices[face.b], vertices[face.c]);

    var vertexNormals = face.vertexNormals;

    if (vertexNormals.length === 3) {

      _normals.push(vertexNormals[0], vertexNormals[1], vertexNormals[2]);

    } else {

      var normal = face.normal;

      _normals.push(normal, normal, normal);

    }

  }

  return {
    vertices: _vertices,
    normals: _normals,
  }
}

function cuttingString(str, len = 1) {
  let child = []
  // var re = /[^/u4e00-/u9fa5]/;
  // str.indexOf()
  for (let i = 0; i < str.length; i += len) {
    
    child.push(
      str.substr(i, i + len > str.length ? str.length - i : len)
    )
  }

  return child
}

export { mergeBigData, getMaxHeight, grentAnimationArray, extractType, fromGeometry, cuttingString }