
// 虚实线
function lineFormate (lines) {

  return lines.map(item => {
    return item.startEndStations.map(({ start, end }) => {
      const type = item.logoReticleType === 'x0' ? 'dash' : 'solid'
      return {
        start: +start, // 起点
        end: +end, // 止点
        type, // 画线类型
        relate: item.rodaName // 关联车道
      }
    })
  }).flat()

}

// 停车带
function partFormate (parts) {
  return parts.map(item => {
    return item.startEndStations.map(({ start, end }) => {
      return {
        start: +start, // 起点
        end: +end, // 止点
        // relate: item.rodaName // 关联车道
      }
    })
  }).flat()
}

// 方向线
function arrowFormate (arrows) {

  return arrows.map(item => {
    return item.startEndStations.map(({ start, end }) => {
      return {
        start: +start, // 起点
        end: +end, // 止点
        // relate: item.rodaName // 关联车道
      }
    })
  }).flat()

}


// 基本数据格式化
function baseFormate (xTunnel) {

  const {
    uplinkDirection, tunnelRoads, tunnelLength,
    startStation, endStation
  } = xTunnel

  const filter = (type) => tunnelRoads.filter(item => item.roadType === type)

  return {
    lanes: 2,  // 车道数
    directionName: uplinkDirection, // 方向名称
    start: +startStation, // 起点
    end: +endStation, // 终点
    long: +tunnelLength, // 隧道长
    lines: lineFormate(filter('x0')), // 虚实现
    packingLot: partFormate(filter('x1')), // 停车带
    roadArrows: arrowFormate(filter('x2')) // 方向线
  }

}

// 段面格式化
function sectionComputed (long, section) {

  const leftMiles = long % section
  const completeSections = (long - leftMiles) / section

  return (new Array(completeSections + (leftMiles > 0 ? 1 : 0))).fill(null).map((_, x) => {
    return {
      section: x + 1,
      inx: x,
      grow: x < completeSections ? 1 : leftMiles / section
    }
  })

}

// 横通道格式化
function crossFormate (cross) {

  return cross.map(item => {
    const { stationLeftHole, channelType } = item
    return {
      start: +stationLeftHole,
      type: channelType
    }
  })

}

export {
  baseFormate,
  sectionComputed,
  crossFormate
}