function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { realtimeConfig, RealtimeLayer, fullTrajectoryDelayStyle, createRealtimeFilters, realtimeDelayStyle, sortByDelay } from "mobility-toolbox-js/ol";
import getDelayString from "../../utils/getDelayString";

/**
 * Trajserv value: 'Tram',  'Subway / Metro / S-Bahn',  'Train', 'Bus', 'Ferry', 'Cable Car', 'Gondola', 'Funicular', 'Long distance bus', 'Rail',
 * New endpoint use Rail instead of Train.
 * New tracker values:  null, "tram", "subway", "rail", "bus", "ferry", "cablecar", "gondola", "funicular", "coach".
 *
 * @ignore
 */
export const types = [/^Tram/i, /^Subway( \/ Metro \/ S-Bahn)?/i, /^Train/i, /^Bus/i, /^Ferry/i, /^Cable ?Car/i, /^Gondola/i, /^Funicular/i, /^(Long distance bus|coach)/i, /^Rail/i // New endpoint use Rail instead of Train.
];
const radiusMapping = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 7, 7, 15],
// tram
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 7, 7, 15],
// subway
[2, 2, 2, 2, 2, 2, 2, 2, 7, 7, 7, 15, 15, 15, 15, 15, 15],
// rail
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 7, 7, 15],
// bus
[0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 7, 7, 7, 15, 15, 15],
// ferry
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 7, 7, 7],
// funicular
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 7, 7, 7],
// gondola
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 7, 7, 7],
// funicular
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 7, 7, 7, 15],
// coach
[2, 2, 2, 2, 2, 2, 2, 2, 7, 7, 7, 15, 15, 15, 15, 15, 15] // rail
];

/**
 * @ignore
 */
const getRadius = (type, zoom) => {
  try {
    const typeIdx = realtimeConfig.getTypeIndex(type || 0);
    return radiusMapping[typeIdx][zoom];
  } catch (e) {
    return 1;
  }
};
const getDelayText = (delayInMs, cancelled) => {
  if (cancelled) {
    return String.fromCodePoint(0x00d7);
  }

  // no realtime or realtime is broken
  if (delayInMs === null) {
    return null;
  }

  // Same function used by the RouteSchedule component
  return getDelayString(delayInMs, true);
};
class TralisLayer extends RealtimeLayer {
  constructor(options) {
    // You can filter the trains displayed using the following properties:
    // tripNumber: '007383.003849.001:3',
    // publishedLineName: 'RE,IC5,S3,17',
    // regexPublishedLineName: '^S[0-5]$'
    const urlParams = new URLSearchParams(window.location.search);
    const tripNumber = options.tripNumber || urlParams.get("tripnumber");
    const publishedLineName = options.operator || urlParams.get("publishedlinename");
    const {
      regexPublishedLineName
    } = options;
    if ((tripNumber || publishedLineName || regexPublishedLineName) && !options.filter) {
      // eslint-disable-next-line no-param-reassign
      options.filter = createRealtimeFilters(publishedLineName, tripNumber, regexPublishedLineName);
    }
    // TODO this url should be set like others url
    super(_objectSpread(_objectSpread({
      isUpdateBboxOnMoveEnd: true,
      minZoomNonTrain: 14,
      tenant: "sbb",
      style: realtimeDelayStyle,
      styleOptions: {
        getRadius,
        getDelayText
      },
      sort: sortByDelay,
      fullTrajectoryStyle: fullTrajectoryDelayStyle,
      generalizationLevelByZoom: [5, 5, 5, 5, 5, 5, 5, 5, 10, 30, 30, 100, 100, 150],
      getMotsByZoom: zoom => {
        if (zoom < 8) {
          return ["rail"];
        }
        if (zoom < 11) {
          return ["rail", "ferry"];
        }
        if (zoom < 12) {
          return ["rail", "ferry", "tram", "subway", "rail", "bus"];
        }
        return ["rail", "tram", "subway", "bus", "ferry", "cablecar", "gondola", "funicular", "coach"];
      }
    }, options), {}, {
      properties: _objectSpread({
        isQueryable: true,
        popupComponent: "PunctualityPopup",
        useTrackerMenu: true
      }, options.properties || {})
    }));
    if (options.filter) {
      this.permalinkFilter = options.filter;
    }
  }
  getFeatureInfoAtCoordinate() {
    // We return only one trajectory
    return super.getFeatureInfoAtCoordinate(...arguments).then(featureInfo => {
      var _featureInfo$features;
      return _objectSpread(_objectSpread({}, featureInfo), {}, {
        features: featureInfo !== null && featureInfo !== void 0 && (_featureInfo$features = featureInfo.features) !== null && _featureInfo$features !== void 0 && _featureInfo$features.length ? [featureInfo.features[0]] : []
      });
    });
  }
}
export default TralisLayer;
//# sourceMappingURL=TralisLayer.js.map