import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import polyline from "@mapbox/polyline";
import { drawArc } from "./util";

/**
 * Create a labeled-line feature for the given transit route pattern
 */
export function patternToRouteFeature(pattern, routes) {
  var _pattern$stops$stop_i;
  var route = routes.find(function (r) {
    return r.route_id === pattern.route_id;
  });
  // Concatenate geometries (arrays of coordinates) to help maplibre spread out labels (not perfect).
  var concatenatedLines = pattern.stops.map(function (stop) {
    return stop.geometry;
  }).filter(function (geometry) {
    return !!geometry;
  }).reduce(function (result, geom, index) {
    var coords = polyline.decode(geom);
    // Remove the first element (except for the first array) because it is a duplicate
    // of the last element of the previous array.
    if (index !== 0) coords.shift();
    return result.concat(coords);
  }, []);
  var routeName = route.route_short_name || route.route_long_name || "";
  var properties = {
    color: "#".concat(route.route_color || "000080"),
    name: routeName,
    routeType: route.route_type,
    textColor: "#".concat(route.route_text_color || "eee"),
    type: "route"
  };
  var isFlex = (_pattern$stops$stop_i = pattern.stops[pattern.stops.length - 1].stop_id) === null || _pattern$stops$stop_i === void 0 ? void 0 : _pattern$stops$stop_i.endsWith("flexed_to");
  var straight = polyline.toGeoJSON(polyline.encode(concatenatedLines));
  return {
    geometry: isFlex ? drawArc(straight) : straight,
    properties: properties,
    type: "Feature"
  };
}

/**
 * Obtains common layout options for route label layers.
 */
export function getRouteLayerLayout(textField) {
  // Generates a single icon based on the string length
  function generateIcon(length) {
    return [["==", ["length", ["get", textField]], length], "".concat(length)];
  }

  // Generates every icon length from 1-17. Anything higher renders a rectangle
  var iconImage = ["case"].concat(_toConsumableArray(Array(17).fill(0).map(function (_, i) {
    return generateIcon(i + 1);
  }).flat()), ["rect"]);

  // TODO: Tweak shape of roundels for perfect circle
  return {
    "icon-image": iconImage,
    "icon-optional": false,
    "icon-allow-overlap": false,
    "icon-rotation-alignment": "viewport",
    "icon-text-fit-padding": [11, 10, 11, 10],
    "icon-text-fit": "both",
    "symbol-placement": "line-center",
    "symbol-spacing": ["interpolate", ["linear"], ["zoom"], 0, 250, 10, 225, 20, 220],
    "text-allow-overlap": true,
    "text-field": ["get", textField],
    "text-font": ["Open Sans Bold", "Arial Unicode MS Bold"],
    "text-ignore-placement": true,
    "text-justify": "left",
    "text-line-height": 0.5,
    "text-letter-spacing": 0,
    "text-padding": 0,
    "text-rotation-alignment": "viewport",
    "text-size": 13
  };
}
//# sourceMappingURL=route-layers.js.map