import along from 'turf-along'
import lineDistance from 'turf-line-distance'

export const componentList = ['route', 'stop', 'fare', 'feedinfo', 'calendar', 'scheduleexception', 'agency']
export const subComponentList = ['trippattern']
export const subSubComponentList = ['timetable']

export const getEntityName = (component, entity) => {
  if (!entity) {
    return '[Unnamed]'
  }
  let entName = component === 'agency'
    ? 'agency_name'
    : component === 'route'
    ? 'route_short_name'
    : component === 'stop'
    ? 'stop_name'
    : component === 'calendar'
    ? 'description'
    : component === 'fare'
    ? 'fare_id'
    : component === 'scheduleexception'
    ? 'name'
    : null
  switch (component) {
    case 'stop':
      return entity.stop_name && entity.stop_code
        ? `${entity.stop_name} (${entity.stop_code})`
        : entity.stop_name && entity.stop_id
        ? `${entity.stop_name} (${entity.stop_id})`
        : entity.stop_name
    case 'route':
      return entity.route_short_name && entity.route_long_name && entity.route_short_name !== '""' && entity.route_long_name !== '""'
        ? `${entity.route_short_name} - ${entity.route_long_name}`
        : entity.route_short_name && entity.route_short_name !== '""'
        ? entity.route_short_name
        : entity.route_long_name && entity.route_long_name !== '""'
        ? entity.route_long_name
        : entity.route_id
    default:
      return entity[entName]
  }
}

export const gtfsIcons = [
  {
    id: 'feedinfo',
    icon: 'info',
    title: 'Edit feed info'
  },
  {
    id: 'agency',
    icon: 'building',
    title: 'Edit agencies'
  },
  {
    id: 'route',
    icon: 'bus',
    title: 'Edit routes'
  },
  {
    id: 'stop',
    icon: 'map-marker',
    title: 'Edit stops'
  },
  {
    id: 'calendar',
    icon: 'calendar',
    title: 'Edit calendars'
  },
  {
    id: 'fare',
    icon: 'ticket',
    title: 'Edit fares'
  }
]

export const getControlPoints = (pattern, snapToStops) => {
  if (!pattern) {
    return []
  }
  let controlPoints = []
  pattern.patternStops && pattern.patternStops.map((ps, index) => {
    // set distance to average of patternStop and next patternStop, if last stop set to end of segment
    let distance = pattern.patternStops[index + 1]
      ? (pattern.patternStops[index + 1].shapeDistTraveled + ps.shapeDistTraveled) / 2
      : lineDistance(pattern.shape, 'meters')
    let point = along(pattern.shape, distance, 'meters')
    let controlPoint = {
      point,
      distance: distance,
      permanent: true
    }
    let stopPoint = along(pattern.shape, ps.shapeDistTraveled, 'meters')
    let stopControl = {
      point: stopPoint,
      permanent: true,
      distance: ps.shapeDistTraveled,
      ...ps
    }
    if (snapToStops) {
      stopControl.hidden = true
    }
    controlPoints.push(stopControl)
    controlPoints.push(controlPoint)
  })
  return controlPoints
}

@@ -69,16 +69,92 @@
 istance'
+%0Aimport %7B latLngBounds %7D from 'leaflet'%0Aimport %7B extent %7D from 'turf-extent'
 %0A%0Aexport
@@ -355,16 +355,977 @@
 able'%5D%0A%0A
+export const getEntityBounds = (entity, offset = 0.005) =%3E %7B%0A  if (!entity) return null%0A%0A  // %5Blng, lat%5D%0A  if (entity.constructor === Array) %7B%0A    return latLngBounds(%5B%5Bentity%5B1%5D + offset, entity%5B0%5D - offset%5D, %5Bentity%5B1%5D - offset, entity%5B0%5D + offset%5D%5D)%0A  %7D%0A  // stop%0A  else if (typeof entity.stop_lat !== 'undefined') %7B%0A    return latLngBounds(%5B%5Bentity.stop_lat + offset, entity.stop_lon - offset%5D, %5Bentity.stop_lat - offset, entity.stop_lon + offset%5D%5D)%0A  %7D%0A  // route%0A  else if (typeof entity.tripPatterns !== 'undefined') %7B%0A    let coordinates = %5B%5D%0A    entity.tripPatterns.map(pattern =%3E %7B%0A      if (pattern.shape.coordinates) %7B%0A        coordinates = %5B%0A          ...coordinates,%0A          ...pattern.shape.coordinates.map(c =%3E (%5Bc%5B1%5D, c%5B0%5D%5D))%0A        %5D%0A      %7D%0A    %7D)%0A    return latLngBounds(coordinates)%0A  %7D%0A  // trip pattern%0A  else if (typeof entity.shape !== 'undefined') %7B%0A    return latLngBounds(entity.shape.coordinates.map(c =%3E (%5Bc%5B1%5D, c%5B0%5D%5D)))%0A  %7D%0A%7D%0A%0A
 export c

add getBounds