"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getRouteLayerLayout = getRouteLayerLayout;
exports.patternToRouteFeature = patternToRouteFeature;
var _polyline = _interopRequireDefault(require("@mapbox/polyline"));
var _util = require("./util");
/**
 * Create a labeled-line feature for the given transit route pattern
 */
function patternToRouteFeature(pattern, routes) {
  var _pattern$stops$stop_i;
  const route = routes.find(r => r.route_id === pattern.route_id);
  // Concatenate geometries (arrays of coordinates) to help maplibre spread out labels (not perfect).
  const concatenatedLines = pattern.stops.map(stop => stop.geometry).filter(geometry => !!geometry).reduce((result, geom, index) => {
    const coords = _polyline.default.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);
  }, []);
  const routeName = route.route_short_name || route.route_long_name || "";
  const properties = {
    color: `#${route.route_color || "000080"}`,
    name: routeName,
    routeType: route.route_type,
    textColor: `#${route.route_text_color || "eee"}`,
    type: "route"
  };
  const 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");
  const straight = _polyline.default.toGeoJSON(_polyline.default.encode(concatenatedLines));
  return {
    geometry: isFlex ? (0, _util.drawArc)(straight) : straight,
    properties,
    type: "Feature"
  };
}

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

  // Generates every icon length from 1-17. Anything higher renders a rectangle
  const iconImage = ["case", ...Array(17).fill(0).map((_, i) => 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